From 25fd54b02f38247159c81fae679becf77e977f10 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Wed, 18 Sep 2013 02:22:57 +0000 Subject: [PATCH] git-svn-id: svn://imanolbarba.net/PAD@5 c2ee353e-ed0d-4329-bf56-03aec153487f --- readline/bin/pad/prac1/EditableBufferedReader.class | Bin 4255 -> 0 bytes readline/src/pad/prac1/EditableBufferedReader.java | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------- 2 files changed, 129 insertions(+), 101 deletions(-) diff --git a/readline/bin/pad/prac1/EditableBufferedReader.class b/readline/bin/pad/prac1/EditableBufferedReader.class index 6d98d11..23839e5 100644 Binary files a/readline/bin/pad/prac1/EditableBufferedReader.class and b/readline/bin/pad/prac1/EditableBufferedReader.class differ diff --git a/readline/src/pad/prac1/EditableBufferedReader.java b/readline/src/pad/prac1/EditableBufferedReader.java index 0c957d6..c77c194 100644 --- a/readline/src/pad/prac1/EditableBufferedReader.java +++ b/readline/src/pad/prac1/EditableBufferedReader.java @@ -8,18 +8,18 @@ public class EditableBufferedReader extends BufferedReader * MODE DEFINITIONS */ - public static final int INSERT = 0; - public static final int OVERWRITE = 1; - public static final int DEL_BACKWARD = 0; - public static final int DEL_FORWARD = 1; + public static final int DEL_BACKWARD = 0; + public static final int DEL_FORWARD = 1; + public static final int INSERT = 0; + public static final int OVERWRITE = 1; /* * TYPE DEFINITIONS */ public static final int ESCAPE_SEQUENCE = 0; - public static final int PRINTABLE = 1; - public static final int NON_PRINTABLE = 2; + public static final int PRINTABLE = 1; + public static final int NON_PRINTABLE = 2; /* KEY DEFINITIONS * ESC (0x1B) starts an escape sequence @@ -33,25 +33,24 @@ public class EditableBufferedReader extends BufferedReader public static final int RETURN_KEY = 0x0D; public static final int BACKSPACE = 0x08; - public static final int BACKSPACE_DEL = 0x7B; + public static final int BACKSPACE_DEL = 0x7F; public static final int ESC = 0x1B; public static final int ESC_SEQ = 0x5B; - public static final int FORWARD_C = 0x43; - public static final int BACKWARD_D = 0x44; - public static final int DEL_3 = 0x33; + public static final int FORWARD = 0x43; + public static final int BACKWARD = 0x44; + public static final int DEL = 0x33; public static final int TILDE = 0x7E; public static final int HOME_END = 0x4F; public static final int HOME = 0x48; public static final int END = 0x46; - public static final int INSERT_2 = 0x32; - - private String line = ""; - private int lineLength = 0; - private int cursorPosition = 1; - private boolean returnKey = false; - private int writeMode = INSERT; - + 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; + private Line line = new Line(); + private boolean returnKey = false; + public EditableBufferedReader(Reader in) { super(in); @@ -76,76 +75,104 @@ public class EditableBufferedReader extends BufferedReader private int parseKey(int key) { - //TO IMPLEMENT BOTH MODES + if(key < 0x20) + { + if(key == ESC) + { + return ESCAPE_SEQUENCE; + } + else if((key == BACKSPACE) || (key == RETURN_KEY)) + { + return PRINTABLE; + } + return NON_PRINTABLE; + } return PRINTABLE; } - public void addChar(char character) + public void addChar(char c) { - //switch writemode - //if insert - //print space - //moveto cursorPosition-1 - //print char - //add to string - //update string length - //update cursor position - //if overwrite - //just print char - //add to string - //update string length - //update cursor position + 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 - //if back - //del char using backspace - //remove char from line string - //update cursor position - //update line length - //if forward - //moveto cursorPosition+1 - //del char using backspace - //remove char from line string - //update line length + 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 moveCursorForward() + private void insertSpace() { System.out.print((char)ESC); System.out.print((char)ESC_SEQ); - System.out.print((char)FORWARD_C); + System.out.print((char)INS_SPACE); } - private void moveCursorBackward() + private void deleteCharacter() { System.out.print((char)ESC); System.out.print((char)ESC_SEQ); - System.out.print((char)BACKWARD_D); + System.out.print((char)DEL_CHAR); } public void moveCursorTo(int pos) { - if((pos <= lineLength) && (pos >= 1)) + if((pos <= line.length()+1) && (pos >= 1)) { - if(pos > cursorPosition) + if(pos > line.getCursorPosition()) { - for(int i = 0; i < (pos - cursorPosition); i++) - { - moveCursorForward(); - } + System.out.print((char)ESC); + System.out.print((char)ESC_SEQ); + System.out.print((char)(NUM_0+pos-line.getCursorPosition())); + System.out.print((char)FORWARD); } - else if(pos < cursorPosition) + else if(pos < line.getCursorPosition()) { - for(int i = 0; i < (cursorPosition - pos); i++) - { - moveCursorBackward(); - } + System.out.print((char)ESC); + System.out.print((char)ESC_SEQ); + System.out.print((char)(NUM_0-pos+line.getCursorPosition())); + System.out.print((char)BACKWARD); } - cursorPosition = pos; + line.setCursorPosition(pos); } } @@ -170,79 +197,80 @@ public class EditableBufferedReader extends BufferedReader try { int character = read(); - System.out.println("\n\n" + character + "\n"); switch(parseKey(character)) { case ESCAPE_SEQUENCE: - if(read() == ESC_SEQ) + switch(read()) { - switch(read()) - { - case FORWARD_C: - moveCursorTo(cursorPosition+1); + case ESC_SEQ: + switch(read()) + { + case FORWARD: + moveCursorTo(line.getCursorPosition()+1); + break; + + case BACKWARD: + moveCursorTo(line.getCursorPosition()-1); break; - case BACKWARD_D: - moveCursorTo(cursorPosition-1); + case DEL: + if(read() == TILDE) + { + delChar(DEL_FORWARD); + } break; - - case DEL_3: - if(read() == TILDE) - { - delChar(DEL_FORWARD); - } + + case INSERT_KEY: + if(read() == TILDE) + { + line.toggleMode(); + } break; - - case INSERT_2: - if(read() == TILDE) - { - writeMode = 1-writeMode; - } + } + break; + + case HOME_END: + switch(read()) + { + case HOME: + moveCursorTo(1); break; - - case HOME_END: - switch(read()) - { - case HOME: - moveCursorTo(1); - break; - - case END: - moveCursorTo(lineLength+1); - break; - } + + case END: + moveCursorTo(line.length()+1); break; - } + } + break; } - break; + break; case PRINTABLE: switch(character) { case RETURN_KEY: returnKey = true; - break; + break; case BACKSPACE: case BACKSPACE_DEL: delChar(DEL_BACKWARD); - break; + break; default: addChar((char)character); - break; + break; } - break; + break; case NON_PRINTABLE: //ignore - break; + break; } } catch (IOException e) { - System.out.println("Couldn't unset raw mode"); + System.out.println("Error reading line"); break; } } @@ -256,6 +284,6 @@ public class EditableBufferedReader extends BufferedReader return ""; } System.out.println(""); - return line; + return line.toString(); } } -- libgit2 0.22.2