diff --git a/readline/bin/pad/prac1/Console.class b/readline/bin/pad/prac1/Console.class index 139706b..6a989ea 100644 --- a/readline/bin/pad/prac1/Console.class +++ b/readline/bin/pad/prac1/Console.class diff --git a/readline/bin/pad/prac1/EditableBufferedReader.class b/readline/bin/pad/prac1/EditableBufferedReader.class index 8d4ea99..de53197 100644 --- a/readline/bin/pad/prac1/EditableBufferedReader.class +++ b/readline/bin/pad/prac1/EditableBufferedReader.class diff --git a/readline/src/pad/prac1/Console.java b/readline/src/pad/prac1/Console.java index c677811..8832729 100644 --- a/readline/src/pad/prac1/Console.java +++ b/readline/src/pad/prac1/Console.java @@ -2,25 +2,41 @@ package pad.prac1; import java.util.Observable; import java.util.Observer; +import java.util.Scanner; +import java.util.regex.Pattern; public class Console implements Observer { - public void moveCursorTo(int pos, int currentPos, int length) + private int currentCol; + private int currentRow; + + public void updateCurrentPos() { - 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.out.print((char)EditableBufferedReader.ESC); - System.out.print((char)EditableBufferedReader.ESC_SEQ); - System.out.print(currentPos-pos); - System.out.print((char)EditableBufferedReader.BACKWARD); - } + Coordinate pos = getCurrentPos(); + currentCol = pos.x(); + currentRow = pos.y(); + } + + public Coordinate getCurrentPos() + { + 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); + return new Coordinate(Integer.parseInt(scanner.match().group(2)), + Integer.parseInt(scanner.match().group(1))); + } + + public void moveCursorTo(Coordinate xy) + { + 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); } private void insertSpace() @@ -48,6 +64,7 @@ public class Console implements Observer public void update(Observable obs, Object arg) { + updateCurrentPos(); Line line = (Line)obs; Command cmd = (Command)arg; switch(cmd.getType()) @@ -61,7 +78,9 @@ public class Console implements Observer break; case Command.MOVE_CURSOR: - moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length()); + Coordinate xy = new Coordinate(cmd.getValue(),currentRow); + //Gestionar si excede el numero de columnas con modulos y asi + moveCursorTo(xy); break; } } diff --git a/readline/src/pad/prac1/EditableBufferedReader.java b/readline/src/pad/prac1/EditableBufferedReader.java index 71cf52d..70fe225 100644 --- a/readline/src/pad/prac1/EditableBufferedReader.java +++ b/readline/src/pad/prac1/EditableBufferedReader.java @@ -1,5 +1,7 @@ package pad.prac1; import java.io.*; +import java.util.Scanner; +import java.util.regex.Pattern; public class EditableBufferedReader extends BufferedReader @@ -39,6 +41,7 @@ public class EditableBufferedReader extends BufferedReader public static final int ESC_SEQ = 0x5B; public static final int FORWARD = 0x43; public static final int BACKWARD = 0x44; + public static final int GOTO = 0x48; public static final int DEL = 0x33; public static final int TILDE = 0x7E; public static final int HOME_END = 0x4F; @@ -76,21 +79,77 @@ public class EditableBufferedReader extends BufferedReader Runtime.getRuntime().exec(cmd).waitFor(); } - private int parseKey(int key) + private void parseKey() throws IOException { - if(key < 0x20) + Pattern pattern; + Scanner scanner = new Scanner(System.in); + pattern = Pattern.compile("(.{1})"); + if(scanner.findWithinHorizon(pattern,0) != null) { - if(key == ESC) + int character = (int)scanner.match().group(1).charAt(0); + if(character >= 0x20 || character == EOF || character == BACKSPACE || character == RETURN_KEY) { - return ESCAPE_SEQUENCE; - } - else if((key == BACKSPACE) || (key == RETURN_KEY) || (key == EOF)) - { - return PRINTABLE; + switch(character) + { + case EOF: + endOfFile = true; + break; + + case BACKSPACE: + case BACKSPACE_DEL: + line.delChar(DEL_BACKWARD); + break; + + case RETURN_KEY: + character = LINE_FEED; + default: + line.addChar((char)character); + break; + } + return; } - return NON_PRINTABLE; } - return PRINTABLE; + pattern = Pattern.compile("\\^\\[\\[(.{1}).*"); + if(scanner.findWithinHorizon(pattern,3) != null) //escape sequence + { + int character = scanner.match().group(1).charAt(0); + switch(character) + { + case FORWARD: + line.setCursorTo(line.getCursorPosition()+1); + break; + + case BACKWARD: + line.setCursorTo(line.getCursorPosition()-1); + break; + + case DEL: + line.delChar(DEL_FORWARD); + break; + + case INSERT_KEY: + line.toggleMode(); + break; + } + return; + } + pattern = Pattern.compile("\\^\\[O(\\w{1})"); + if(scanner.findWithinHorizon(pattern,3) != null) //HOME or END + { + + int character = (int)scanner.match().group(1).charAt(0); + switch(character) + { + case HOME: + line.setCursorTo(1); + break; + + case END: + line.setCursorTo(line.length()+1); + break; + } + return; + } } public int read() throws IOException @@ -109,90 +168,18 @@ public class EditableBufferedReader extends BufferedReader System.out.println("Couldn't set terminal in raw mode"); return ""; } - while(!endOfFile) + try { - try + while(!endOfFile) { - int character = read(); - //System.err.print((String)(Integer.toHexString(character) + " ").toUpperCase()); - switch(parseKey(character)) - { - case ESCAPE_SEQUENCE: - switch(read()) - { - case ESC_SEQ: - switch(read()) - { - case FORWARD: - line.setCursorTo(line.getCursorPosition()+1); - break; - - case BACKWARD: - line.setCursorTo(line.getCursorPosition()-1); - break; - - case DEL: - if(read() == TILDE) - { - line.delChar(DEL_FORWARD); - } - break; - - case INSERT_KEY: - if(read() == TILDE) - { - line.toggleMode(); - } - break; - } - break; - - case HOME_END: - switch(read()) - { - case HOME: - line.setCursorTo(1); - break; - - case END: - line.setCursorTo(line.length()+1); - break; - } - break; - } - break; - - case PRINTABLE: - switch(character) - { - case EOF: - endOfFile = true; - break; - - case BACKSPACE: - case BACKSPACE_DEL: - line.delChar(DEL_BACKWARD); - break; - - case RETURN_KEY: - character = LINE_FEED; - default: - line.addChar((char)character); - break; - } - break; - - case NON_PRINTABLE: - //ignore - break; - } - } - catch (IOException e) - { - System.out.println("Error reading line"); - break; + parseKey(); } } + catch(Exception e) + { + System.out.println("Error reading line"); + return ""; + } try { unsetRaw();