Commit 8727f445469d84aa59e8618a12b3088ded4e45dc
1 parent
2d72aa95
Final Commit, reverted multi-line, bugs fixed
git-svn-id: svn://imanolbarba.net/PAD@17 c2ee353e-ed0d-4329-bf56-03aec153487f
Showing
6 changed files
with
120 additions
and
92 deletions
readline/src/pad/prac1/Command.java
@@ -9,7 +9,6 @@ public class Command | @@ -9,7 +9,6 @@ public class Command | ||
9 | public static final int INSERT_CHAR = 0; | 9 | public static final int INSERT_CHAR = 0; |
10 | public static final int DELETE_CHAR = 1; | 10 | public static final int DELETE_CHAR = 1; |
11 | public static final int MOVE_CURSOR = 2; | 11 | public static final int MOVE_CURSOR = 2; |
12 | - public static final int NEWLINE = 3; | ||
13 | 12 | ||
14 | private int type; | 13 | private int type; |
15 | private int value; | 14 | private int value; |
readline/src/pad/prac1/Console.java
@@ -7,53 +7,104 @@ import java.util.regex.Pattern; | @@ -7,53 +7,104 @@ import java.util.regex.Pattern; | ||
7 | 7 | ||
8 | public class Console implements Observer | 8 | public class Console implements Observer |
9 | { | 9 | { |
10 | + private Coordinate currentXY; | ||
11 | + //private int numRows; | ||
10 | private int numCols; | 12 | private int numCols; |
11 | - private int numRows; | ||
12 | - private int currentRow; | ||
13 | 13 | ||
14 | - public void updateCurrentPos() | 14 | + public Console() |
15 | { | 15 | { |
16 | - Coordinate pos = getCurrentPos(); | ||
17 | - currentRow = pos.y(); | 16 | + currentXY = new Coordinate(0,0); |
18 | } | 17 | } |
19 | 18 | ||
20 | - public void updateTermSize() | 19 | + public void moveCursorTo(int pos, int currentPos, int length) |
21 | { | 20 | { |
22 | - // If we could just handle SIGWINCH, this would not be necessary and it would run A LOT faster | ||
23 | - System.out.print((char)EditableBufferedReader.ESC); | ||
24 | - System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
25 | - System.out.print("18t"); | ||
26 | - Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)"); | ||
27 | - Scanner scanner = new Scanner(System.in); | ||
28 | - scanner.findWithinHorizon(pattern,0); | ||
29 | - numCols = Integer.parseInt(scanner.match().group(2)); | ||
30 | - numRows = Integer.parseInt(scanner.match().group(1)); | ||
31 | - System.err.println("TERMSIZE:" + numCols + " " + numRows); | 21 | + updateTermSize(); |
22 | + updateCurrentPos(); | ||
23 | + if(pos > currentPos) | ||
24 | + { | ||
25 | + while((pos-currentPos) >= numCols) | ||
26 | + { | ||
27 | + currentPos += numCols - currentXY.x()+1; | ||
28 | + System.out.print((char)EditableBufferedReader.ESC); | ||
29 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
30 | + System.out.print((char)EditableBufferedReader.DOWN); | ||
31 | + currentXY.setY(currentXY.y()+1); | ||
32 | + System.out.print((char)EditableBufferedReader.ESC); | ||
33 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
34 | + System.out.print(numCols-1); | ||
35 | + System.out.print((char)EditableBufferedReader.BACKWARD); | ||
36 | + currentXY.setX(1); | ||
37 | + } | ||
38 | + if(pos > currentPos) | ||
39 | + { | ||
40 | + System.out.print((char)EditableBufferedReader.ESC); | ||
41 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
42 | + System.out.print(pos - currentPos); | ||
43 | + System.out.print((char)EditableBufferedReader.FORWARD); | ||
44 | + } | ||
45 | + } | ||
46 | + else if(pos < currentPos) | ||
47 | + { | ||
48 | + System.err.println(currentPos); | ||
49 | + System.err.println(pos); | ||
50 | + System.err.println(currentXY); | ||
51 | + while((currentPos-pos) >= currentXY.x()) | ||
52 | + { | ||
53 | + currentPos -= currentXY.x(); | ||
54 | + System.out.print((char)EditableBufferedReader.ESC); | ||
55 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
56 | + System.out.print((char)EditableBufferedReader.UP); | ||
57 | + currentXY.setY(currentXY.y()-1); | ||
58 | + System.out.print((char)EditableBufferedReader.ESC); | ||
59 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
60 | + System.out.print(numCols); | ||
61 | + System.out.print((char)EditableBufferedReader.FORWARD); | ||
62 | + currentXY.setX(numCols); | ||
63 | + } | ||
64 | + if(pos < currentPos) | ||
65 | + { | ||
66 | + System.out.print((char)EditableBufferedReader.ESC); | ||
67 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
68 | + System.out.print(currentPos-pos); | ||
69 | + System.out.print((char)EditableBufferedReader.BACKWARD); | ||
70 | + } | ||
71 | + } | ||
32 | } | 72 | } |
33 | 73 | ||
34 | - public Coordinate getCurrentPos() | 74 | + public void updateCurrentPos() |
75 | + { | ||
76 | + Coordinate pos = getCurrentPos(); | ||
77 | + currentXY.setY(pos.y()); | ||
78 | + currentXY.setX(pos.x()); | ||
79 | + } | ||
80 | + | ||
81 | + public void updateTermSize() | ||
35 | { | 82 | { |
36 | - System.out.print((char)EditableBufferedReader.ESC); | ||
37 | - System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
38 | - System.out.print("6n"); | ||
39 | - Pattern pattern = Pattern.compile("(\\d+);(\\d+)"); | ||
40 | - Scanner scanner = new Scanner(System.in); | ||
41 | - scanner.findWithinHorizon(pattern,0); | ||
42 | - int x = Integer.parseInt(scanner.match().group(2)); | ||
43 | - int y = Integer.parseInt(scanner.match().group(1)); | ||
44 | - System.err.println("POS:" + x + " " + y); | ||
45 | - return new Coordinate(x,y); | 83 | + System.out.print((char)EditableBufferedReader.ESC); |
84 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
85 | + System.out.print("18t"); | ||
86 | + Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)"); | ||
87 | + Scanner scanner = new Scanner(System.in); | ||
88 | + scanner.findWithinHorizon(pattern,0); | ||
89 | + numCols = Integer.parseInt(scanner.match().group(2)); | ||
90 | + //numRows = Integer.parseInt(scanner.match().group(1)); | ||
46 | } | 91 | } |
47 | 92 | ||
48 | - public void moveCursorTo(Coordinate xy) | 93 | + public Coordinate getCurrentPos() |
49 | { | 94 | { |
50 | - System.out.print((char)EditableBufferedReader.ESC); | ||
51 | - System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
52 | - System.out.print(xy.y()); | ||
53 | - System.out.print(';'); | ||
54 | - System.out.print(xy.x()); | ||
55 | - System.out.print((char)EditableBufferedReader.GOTO); | 95 | + System.out.print((char)EditableBufferedReader.ESC); |
96 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | ||
97 | + System.out.print("6n"); | ||
98 | + Pattern pattern = Pattern.compile("(\\d+);(\\d+)"); | ||
99 | + Scanner scanner = new Scanner(System.in); | ||
100 | + scanner.findWithinHorizon(pattern,0); | ||
101 | + int x = Integer.parseInt(scanner.match().group(2)); | ||
102 | + int y = Integer.parseInt(scanner.match().group(1)); | ||
103 | + return new Coordinate(x,y); | ||
56 | } | 104 | } |
105 | + | ||
106 | + | ||
107 | + | ||
57 | 108 | ||
58 | private void insertSpace() | 109 | private void insertSpace() |
59 | { | 110 | { |
@@ -80,8 +131,6 @@ public class Console implements Observer | @@ -80,8 +131,6 @@ public class Console implements Observer | ||
80 | 131 | ||
81 | public void update(Observable obs, Object arg) | 132 | public void update(Observable obs, Object arg) |
82 | { | 133 | { |
83 | - updateCurrentPos(); | ||
84 | - updateTermSize(); | ||
85 | Line line = (Line)obs; | 134 | Line line = (Line)obs; |
86 | Command cmd = (Command)arg; | 135 | Command cmd = (Command)arg; |
87 | switch(cmd.getType()) | 136 | switch(cmd.getType()) |
@@ -95,19 +144,8 @@ public class Console implements Observer | @@ -95,19 +144,8 @@ public class Console implements Observer | ||
95 | break; | 144 | break; |
96 | 145 | ||
97 | case Command.MOVE_CURSOR: | 146 | case Command.MOVE_CURSOR: |
98 | - currentRow += cmd.getValue()/numCols; | ||
99 | - if(currentRow > numRows) | ||
100 | - { | ||
101 | - currentRow = numRows; | ||
102 | - } | ||
103 | - Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow); | ||
104 | - moveCursorTo(xy); | 147 | + moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length()); |
105 | break; | 148 | break; |
106 | - | ||
107 | - case Command.NEWLINE: | ||
108 | - System.out.print((char)EditableBufferedReader.LINE_FEED); | ||
109 | - System.out.print((char)EditableBufferedReader.RETURN_KEY); | ||
110 | - currentRow++; | ||
111 | } | 149 | } |
112 | } | 150 | } |
113 | } | 151 | } |
readline/src/pad/prac1/Coordinate.java
readline/src/pad/prac1/EditableBufferedReader.java
@@ -26,9 +26,9 @@ public class EditableBufferedReader extends BufferedReader | @@ -26,9 +26,9 @@ public class EditableBufferedReader extends BufferedReader | ||
26 | 26 | ||
27 | /* KEY DEFINITIONS | 27 | /* KEY DEFINITIONS |
28 | * | 28 | * |
29 | - * ESC (0x1B) starts an escape sequence | 29 | + * ESC (0x1B) Starts an escape sequence |
30 | * ESC+[+C (0x1B 0x5B 0x43) Cursor Forward | 30 | * ESC+[+C (0x1B 0x5B 0x43) Cursor Forward |
31 | - * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward | 31 | + * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward |
32 | * ESC+[+2+~ (0x1B 0x5B 0x32 0x7E) Insert | 32 | * ESC+[+2+~ (0x1B 0x5B 0x32 0x7E) Insert |
33 | * ESC+[+3+~ (0x1B 0x5B 0x33 0x7E) Delete forward | 33 | * ESC+[+3+~ (0x1B 0x5B 0x33 0x7E) Delete forward |
34 | * ESC+O+H (0x1B 0x4F 0x48) Home | 34 | * ESC+O+H (0x1B 0x4F 0x48) Home |
@@ -40,6 +40,8 @@ public class EditableBufferedReader extends BufferedReader | @@ -40,6 +40,8 @@ public class EditableBufferedReader extends BufferedReader | ||
40 | public static final int BACKSPACE_DEL = 0x7F; | 40 | public static final int BACKSPACE_DEL = 0x7F; |
41 | public static final int ESC = 0x1B; | 41 | public static final int ESC = 0x1B; |
42 | public static final int ESC_SEQ = 0x5B; | 42 | public static final int ESC_SEQ = 0x5B; |
43 | + public static final int UP = 0x41; | ||
44 | + public static final int DOWN = 0x42; | ||
43 | public static final int FORWARD = 0x43; | 45 | public static final int FORWARD = 0x43; |
44 | public static final int BACKWARD = 0x44; | 46 | public static final int BACKWARD = 0x44; |
45 | public static final int GOTO = 0x48; | 47 | public static final int GOTO = 0x48; |
@@ -54,9 +56,9 @@ public class EditableBufferedReader extends BufferedReader | @@ -54,9 +56,9 @@ public class EditableBufferedReader extends BufferedReader | ||
54 | public static final int EOF = 0x04; | 56 | public static final int EOF = 0x04; |
55 | public static final int LINE_FEED = 0x0A; | 57 | public static final int LINE_FEED = 0x0A; |
56 | 58 | ||
57 | - private Console console = new Console(); | ||
58 | - private Line line = new Line(console); | ||
59 | - private boolean endOfFile = false; | 59 | + private Console console = new Console(); |
60 | + private Line line = new Line(console); | ||
61 | + private boolean endOfFile = false; | ||
60 | 62 | ||
61 | public EditableBufferedReader(Reader in) | 63 | public EditableBufferedReader(Reader in) |
62 | { | 64 | { |
@@ -132,17 +134,15 @@ public class EditableBufferedReader extends BufferedReader | @@ -132,17 +134,15 @@ public class EditableBufferedReader extends BufferedReader | ||
132 | int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); | 134 | int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); |
133 | switch(character) | 135 | switch(character) |
134 | { | 136 | { |
135 | - case EOF: | ||
136 | - endOfFile = true; | ||
137 | - break; | ||
138 | - | ||
139 | case BACKSPACE: | 137 | case BACKSPACE: |
140 | case BACKSPACE_DEL: | 138 | case BACKSPACE_DEL: |
141 | line.delChar(DEL_BACKWARD); | 139 | line.delChar(DEL_BACKWARD); |
142 | break; | 140 | break; |
143 | 141 | ||
142 | + case EOF: | ||
144 | case RETURN_KEY: | 143 | case RETURN_KEY: |
145 | - character = LINE_FEED; | 144 | + endOfFile = true; |
145 | + break; | ||
146 | default: | 146 | default: |
147 | line.addChar((char)character); | 147 | line.addChar((char)character); |
148 | break; | 148 | break; |
@@ -192,4 +192,4 @@ public class EditableBufferedReader extends BufferedReader | @@ -192,4 +192,4 @@ public class EditableBufferedReader extends BufferedReader | ||
192 | System.out.println(""); | 192 | System.out.println(""); |
193 | return line.toString(); | 193 | return line.toString(); |
194 | } | 194 | } |
195 | -} | ||
196 | \ No newline at end of file | 195 | \ No newline at end of file |
196 | +} |
readline/src/pad/prac1/Line.java
@@ -88,31 +88,26 @@ public class Line extends Observable | @@ -88,31 +88,26 @@ public class Line extends Observable | ||
88 | { | 88 | { |
89 | if(c == EditableBufferedReader.LINE_FEED) | 89 | if(c == EditableBufferedReader.LINE_FEED) |
90 | { | 90 | { |
91 | - sendCommand(Command.NEWLINE); | ||
92 | - insertCharAt(c, cursorPosition-1); | ||
93 | - cursorPosition++; | 91 | + sendCommand(Command.INSERT_CHAR,EditableBufferedReader.RETURN_KEY); |
94 | } | 92 | } |
95 | - else | 93 | + switch(writeMode) |
96 | { | 94 | { |
97 | - switch(writeMode) | ||
98 | - { | ||
99 | - case EditableBufferedReader.INSERT: | ||
100 | - sendCommand(Command.INSERT_CHAR,(int)c); | ||
101 | - insertCharAt(c, cursorPosition-1); | ||
102 | - cursorPosition++; | ||
103 | - break; | ||
104 | - | ||
105 | - case EditableBufferedReader.OVERWRITE: | ||
106 | - if(cursorPosition != line.length()+1) | ||
107 | - { | ||
108 | - removeCharAt(cursorPosition-1); | ||
109 | - sendCommand(Command.DELETE_CHAR); | ||
110 | - } | ||
111 | - sendCommand(Command.INSERT_CHAR,(int)c); | ||
112 | - insertCharAt(c, cursorPosition-1); | ||
113 | - cursorPosition++; | 95 | + case EditableBufferedReader.INSERT: |
96 | + sendCommand(Command.INSERT_CHAR,(int)c); | ||
97 | + insertCharAt(c, cursorPosition-1); | ||
98 | + cursorPosition++; | ||
114 | break; | 99 | break; |
115 | - } | 100 | + |
101 | + case EditableBufferedReader.OVERWRITE: | ||
102 | + if(cursorPosition != line.length()+1) | ||
103 | + { | ||
104 | + removeCharAt(cursorPosition-1); | ||
105 | + sendCommand(Command.DELETE_CHAR); | ||
106 | + } | ||
107 | + sendCommand(Command.INSERT_CHAR,(int)c); | ||
108 | + insertCharAt(c, cursorPosition-1); | ||
109 | + cursorPosition++; | ||
110 | + break; | ||
116 | } | 111 | } |
117 | } | 112 | } |
118 | 113 |
readline/src/pad/prac1/MainClass.java
@@ -12,14 +12,5 @@ public class MainClass | @@ -12,14 +12,5 @@ public class MainClass | ||
12 | System.out.println("\nLine read: \n" + editable.readLine()); | 12 | System.out.println("\nLine read: \n" + editable.readLine()); |
13 | editable.close(); | 13 | editable.close(); |
14 | } | 14 | } |
15 | -} | ||
16 | 15 | ||
17 | -/* TO-DO | ||
18 | - * up/down keys | ||
19 | - * pgup pgdown keys (goesto penultima linea visible) | ||
20 | - * implement change of terminal dimensions | ||
21 | - * | ||
22 | - * TO-FIX | ||
23 | - * Cursor keys, Home and end keys when new line, reset line length | ||
24 | - * SPEED | ||
25 | - */ | ||
26 | \ No newline at end of file | 16 | \ No newline at end of file |
17 | +} |