Commit a59076a2a42feb0ad4bb232bf8e507dc3e85cd17
1 parent
de88d7ee
git-svn-id: svn://imanolbarba.net/PAD@11 c2ee353e-ed0d-4329-bf56-03aec153487f
Showing
4 changed files
with
112 additions
and
106 deletions
readline/bin/pad/prac1/Console.class
No preview for this file type
readline/bin/pad/prac1/EditableBufferedReader.class
No preview for this file type
readline/src/pad/prac1/Console.java
... | ... | @@ -2,25 +2,41 @@ package pad.prac1; |
2 | 2 | |
3 | 3 | import java.util.Observable; |
4 | 4 | import java.util.Observer; |
5 | +import java.util.Scanner; | |
6 | +import java.util.regex.Pattern; | |
5 | 7 | |
6 | 8 | public class Console implements Observer |
7 | 9 | { |
8 | - public void moveCursorTo(int pos, int currentPos, int length) | |
10 | + private int currentCol; | |
11 | + private int currentRow; | |
12 | + | |
13 | + public void updateCurrentPos() | |
9 | 14 | { |
10 | - if(pos > currentPos) | |
11 | - { | |
12 | - System.out.print((char)EditableBufferedReader.ESC); | |
13 | - System.out.print((char)EditableBufferedReader.ESC_SEQ); | |
14 | - System.out.print(pos - currentPos); | |
15 | - System.out.print((char)EditableBufferedReader.FORWARD); | |
16 | - } | |
17 | - else if(pos < currentPos) | |
18 | - { | |
19 | - System.out.print((char)EditableBufferedReader.ESC); | |
20 | - System.out.print((char)EditableBufferedReader.ESC_SEQ); | |
21 | - System.out.print(currentPos-pos); | |
22 | - System.out.print((char)EditableBufferedReader.BACKWARD); | |
23 | - } | |
15 | + Coordinate pos = getCurrentPos(); | |
16 | + currentCol = pos.x(); | |
17 | + currentRow = pos.y(); | |
18 | + } | |
19 | + | |
20 | + public Coordinate getCurrentPos() | |
21 | + { | |
22 | + System.out.print((char)EditableBufferedReader.ESC); | |
23 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | |
24 | + System.out.print("6n"); | |
25 | + Pattern pattern = Pattern.compile("(\\d*);(\\d*)"); | |
26 | + Scanner scanner = new Scanner(System.in); | |
27 | + scanner.findWithinHorizon(pattern,0); | |
28 | + return new Coordinate(Integer.parseInt(scanner.match().group(2)), | |
29 | + Integer.parseInt(scanner.match().group(1))); | |
30 | + } | |
31 | + | |
32 | + public void moveCursorTo(Coordinate xy) | |
33 | + { | |
34 | + System.out.print((char)EditableBufferedReader.ESC); | |
35 | + System.out.print((char)EditableBufferedReader.ESC_SEQ); | |
36 | + System.out.print(xy.y()); | |
37 | + System.out.print(';'); | |
38 | + System.out.print(xy.x()); | |
39 | + System.out.print((char)EditableBufferedReader.GOTO); | |
24 | 40 | } |
25 | 41 | |
26 | 42 | private void insertSpace() |
... | ... | @@ -48,6 +64,7 @@ public class Console implements Observer |
48 | 64 | |
49 | 65 | public void update(Observable obs, Object arg) |
50 | 66 | { |
67 | + updateCurrentPos(); | |
51 | 68 | Line line = (Line)obs; |
52 | 69 | Command cmd = (Command)arg; |
53 | 70 | switch(cmd.getType()) |
... | ... | @@ -61,7 +78,9 @@ public class Console implements Observer |
61 | 78 | break; |
62 | 79 | |
63 | 80 | case Command.MOVE_CURSOR: |
64 | - moveCursorTo(cmd.getValue(),line.getCursorPosition(),line.length()); | |
81 | + Coordinate xy = new Coordinate(cmd.getValue(),currentRow); | |
82 | + //Gestionar si excede el numero de columnas con modulos y asi | |
83 | + moveCursorTo(xy); | |
65 | 84 | break; |
66 | 85 | } |
67 | 86 | } | ... | ... |
readline/src/pad/prac1/EditableBufferedReader.java
1 | 1 | package pad.prac1; |
2 | 2 | import java.io.*; |
3 | +import java.util.Scanner; | |
4 | +import java.util.regex.Pattern; | |
3 | 5 | |
4 | 6 | |
5 | 7 | public class EditableBufferedReader extends BufferedReader |
... | ... | @@ -39,6 +41,7 @@ public class EditableBufferedReader extends BufferedReader |
39 | 41 | public static final int ESC_SEQ = 0x5B; |
40 | 42 | public static final int FORWARD = 0x43; |
41 | 43 | public static final int BACKWARD = 0x44; |
44 | + public static final int GOTO = 0x48; | |
42 | 45 | public static final int DEL = 0x33; |
43 | 46 | public static final int TILDE = 0x7E; |
44 | 47 | public static final int HOME_END = 0x4F; |
... | ... | @@ -76,21 +79,77 @@ public class EditableBufferedReader extends BufferedReader |
76 | 79 | Runtime.getRuntime().exec(cmd).waitFor(); |
77 | 80 | } |
78 | 81 | |
79 | - private int parseKey(int key) | |
82 | + private void parseKey() throws IOException | |
80 | 83 | { |
81 | - if(key < 0x20) | |
84 | + Pattern pattern; | |
85 | + Scanner scanner = new Scanner(System.in); | |
86 | + pattern = Pattern.compile("(.{1})"); | |
87 | + if(scanner.findWithinHorizon(pattern,0) != null) | |
82 | 88 | { |
83 | - if(key == ESC) | |
89 | + int character = (int)scanner.match().group(1).charAt(0); | |
90 | + if(character >= 0x20 || character == EOF || character == BACKSPACE || character == RETURN_KEY) | |
84 | 91 | { |
85 | - return ESCAPE_SEQUENCE; | |
86 | - } | |
87 | - else if((key == BACKSPACE) || (key == RETURN_KEY) || (key == EOF)) | |
88 | - { | |
89 | - return PRINTABLE; | |
92 | + switch(character) | |
93 | + { | |
94 | + case EOF: | |
95 | + endOfFile = true; | |
96 | + break; | |
97 | + | |
98 | + case BACKSPACE: | |
99 | + case BACKSPACE_DEL: | |
100 | + line.delChar(DEL_BACKWARD); | |
101 | + break; | |
102 | + | |
103 | + case RETURN_KEY: | |
104 | + character = LINE_FEED; | |
105 | + default: | |
106 | + line.addChar((char)character); | |
107 | + break; | |
108 | + } | |
109 | + return; | |
90 | 110 | } |
91 | - return NON_PRINTABLE; | |
92 | 111 | } |
93 | - return PRINTABLE; | |
112 | + pattern = Pattern.compile("\\^\\[\\[(.{1}).*"); | |
113 | + if(scanner.findWithinHorizon(pattern,3) != null) //escape sequence | |
114 | + { | |
115 | + int character = scanner.match().group(1).charAt(0); | |
116 | + switch(character) | |
117 | + { | |
118 | + case FORWARD: | |
119 | + line.setCursorTo(line.getCursorPosition()+1); | |
120 | + break; | |
121 | + | |
122 | + case BACKWARD: | |
123 | + line.setCursorTo(line.getCursorPosition()-1); | |
124 | + break; | |
125 | + | |
126 | + case DEL: | |
127 | + line.delChar(DEL_FORWARD); | |
128 | + break; | |
129 | + | |
130 | + case INSERT_KEY: | |
131 | + line.toggleMode(); | |
132 | + break; | |
133 | + } | |
134 | + return; | |
135 | + } | |
136 | + pattern = Pattern.compile("\\^\\[O(\\w{1})"); | |
137 | + if(scanner.findWithinHorizon(pattern,3) != null) //HOME or END | |
138 | + { | |
139 | + | |
140 | + int character = (int)scanner.match().group(1).charAt(0); | |
141 | + switch(character) | |
142 | + { | |
143 | + case HOME: | |
144 | + line.setCursorTo(1); | |
145 | + break; | |
146 | + | |
147 | + case END: | |
148 | + line.setCursorTo(line.length()+1); | |
149 | + break; | |
150 | + } | |
151 | + return; | |
152 | + } | |
94 | 153 | } |
95 | 154 | |
96 | 155 | public int read() throws IOException |
... | ... | @@ -109,90 +168,18 @@ public class EditableBufferedReader extends BufferedReader |
109 | 168 | System.out.println("Couldn't set terminal in raw mode"); |
110 | 169 | return ""; |
111 | 170 | } |
112 | - while(!endOfFile) | |
171 | + try | |
113 | 172 | { |
114 | - try | |
173 | + while(!endOfFile) | |
115 | 174 | { |
116 | - int character = read(); | |
117 | - //System.err.print((String)(Integer.toHexString(character) + " ").toUpperCase()); | |
118 | - switch(parseKey(character)) | |
119 | - { | |
120 | - case ESCAPE_SEQUENCE: | |
121 | - switch(read()) | |
122 | - { | |
123 | - case ESC_SEQ: | |
124 | - switch(read()) | |
125 | - { | |
126 | - case FORWARD: | |
127 | - line.setCursorTo(line.getCursorPosition()+1); | |
128 | - break; | |
129 | - | |
130 | - case BACKWARD: | |
131 | - line.setCursorTo(line.getCursorPosition()-1); | |
132 | - break; | |
133 | - | |
134 | - case DEL: | |
135 | - if(read() == TILDE) | |
136 | - { | |
137 | - line.delChar(DEL_FORWARD); | |
138 | - } | |
139 | - break; | |
140 | - | |
141 | - case INSERT_KEY: | |
142 | - if(read() == TILDE) | |
143 | - { | |
144 | - line.toggleMode(); | |
145 | - } | |
146 | - break; | |
147 | - } | |
148 | - break; | |
149 | - | |
150 | - case HOME_END: | |
151 | - switch(read()) | |
152 | - { | |
153 | - case HOME: | |
154 | - line.setCursorTo(1); | |
155 | - break; | |
156 | - | |
157 | - case END: | |
158 | - line.setCursorTo(line.length()+1); | |
159 | - break; | |
160 | - } | |
161 | - break; | |
162 | - } | |
163 | - break; | |
164 | - | |
165 | - case PRINTABLE: | |
166 | - switch(character) | |
167 | - { | |
168 | - case EOF: | |
169 | - endOfFile = true; | |
170 | - break; | |
171 | - | |
172 | - case BACKSPACE: | |
173 | - case BACKSPACE_DEL: | |
174 | - line.delChar(DEL_BACKWARD); | |
175 | - break; | |
176 | - | |
177 | - case RETURN_KEY: | |
178 | - character = LINE_FEED; | |
179 | - default: | |
180 | - line.addChar((char)character); | |
181 | - break; | |
182 | - } | |
183 | - break; | |
184 | - | |
185 | - case NON_PRINTABLE: | |
186 | - //ignore | |
187 | - break; | |
188 | - } | |
189 | - } | |
190 | - catch (IOException e) | |
191 | - { | |
192 | - System.out.println("Error reading line"); | |
193 | - break; | |
175 | + parseKey(); | |
194 | 176 | } |
195 | 177 | } |
178 | + catch(Exception e) | |
179 | + { | |
180 | + System.out.println("Error reading line"); | |
181 | + return ""; | |
182 | + } | |
196 | 183 | try |
197 | 184 | { |
198 | 185 | unsetRaw(); | ... | ... |