From 90d375bfc0dd0411f3ae044e4d45c674841b132b Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Wed, 25 Sep 2013 09:14:29 +0000 Subject: [PATCH] git-svn-id: svn://imanolbarba.net/PAD@9 c2ee353e-ed0d-4329-bf56-03aec153487f --- readline/bin/pad/prac1/Command.class | Bin 0 -> 740 bytes readline/bin/pad/prac1/Console.class | Bin 0 -> 1654 bytes readline/bin/pad/prac1/EditableBufferedReader.class | Bin 4828 -> 0 bytes readline/bin/pad/prac1/Line.class | Bin 1995 -> 0 bytes readline/bin/pad/prac1/MainClass.class | Bin 1123 -> 0 bytes readline/src/pad/prac1/Command.java | 37 +++++++++++++++++++++++++++++++++++++ readline/src/pad/prac1/Console.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ readline/src/pad/prac1/EditableBufferedReader.java | 135 +++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------ readline/src/pad/prac1/Line.java | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- readline/src/pad/prac1/MainClass.java | 2 +- 10 files changed, 225 insertions(+), 131 deletions(-) create mode 100644 readline/bin/pad/prac1/Command.class create mode 100644 readline/bin/pad/prac1/Console.class create mode 100644 readline/src/pad/prac1/Command.java create mode 100644 readline/src/pad/prac1/Console.java diff --git a/readline/bin/pad/prac1/Command.class b/readline/bin/pad/prac1/Command.class new file mode 100644 index 0000000..5c8b189 Binary files /dev/null and b/readline/bin/pad/prac1/Command.class differ diff --git a/readline/bin/pad/prac1/Console.class b/readline/bin/pad/prac1/Console.class new file mode 100644 index 0000000..139706b Binary files /dev/null and b/readline/bin/pad/prac1/Console.class differ diff --git a/readline/bin/pad/prac1/EditableBufferedReader.class b/readline/bin/pad/prac1/EditableBufferedReader.class index 92d45c1..8d4ea99 100644 Binary files a/readline/bin/pad/prac1/EditableBufferedReader.class and b/readline/bin/pad/prac1/EditableBufferedReader.class differ diff --git a/readline/bin/pad/prac1/Line.class b/readline/bin/pad/prac1/Line.class index 52d14f9..a1ab20f 100644 Binary files a/readline/bin/pad/prac1/Line.class and b/readline/bin/pad/prac1/Line.class differ diff --git a/readline/bin/pad/prac1/MainClass.class b/readline/bin/pad/prac1/MainClass.class index f6c38ec..5c0045f 100644 Binary files a/readline/bin/pad/prac1/MainClass.class and b/readline/bin/pad/prac1/MainClass.class differ diff --git a/readline/src/pad/prac1/Command.java b/readline/src/pad/prac1/Command.java new file mode 100644 index 0000000..14a2320 --- /dev/null +++ b/readline/src/pad/prac1/Command.java @@ -0,0 +1,37 @@ +package pad.prac1; + +public class Command +{ + /* + * COMMAND DEFINITIONS + */ + + public static final int INSERT_CHAR = 0; + public static final int DELETE_CHAR = 1; + public static final int MOVE_CURSOR = 2; + + private int type; + private int value; + + public Command(int t, int v) + { + type = t; + value = v; + } + + public Command(int t) + { + type = t; + } + + public int getType() + { + return type; + } + + public int getValue() + { + return value; + } + +} diff --git a/readline/src/pad/prac1/Console.java b/readline/src/pad/prac1/Console.java new file mode 100644 index 0000000..c677811 --- /dev/null +++ b/readline/src/pad/prac1/Console.java @@ -0,0 +1,68 @@ +package pad.prac1; + +import java.util.Observable; +import java.util.Observer; + +public class Console implements Observer +{ + public void moveCursorTo(int pos, int currentPos, int length) + { + 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); + } + } + + private void insertSpace() + { + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print((char)EditableBufferedReader.INS_SPACE); + } + + private void delChar() + { + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print((char)EditableBufferedReader.DEL_CHAR); + } + + public void addChar(char c, Line line) + { + if(line.getCursorPosition() != line.length()+1) + { + insertSpace(); + } + System.out.print(c); + } + + public void update(Observable obs, Object arg) + { + Line line = (Line)obs; + Command cmd = (Command)arg; + switch(cmd.getType()) + { + case Command.INSERT_CHAR: + addChar((char)cmd.getValue(),line); + break; + + case Command.DELETE_CHAR: + delChar(); + break; + + case Command.MOVE_CURSOR: + moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length()); + break; + } + } +} diff --git a/readline/src/pad/prac1/EditableBufferedReader.java b/readline/src/pad/prac1/EditableBufferedReader.java index aacdf63..71cf52d 100644 --- a/readline/src/pad/prac1/EditableBufferedReader.java +++ b/readline/src/pad/prac1/EditableBufferedReader.java @@ -22,6 +22,7 @@ public class EditableBufferedReader extends BufferedReader public static final int NON_PRINTABLE = 2; /* KEY DEFINITIONS + * * ESC (0x1B) starts an escape sequence * ESC+[+C (0x1B 0x5B 0x43) Cursor Forward * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward @@ -46,10 +47,12 @@ public class EditableBufferedReader extends BufferedReader public static final int INSERT_KEY = 0x32; public static final int INS_SPACE = 0x40; public static final int DEL_CHAR = 0x50; - public static final int NUM_0 = 0x30; + public static final int EOF = 0x04; + public static final int LINE_FEED = 0x0A; - private Line line = new Line(); - private boolean returnKey = false; + private Console console = new Console(); + private Line line = new Line(console); + private boolean endOfFile = false; public EditableBufferedReader(Reader in) { @@ -81,7 +84,7 @@ public class EditableBufferedReader extends BufferedReader { return ESCAPE_SEQUENCE; } - else if((key == BACKSPACE) || (key == RETURN_KEY)) + else if((key == BACKSPACE) || (key == RETURN_KEY) || (key == EOF)) { return PRINTABLE; } @@ -90,92 +93,6 @@ public class EditableBufferedReader extends BufferedReader return PRINTABLE; } - public void addChar(char c) - { - switch(line.getMode()) - { - case INSERT: - if(line.getCursorPosition() != line.length()+1) - { - insertSpace(); - } - System.out.print(c); - line.insertCharAt(c, line.getCursorPosition()-1); - line.setCursorPosition(line.getCursorPosition()+1); - break; - - case OVERWRITE: - if(line.getCursorPosition() != line.length()+1) - { - line.removeCharAt(line.getCursorPosition()-1); - } - System.out.print(c); - line.insertCharAt(c, line.getCursorPosition()-1); - line.setCursorPosition(line.getCursorPosition()+1); - break; - } - } - - public void delChar(int mode) - { - switch(mode) - { - case DEL_BACKWARD: - if(line.getCursorPosition() != 1) - { - System.out.print((char)BACKSPACE); - deleteCharacter(); - line.removeCharAt(line.getCursorPosition()-2); - line.setCursorPosition(line.getCursorPosition()-1); - } - break; - case DEL_FORWARD: - if(line.getCursorPosition() != (line.length()+1)) - { - line.removeCharAt(line.getCursorPosition()-1); - deleteCharacter(); - } - break; - } - } - - private void insertSpace() - { - System.out.print((char)ESC); - System.out.print((char)ESC_SEQ); - System.out.print((char)INS_SPACE); - } - - private void deleteCharacter() - { - System.out.print((char)ESC); - System.out.print((char)ESC_SEQ); - System.out.print((char)DEL_CHAR); - } - - public void moveCursorTo(int pos) - { - if((pos <= line.length()+1) && (pos >= 1)) - { - if(pos > line.getCursorPosition()) - { - System.out.print((char)ESC); - System.out.print((char)ESC_SEQ); - System.out.print(Integer.toString(pos-line.getCursorPosition())); - System.out.print((char)FORWARD); - } - - else if(pos < line.getCursorPosition()) - { - System.out.print((char)ESC); - System.out.print((char)ESC_SEQ); - System.out.print(Integer.toString(line.getCursorPosition()-pos)); - System.out.print((char)BACKWARD); - } - line.setCursorPosition(pos); - } - } - public int read() throws IOException { return super.read(); @@ -192,12 +109,12 @@ public class EditableBufferedReader extends BufferedReader System.out.println("Couldn't set terminal in raw mode"); return ""; } - while(!returnKey) + while(!endOfFile) { try { int character = read(); - //System.err.print(Integer.toHexString(character) + " \n"); + //System.err.print((String)(Integer.toHexString(character) + " ").toUpperCase()); switch(parseKey(character)) { case ESCAPE_SEQUENCE: @@ -207,17 +124,17 @@ public class EditableBufferedReader extends BufferedReader switch(read()) { case FORWARD: - moveCursorTo(line.getCursorPosition()+1); + line.setCursorTo(line.getCursorPosition()+1); break; case BACKWARD: - moveCursorTo(line.getCursorPosition()-1); + line.setCursorTo(line.getCursorPosition()-1); break; case DEL: if(read() == TILDE) { - delChar(DEL_FORWARD); + line.delChar(DEL_FORWARD); } break; @@ -234,11 +151,11 @@ public class EditableBufferedReader extends BufferedReader switch(read()) { case HOME: - moveCursorTo(1); + line.setCursorTo(1); break; case END: - moveCursorTo(line.length()+1); + line.setCursorTo(line.length()+1); break; } break; @@ -248,17 +165,19 @@ public class EditableBufferedReader extends BufferedReader case PRINTABLE: switch(character) { - case RETURN_KEY: - returnKey = true; + case EOF: + endOfFile = true; break; case BACKSPACE: case BACKSPACE_DEL: - delChar(DEL_BACKWARD); + line.delChar(DEL_BACKWARD); break; - + + case RETURN_KEY: + character = LINE_FEED; default: - addChar((char)character); + line.addChar((char)character); break; } break; @@ -287,3 +206,15 @@ public class EditableBufferedReader extends BufferedReader return line.toString(); } } + + +/* TO-DO + * + * up/down keys (needs text to work) + * pgup pgdown keys (goesto penultima linea visible) + * read terminal size + * update size dynamically + * long strings wrap down + * cursor keys skip final \n on a line + * careful if overwrite mode and final character is \n + */ diff --git a/readline/src/pad/prac1/Line.java b/readline/src/pad/prac1/Line.java index fd56a66..61ef5cd 100644 --- a/readline/src/pad/prac1/Line.java +++ b/readline/src/pad/prac1/Line.java @@ -1,23 +1,21 @@ package pad.prac1; -public class Line +import java.util.Observable; + +public class Line extends Observable { - /* - * MODE DEFINITIONS - */ - - public static final int INSERT = 0; - public static final int OVERWRITE = 1; - private String line; private int cursorPosition; private int writeMode; + private Console console; - public Line() + public Line(Console cons) { + console = cons; line = ""; - writeMode = INSERT; + writeMode = EditableBufferedReader.INSERT; cursorPosition = 1; + addObserver(console); } public int length() @@ -30,22 +28,44 @@ public class Line return cursorPosition; } - public void setCursorPosition(int pos) + public void toggleMode() { - cursorPosition = pos; + writeMode = 1 - writeMode; } - public int getMode() + private void sendCommand(int type, int value) { - return writeMode; + setChanged(); + notifyObservers(new Command(type,value)); } - public void toggleMode() + private void sendCommand(int type) { - writeMode = 1 - writeMode; + setChanged(); + notifyObservers(new Command(type)); + } + + private void insertCharAt(char c, int pos) + { + if(pos == 0) + { + String s = ""; + s += c; + line = s + line; + } + else if(pos == line.length()) + { + line += c; + } + else + { + String s = ""; + s += c; + line = line.substring(0, pos).concat(s).concat(line.substring(pos,line.length())); + } } - public void removeCharAt(int pos) + private void removeCharAt(int pos) { if(pos >= 0) { @@ -64,23 +84,61 @@ public class Line } } - public void insertCharAt(char c, int pos) + public void addChar(char c) { - if(pos == 0) + if(c == EditableBufferedReader.LINE_FEED) { - String s = ""; - s += c; - line = s + line; + sendCommand(Command.INSERT_CHAR,EditableBufferedReader.RETURN_KEY); } - else if(pos == line.length()) + switch(writeMode) { - line += c; + 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.INSERT_CHAR,(int)c); + insertCharAt(c, cursorPosition-1); + cursorPosition++; + break; } - else + } + + public void delChar(int mode) + { + switch(mode) { - String s = ""; - s += c; - line = line.substring(0, pos).concat(s).concat(line.substring(pos,line.length())); + case EditableBufferedReader.DEL_BACKWARD: + if(cursorPosition != 1) + { + sendCommand(Command.MOVE_CURSOR,cursorPosition-1); + sendCommand(Command.DELETE_CHAR); + removeCharAt(cursorPosition-2); + cursorPosition--; + } + break; + case EditableBufferedReader.DEL_FORWARD: + if(cursorPosition != (line.length()+1)) + { + removeCharAt(cursorPosition-1); + sendCommand(Command.DELETE_CHAR,1); + } + break; + } + } + + public void setCursorTo(int pos) + { + if((pos <= line.length()+1) && (pos >= 1) && (pos != cursorPosition)) + { + sendCommand(Command.MOVE_CURSOR,pos); + cursorPosition = pos; } } diff --git a/readline/src/pad/prac1/MainClass.java b/readline/src/pad/prac1/MainClass.java index fdc3692..09ec8d6 100644 --- a/readline/src/pad/prac1/MainClass.java +++ b/readline/src/pad/prac1/MainClass.java @@ -9,7 +9,7 @@ public class MainClass { InputStreamReader input = new InputStreamReader(System.in); EditableBufferedReader editable = new EditableBufferedReader(input); - System.out.println("Line read: " + editable.readLine()); + System.out.println("\nLine read: \n" + editable.readLine()); editable.close(); } -- libgit2 0.22.2