diff --git a/readline/src/pad/prac1/Command.java b/readline/src/pad/prac1/Command.java index 173b684..14a2320 100644 --- a/readline/src/pad/prac1/Command.java +++ b/readline/src/pad/prac1/Command.java @@ -9,7 +9,6 @@ public class Command public static final int INSERT_CHAR = 0; public static final int DELETE_CHAR = 1; public static final int MOVE_CURSOR = 2; - public static final int NEWLINE = 3; private int type; private int value; diff --git a/readline/src/pad/prac1/Console.java b/readline/src/pad/prac1/Console.java index 0d8e8ed..feec07f 100644 --- a/readline/src/pad/prac1/Console.java +++ b/readline/src/pad/prac1/Console.java @@ -7,53 +7,104 @@ import java.util.regex.Pattern; public class Console implements Observer { + private Coordinate currentXY; + //private int numRows; private int numCols; - private int numRows; - private int currentRow; - public void updateCurrentPos() + public Console() { - Coordinate pos = getCurrentPos(); - currentRow = pos.y(); + currentXY = new Coordinate(0,0); } - public void updateTermSize() + public void moveCursorTo(int pos, int currentPos, int length) { - // If we could just handle SIGWINCH, this would not be necessary and it would run A LOT faster - System.out.print((char)EditableBufferedReader.ESC); - System.out.print((char)EditableBufferedReader.ESC_SEQ); - System.out.print("18t"); - Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)"); - Scanner scanner = new Scanner(System.in); - scanner.findWithinHorizon(pattern,0); - numCols = Integer.parseInt(scanner.match().group(2)); - numRows = Integer.parseInt(scanner.match().group(1)); - System.err.println("TERMSIZE:" + numCols + " " + numRows); + updateTermSize(); + updateCurrentPos(); + if(pos > currentPos) + { + while((pos-currentPos) >= numCols) + { + currentPos += numCols - currentXY.x()+1; + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print((char)EditableBufferedReader.DOWN); + currentXY.setY(currentXY.y()+1); + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print(numCols-1); + System.out.print((char)EditableBufferedReader.BACKWARD); + currentXY.setX(1); + } + if(pos > currentPos) + { + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print(pos - currentPos); + System.out.print((char)EditableBufferedReader.FORWARD); + } + } + else if(pos < currentPos) + { + System.err.println(currentPos); + System.err.println(pos); + System.err.println(currentXY); + while((currentPos-pos) >= currentXY.x()) + { + currentPos -= currentXY.x(); + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print((char)EditableBufferedReader.UP); + currentXY.setY(currentXY.y()-1); + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print(numCols); + System.out.print((char)EditableBufferedReader.FORWARD); + currentXY.setX(numCols); + } + if(pos < currentPos) + { + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print(currentPos-pos); + System.out.print((char)EditableBufferedReader.BACKWARD); + } + } } - public Coordinate getCurrentPos() + public void updateCurrentPos() + { + Coordinate pos = getCurrentPos(); + currentXY.setY(pos.y()); + currentXY.setX(pos.x()); + } + + public void updateTermSize() { - System.out.print((char)EditableBufferedReader.ESC); - System.out.print((char)EditableBufferedReader.ESC_SEQ); - System.out.print("6n"); - Pattern pattern = Pattern.compile("(\\d+);(\\d+)"); - Scanner scanner = new Scanner(System.in); - scanner.findWithinHorizon(pattern,0); - int x = Integer.parseInt(scanner.match().group(2)); - int y = Integer.parseInt(scanner.match().group(1)); - System.err.println("POS:" + x + " " + y); - return new Coordinate(x,y); + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print("18t"); + Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)"); + Scanner scanner = new Scanner(System.in); + scanner.findWithinHorizon(pattern,0); + numCols = Integer.parseInt(scanner.match().group(2)); + //numRows = Integer.parseInt(scanner.match().group(1)); } - public void moveCursorTo(Coordinate xy) + public Coordinate getCurrentPos() { - System.out.print((char)EditableBufferedReader.ESC); - System.out.print((char)EditableBufferedReader.ESC_SEQ); - System.out.print(xy.y()); - System.out.print(';'); - System.out.print(xy.x()); - System.out.print((char)EditableBufferedReader.GOTO); + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print("6n"); + Pattern pattern = Pattern.compile("(\\d+);(\\d+)"); + Scanner scanner = new Scanner(System.in); + scanner.findWithinHorizon(pattern,0); + int x = Integer.parseInt(scanner.match().group(2)); + int y = Integer.parseInt(scanner.match().group(1)); + return new Coordinate(x,y); } + + + private void insertSpace() { @@ -80,8 +131,6 @@ public class Console implements Observer public void update(Observable obs, Object arg) { - updateCurrentPos(); - updateTermSize(); Line line = (Line)obs; Command cmd = (Command)arg; switch(cmd.getType()) @@ -95,19 +144,8 @@ public class Console implements Observer break; case Command.MOVE_CURSOR: - currentRow += cmd.getValue()/numCols; - if(currentRow > numRows) - { - currentRow = numRows; - } - Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow); - moveCursorTo(xy); + moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length()); break; - - case Command.NEWLINE: - System.out.print((char)EditableBufferedReader.LINE_FEED); - System.out.print((char)EditableBufferedReader.RETURN_KEY); - currentRow++; } } } diff --git a/readline/src/pad/prac1/Coordinate.java b/readline/src/pad/prac1/Coordinate.java index d136dbe..c976a3d 100644 --- a/readline/src/pad/prac1/Coordinate.java +++ b/readline/src/pad/prac1/Coordinate.java @@ -30,4 +30,9 @@ public class Coordinate { yPos = y; } + + public String toString() + { + return "("+xPos+","+yPos+")"; + } } diff --git a/readline/src/pad/prac1/EditableBufferedReader.java b/readline/src/pad/prac1/EditableBufferedReader.java index b4ded63..3fcfe08 100644 --- a/readline/src/pad/prac1/EditableBufferedReader.java +++ b/readline/src/pad/prac1/EditableBufferedReader.java @@ -26,9 +26,9 @@ public class EditableBufferedReader extends BufferedReader /* KEY DEFINITIONS * - * ESC (0x1B) starts an escape sequence + * ESC (0x1B) Starts an escape sequence * ESC+[+C (0x1B 0x5B 0x43) Cursor Forward - * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward + * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward * ESC+[+2+~ (0x1B 0x5B 0x32 0x7E) Insert * ESC+[+3+~ (0x1B 0x5B 0x33 0x7E) Delete forward * ESC+O+H (0x1B 0x4F 0x48) Home @@ -40,6 +40,8 @@ public class EditableBufferedReader extends BufferedReader public static final int BACKSPACE_DEL = 0x7F; public static final int ESC = 0x1B; public static final int ESC_SEQ = 0x5B; + public static final int UP = 0x41; + public static final int DOWN = 0x42; public static final int FORWARD = 0x43; public static final int BACKWARD = 0x44; public static final int GOTO = 0x48; @@ -54,9 +56,9 @@ public class EditableBufferedReader extends BufferedReader public static final int EOF = 0x04; public static final int LINE_FEED = 0x0A; - private Console console = new Console(); - private Line line = new Line(console); - private boolean endOfFile = false; + private Console console = new Console(); + private Line line = new Line(console); + private boolean endOfFile = false; public EditableBufferedReader(Reader in) { @@ -132,17 +134,15 @@ public class EditableBufferedReader extends BufferedReader int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); switch(character) { - case EOF: - endOfFile = true; - break; - case BACKSPACE: case BACKSPACE_DEL: line.delChar(DEL_BACKWARD); break; + case EOF: case RETURN_KEY: - character = LINE_FEED; + endOfFile = true; + break; default: line.addChar((char)character); break; @@ -192,4 +192,4 @@ public class EditableBufferedReader extends BufferedReader System.out.println(""); return line.toString(); } -} \ No newline at end of file +} diff --git a/readline/src/pad/prac1/Line.java b/readline/src/pad/prac1/Line.java index 117e4b7..797baa8 100644 --- a/readline/src/pad/prac1/Line.java +++ b/readline/src/pad/prac1/Line.java @@ -88,31 +88,26 @@ public class Line extends Observable { if(c == EditableBufferedReader.LINE_FEED) { - sendCommand(Command.NEWLINE); - insertCharAt(c, cursorPosition-1); - cursorPosition++; + sendCommand(Command.INSERT_CHAR,EditableBufferedReader.RETURN_KEY); } - else + switch(writeMode) { - switch(writeMode) - { - case EditableBufferedReader.INSERT: - sendCommand(Command.INSERT_CHAR,(int)c); - insertCharAt(c, cursorPosition-1); - cursorPosition++; - break; - - case EditableBufferedReader.OVERWRITE: - if(cursorPosition != line.length()+1) - { - removeCharAt(cursorPosition-1); - sendCommand(Command.DELETE_CHAR); - } - sendCommand(Command.INSERT_CHAR,(int)c); - insertCharAt(c, cursorPosition-1); - cursorPosition++; + case EditableBufferedReader.INSERT: + sendCommand(Command.INSERT_CHAR,(int)c); + insertCharAt(c, cursorPosition-1); + cursorPosition++; break; - } + + case EditableBufferedReader.OVERWRITE: + if(cursorPosition != line.length()+1) + { + removeCharAt(cursorPosition-1); + sendCommand(Command.DELETE_CHAR); + } + sendCommand(Command.INSERT_CHAR,(int)c); + insertCharAt(c, cursorPosition-1); + cursorPosition++; + break; } } diff --git a/readline/src/pad/prac1/MainClass.java b/readline/src/pad/prac1/MainClass.java index f3e25d9..09ec8d6 100644 --- a/readline/src/pad/prac1/MainClass.java +++ b/readline/src/pad/prac1/MainClass.java @@ -12,14 +12,5 @@ public class MainClass System.out.println("\nLine read: \n" + editable.readLine()); editable.close(); } -} -/* TO-DO - * up/down keys - * pgup pgdown keys (goesto penultima linea visible) - * implement change of terminal dimensions - * - * TO-FIX - * Cursor keys, Home and end keys when new line, reset line length - * SPEED - */ \ No newline at end of file +}