Commit 6587c670ccd950fc835140c4413ebb3036c2d26e
1 parent
78ef8567
git-svn-id: svn://imanolbarba.net/PAD@3 c2ee353e-ed0d-4329-bf56-03aec153487f
Showing
2 changed files
with
184 additions
and
6 deletions
readline/bin/pad/prac1/EditableBufferedReader.class
No preview for this file type
readline/src/pad/prac1/EditableBufferedReader.java
@@ -4,8 +4,53 @@ import java.io.*; | @@ -4,8 +4,53 @@ import java.io.*; | ||
4 | 4 | ||
5 | public class EditableBufferedReader extends BufferedReader | 5 | public class EditableBufferedReader extends BufferedReader |
6 | { | 6 | { |
7 | + /* | ||
8 | + * MODE DEFINITIONS | ||
9 | + */ | ||
10 | + | ||
11 | + public static final int INSERT = 0; | ||
12 | + public static final int OVERWRITE = 1; | ||
13 | + public static final int DEL_BACKWARD = 0; | ||
14 | + public static final int DEL_FORWARD = 1; | ||
15 | + | ||
16 | + /* | ||
17 | + * TYPE DEFINITIONS | ||
18 | + */ | ||
19 | + | ||
20 | + public static final int ESCAPE_SEQUENCE = 0; | ||
21 | + public static final int PRINTABLE = 1; | ||
22 | + public static final int NON_PRINTABLE = 2; | ||
23 | + | ||
24 | + /* KEY DEFINITIONS | ||
25 | + * ESC (0x1B) starts an escape sequence | ||
26 | + * ESC+[+C (0x1B 0x5B 0x43) Cursor Forward | ||
27 | + * ESC+[+D (0x1B 0x5B 0x44) Cursor Backward | ||
28 | + * ESC+[+2+~ (0x1B 0x5B 0x32 0x7E) Insert | ||
29 | + * ESC+[+3+~ (0x1B 0x5B 0x33 0x7E) Delete forward | ||
30 | + * ESC+O+H (0x1B 0x4F 0x48) Home | ||
31 | + * ESC+O+F (0x1B 0x4F 0x46) End | ||
32 | + */ | ||
33 | + | ||
34 | + public static final int RETURN_KEY = 0x0D; | ||
35 | + public static final int BACKSPACE = 0x08; | ||
36 | + public static final int BACKSPACE_DEL = 0x7B; | ||
37 | + public static final int ESC = 0x1B; | ||
38 | + public static final int ESC_SEQ = 0x5B; | ||
39 | + public static final int FORWARD_C = 0x43; | ||
40 | + public static final int BACKWARD_D = 0x44; | ||
41 | + public static final int DEL_3 = 0x33; | ||
42 | + public static final int TILDE = 0x7E; | ||
43 | + public static final int HOME_END = 0x4F; | ||
44 | + public static final int HOME = 0x48; | ||
45 | + public static final int END = 0x46; | ||
46 | + public static final int INSERT_2 = 0x32; | ||
47 | + | ||
7 | private String line = ""; | 48 | private String line = ""; |
49 | + private int lineLength = 0; | ||
50 | + private int cursorPosition = 1; | ||
8 | private boolean returnKey = false; | 51 | private boolean returnKey = false; |
52 | + private int writeMode = INSERT; | ||
53 | + | ||
9 | 54 | ||
10 | public EditableBufferedReader(Reader in) | 55 | public EditableBufferedReader(Reader in) |
11 | { | 56 | { |
@@ -29,6 +74,80 @@ public class EditableBufferedReader extends BufferedReader | @@ -29,6 +74,80 @@ public class EditableBufferedReader extends BufferedReader | ||
29 | Runtime.getRuntime().exec(cmd).waitFor(); | 74 | Runtime.getRuntime().exec(cmd).waitFor(); |
30 | } | 75 | } |
31 | 76 | ||
77 | + private int parseKey(int key) | ||
78 | + { | ||
79 | + return PRINTABLE; | ||
80 | + } | ||
81 | + | ||
82 | + public void addChar(char character) | ||
83 | + { | ||
84 | + //switch writemode | ||
85 | + //if insert | ||
86 | + //print space | ||
87 | + //moveto cursorPosition-1 | ||
88 | + //print char | ||
89 | + //add to string | ||
90 | + //update string length | ||
91 | + //update cursor position | ||
92 | + //if overwrite | ||
93 | + //just print char | ||
94 | + //add to string | ||
95 | + //update string length | ||
96 | + //update cursor position | ||
97 | + } | ||
98 | + | ||
99 | + public void delChar(int mode) | ||
100 | + { | ||
101 | + //switch mode | ||
102 | + //if back | ||
103 | + //del char using backspace | ||
104 | + //remove char from line string | ||
105 | + //update cursor position | ||
106 | + //update line length | ||
107 | + //if forward | ||
108 | + //moveto cursorPosition+1 | ||
109 | + //del char using backspace | ||
110 | + //remove char from line string | ||
111 | + //update line length | ||
112 | + } | ||
113 | + | ||
114 | + private void moveCursorForward() | ||
115 | + { | ||
116 | + System.out.print((char)ESC); | ||
117 | + System.out.print((char)ESC_SEQ); | ||
118 | + System.out.print((char)FORWARD_C); | ||
119 | + } | ||
120 | + | ||
121 | + private void moveCursorBackward() | ||
122 | + { | ||
123 | + System.out.print((char)ESC); | ||
124 | + System.out.print((char)ESC_SEQ); | ||
125 | + System.out.print((char)BACKWARD_D); | ||
126 | + } | ||
127 | + | ||
128 | + public void moveCursorTo(int pos) | ||
129 | + { | ||
130 | + if((pos <= lineLength) && (pos >= 1)) | ||
131 | + { | ||
132 | + if(pos > cursorPosition) | ||
133 | + { | ||
134 | + for(int i = 0; i < (pos - cursorPosition); i++) | ||
135 | + { | ||
136 | + moveCursorForward(); | ||
137 | + } | ||
138 | + } | ||
139 | + | ||
140 | + else if(pos < cursorPosition) | ||
141 | + { | ||
142 | + for(int i = 0; i < (cursorPosition - pos); i++) | ||
143 | + { | ||
144 | + moveCursorBackward(); | ||
145 | + } | ||
146 | + } | ||
147 | + cursorPosition = pos; | ||
148 | + } | ||
149 | + } | ||
150 | + | ||
32 | public int read() throws IOException | 151 | public int read() throws IOException |
33 | { | 152 | { |
34 | return super.read(); | 153 | return super.read(); |
@@ -50,16 +169,75 @@ public class EditableBufferedReader extends BufferedReader | @@ -50,16 +169,75 @@ public class EditableBufferedReader extends BufferedReader | ||
50 | try | 169 | try |
51 | { | 170 | { |
52 | int character = read(); | 171 | int character = read(); |
53 | - switch(character) | 172 | + System.out.println("\n\n" + character + "\n"); |
173 | + switch(parseKey(character)) | ||
54 | { | 174 | { |
55 | - case 0x0D: | ||
56 | - returnKey = true; | 175 | + case ESCAPE_SEQUENCE: |
176 | + if(read() == ESC_SEQ) | ||
177 | + { | ||
178 | + switch(read()) | ||
179 | + { | ||
180 | + case FORWARD_C: | ||
181 | + moveCursorTo(cursorPosition+1); | ||
182 | + break; | ||
183 | + | ||
184 | + case BACKWARD_D: | ||
185 | + moveCursorTo(cursorPosition-1); | ||
186 | + break; | ||
187 | + | ||
188 | + case DEL_3: | ||
189 | + if(read() == TILDE) | ||
190 | + { | ||
191 | + delChar(DEL_FORWARD); | ||
192 | + } | ||
193 | + break; | ||
194 | + | ||
195 | + case INSERT_2: | ||
196 | + if(read() == TILDE) | ||
197 | + { | ||
198 | + writeMode = 1-writeMode; | ||
199 | + } | ||
200 | + break; | ||
201 | + | ||
202 | + case HOME_END: | ||
203 | + switch(read()) | ||
204 | + { | ||
205 | + case HOME: | ||
206 | + moveCursorTo(1); | ||
207 | + break; | ||
208 | + | ||
209 | + case END: | ||
210 | + moveCursorTo(lineLength+1); | ||
211 | + break; | ||
212 | + } | ||
213 | + break; | ||
214 | + } | ||
215 | + } | ||
216 | + break; | ||
217 | + | ||
218 | + case PRINTABLE: | ||
219 | + switch(character) | ||
220 | + { | ||
221 | + case RETURN_KEY: | ||
222 | + returnKey = true; | ||
223 | + break; | ||
224 | + | ||
225 | + case BACKSPACE: | ||
226 | + case BACKSPACE_DEL: | ||
227 | + delChar(DEL_BACKWARD); | ||
228 | + break; | ||
229 | + | ||
230 | + default: | ||
231 | + addChar((char)character); | ||
232 | + break; | ||
233 | + } | ||
57 | break; | 234 | break; |
58 | - default: | ||
59 | - line += (char)character; | ||
60 | - System.out.print((char)character); | 235 | + |
236 | + case NON_PRINTABLE: | ||
237 | + //ignore | ||
61 | break; | 238 | break; |
62 | } | 239 | } |
240 | + | ||
63 | } | 241 | } |
64 | catch (IOException e) | 242 | catch (IOException e) |
65 | { | 243 | { |