Commit 25fd54b02f38247159c81fae679becf77e977f10

Authored by Imanol-Mikel Barba Sabariego
1 parent ff06f381

git-svn-id: svn://imanolbarba.net/PAD@5 c2ee353e-ed0d-4329-bf56-03aec153487f

readline/bin/pad/prac1/EditableBufferedReader.class
No preview for this file type
readline/src/pad/prac1/EditableBufferedReader.java
... ... @@ -8,18 +8,18 @@ public class EditableBufferedReader extends BufferedReader
8 8 * MODE DEFINITIONS
9 9 */
10 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;
  11 + public static final int DEL_BACKWARD = 0;
  12 + public static final int DEL_FORWARD = 1;
  13 + public static final int INSERT = 0;
  14 + public static final int OVERWRITE = 1;
15 15  
16 16 /*
17 17 * TYPE DEFINITIONS
18 18 */
19 19  
20 20 public static final int ESCAPE_SEQUENCE = 0;
21   - public static final int PRINTABLE = 1;
22   - public static final int NON_PRINTABLE = 2;
  21 + public static final int PRINTABLE = 1;
  22 + public static final int NON_PRINTABLE = 2;
23 23  
24 24 /* KEY DEFINITIONS
25 25 * ESC (0x1B) starts an escape sequence
... ... @@ -33,25 +33,24 @@ public class EditableBufferedReader extends BufferedReader
33 33  
34 34 public static final int RETURN_KEY = 0x0D;
35 35 public static final int BACKSPACE = 0x08;
36   - public static final int BACKSPACE_DEL = 0x7B;
  36 + public static final int BACKSPACE_DEL = 0x7F;
37 37 public static final int ESC = 0x1B;
38 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;
  39 + public static final int FORWARD = 0x43;
  40 + public static final int BACKWARD = 0x44;
  41 + public static final int DEL = 0x33;
42 42 public static final int TILDE = 0x7E;
43 43 public static final int HOME_END = 0x4F;
44 44 public static final int HOME = 0x48;
45 45 public static final int END = 0x46;
46   - public static final int INSERT_2 = 0x32;
47   -
48   - private String line = "";
49   - private int lineLength = 0;
50   - private int cursorPosition = 1;
51   - private boolean returnKey = false;
52   - private int writeMode = INSERT;
53   -
  46 + public static final int INSERT_KEY = 0x32;
  47 + public static final int INS_SPACE = 0x40;
  48 + public static final int DEL_CHAR = 0x50;
  49 + public static final int NUM_0 = 0x30;
54 50  
  51 + private Line line = new Line();
  52 + private boolean returnKey = false;
  53 +
55 54 public EditableBufferedReader(Reader in)
56 55 {
57 56 super(in);
... ... @@ -76,76 +75,104 @@ public class EditableBufferedReader extends BufferedReader
76 75  
77 76 private int parseKey(int key)
78 77 {
79   - //TO IMPLEMENT BOTH MODES
  78 + if(key < 0x20)
  79 + {
  80 + if(key == ESC)
  81 + {
  82 + return ESCAPE_SEQUENCE;
  83 + }
  84 + else if((key == BACKSPACE) || (key == RETURN_KEY))
  85 + {
  86 + return PRINTABLE;
  87 + }
  88 + return NON_PRINTABLE;
  89 + }
80 90 return PRINTABLE;
81 91 }
82 92  
83   - public void addChar(char character)
  93 + public void addChar(char c)
84 94 {
85   - //switch writemode
86   - //if insert
87   - //print space
88   - //moveto cursorPosition-1
89   - //print char
90   - //add to string
91   - //update string length
92   - //update cursor position
93   - //if overwrite
94   - //just print char
95   - //add to string
96   - //update string length
97   - //update cursor position
  95 + switch(line.getMode())
  96 + {
  97 + case INSERT:
  98 + if(line.getCursorPosition() != line.length()+1)
  99 + {
  100 + insertSpace();
  101 + }
  102 + System.out.print(c);
  103 + line.insertCharAt(c, line.getCursorPosition()-1);
  104 + line.setCursorPosition(line.getCursorPosition()+1);
  105 + break;
  106 +
  107 + case OVERWRITE:
  108 + if(line.getCursorPosition() != line.length()+1)
  109 + {
  110 + line.removeCharAt(line.getCursorPosition()-1);
  111 + }
  112 + System.out.print(c);
  113 + line.insertCharAt(c, line.getCursorPosition()-1);
  114 + line.setCursorPosition(line.getCursorPosition()+1);
  115 + break;
  116 + }
98 117 }
99 118  
100 119 public void delChar(int mode)
101 120 {
102   - //switch mode
103   - //if back
104   - //del char using backspace
105   - //remove char from line string
106   - //update cursor position
107   - //update line length
108   - //if forward
109   - //moveto cursorPosition+1
110   - //del char using backspace
111   - //remove char from line string
112   - //update line length
  121 + switch(mode)
  122 + {
  123 + case DEL_BACKWARD:
  124 + if(line.getCursorPosition() != 1)
  125 + {
  126 + System.out.print((char)BACKSPACE);
  127 + deleteCharacter();
  128 + line.removeCharAt(line.getCursorPosition()-2);
  129 + line.setCursorPosition(line.getCursorPosition()-1);
  130 + }
  131 + break;
  132 + case DEL_FORWARD:
  133 + if(line.getCursorPosition() != (line.length()+1))
  134 + {
  135 + line.removeCharAt(line.getCursorPosition()-1);
  136 + deleteCharacter();
  137 + }
  138 + break;
  139 + }
113 140 }
114 141  
115   - private void moveCursorForward()
  142 + private void insertSpace()
116 143 {
117 144 System.out.print((char)ESC);
118 145 System.out.print((char)ESC_SEQ);
119   - System.out.print((char)FORWARD_C);
  146 + System.out.print((char)INS_SPACE);
120 147 }
121 148  
122   - private void moveCursorBackward()
  149 + private void deleteCharacter()
123 150 {
124 151 System.out.print((char)ESC);
125 152 System.out.print((char)ESC_SEQ);
126   - System.out.print((char)BACKWARD_D);
  153 + System.out.print((char)DEL_CHAR);
127 154 }
128 155  
129 156 public void moveCursorTo(int pos)
130 157 {
131   - if((pos <= lineLength) && (pos >= 1))
  158 + if((pos <= line.length()+1) && (pos >= 1))
132 159 {
133   - if(pos > cursorPosition)
  160 + if(pos > line.getCursorPosition())
134 161 {
135   - for(int i = 0; i < (pos - cursorPosition); i++)
136   - {
137   - moveCursorForward();
138   - }
  162 + System.out.print((char)ESC);
  163 + System.out.print((char)ESC_SEQ);
  164 + System.out.print((char)(NUM_0+pos-line.getCursorPosition()));
  165 + System.out.print((char)FORWARD);
139 166 }
140 167  
141   - else if(pos < cursorPosition)
  168 + else if(pos < line.getCursorPosition())
142 169 {
143   - for(int i = 0; i < (cursorPosition - pos); i++)
144   - {
145   - moveCursorBackward();
146   - }
  170 + System.out.print((char)ESC);
  171 + System.out.print((char)ESC_SEQ);
  172 + System.out.print((char)(NUM_0-pos+line.getCursorPosition()));
  173 + System.out.print((char)BACKWARD);
147 174 }
148   - cursorPosition = pos;
  175 + line.setCursorPosition(pos);
149 176 }
150 177 }
151 178  
... ... @@ -170,79 +197,80 @@ public class EditableBufferedReader extends BufferedReader
170 197 try
171 198 {
172 199 int character = read();
173   - System.out.println("\n\n" + character + "\n");
174 200 switch(parseKey(character))
175 201 {
176 202 case ESCAPE_SEQUENCE:
177   - if(read() == ESC_SEQ)
  203 + switch(read())
178 204 {
179   - switch(read())
180   - {
181   - case FORWARD_C:
182   - moveCursorTo(cursorPosition+1);
  205 + case ESC_SEQ:
  206 + switch(read())
  207 + {
  208 + case FORWARD:
  209 + moveCursorTo(line.getCursorPosition()+1);
  210 + break;
  211 +
  212 + case BACKWARD:
  213 + moveCursorTo(line.getCursorPosition()-1);
183 214 break;
184 215  
185   - case BACKWARD_D:
186   - moveCursorTo(cursorPosition-1);
  216 + case DEL:
  217 + if(read() == TILDE)
  218 + {
  219 + delChar(DEL_FORWARD);
  220 + }
187 221 break;
188   -
189   - case DEL_3:
190   - if(read() == TILDE)
191   - {
192   - delChar(DEL_FORWARD);
193   - }
  222 +
  223 + case INSERT_KEY:
  224 + if(read() == TILDE)
  225 + {
  226 + line.toggleMode();
  227 + }
194 228 break;
195   -
196   - case INSERT_2:
197   - if(read() == TILDE)
198   - {
199   - writeMode = 1-writeMode;
200   - }
  229 + }
  230 + break;
  231 +
  232 + case HOME_END:
  233 + switch(read())
  234 + {
  235 + case HOME:
  236 + moveCursorTo(1);
201 237 break;
202   -
203   - case HOME_END:
204   - switch(read())
205   - {
206   - case HOME:
207   - moveCursorTo(1);
208   - break;
209   -
210   - case END:
211   - moveCursorTo(lineLength+1);
212   - break;
213   - }
  238 +
  239 + case END:
  240 + moveCursorTo(line.length()+1);
214 241 break;
215   - }
  242 + }
  243 + break;
216 244 }
217   - break;
  245 + break;
218 246  
219 247 case PRINTABLE:
220 248 switch(character)
221 249 {
222 250 case RETURN_KEY:
223 251 returnKey = true;
224   - break;
  252 + break;
225 253  
226 254 case BACKSPACE:
227 255 case BACKSPACE_DEL:
228 256 delChar(DEL_BACKWARD);
229   - break;
  257 + break;
230 258  
231 259 default:
232 260 addChar((char)character);
233   - break;
  261 + break;
234 262 }
235   - break;
  263 + break;
236 264  
237 265 case NON_PRINTABLE:
238 266 //ignore
239   - break;
  267 + break;
240 268 }
241 269  
242 270 }
243 271 catch (IOException e)
244 272 {
245   - System.out.println("Couldn't unset raw mode");
  273 + System.out.println("Error reading line");
246 274 break;
247 275 }
248 276 }
... ... @@ -256,6 +284,6 @@ public class EditableBufferedReader extends BufferedReader
256 284 return "";
257 285 }
258 286 System.out.println("");
259   - return line;
  287 + return line.toString();
260 288 }
261 289 }
... ...