Commit e29239950ae0b78f7084d6a51bd8ab7d91bd0db8

Authored by Imanol-Mikel Barba Sabariego
1 parent a59076a2

regex implemented

git-svn-id: svn://imanolbarba.net/PAD@12 c2ee353e-ed0d-4329-bf56-03aec153487f
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
... ... @@ -7,24 +7,36 @@ import java.util.regex.Pattern;
7 7  
8 8 public class Console implements Observer
9 9 {
10   - private int currentCol;
  10 + private int numCols;
  11 + private int numRows;
11 12 private int currentRow;
12 13  
13 14 public void updateCurrentPos()
14 15 {
15 16 Coordinate pos = getCurrentPos();
16   - currentCol = pos.x();
17 17 currentRow = pos.y();
18 18 }
19 19  
  20 + public void updateTermSize()
  21 + {
  22 + System.out.print((char)EditableBufferedReader.ESC);
  23 + System.out.print((char)EditableBufferedReader.ESC_SEQ);
  24 + System.out.print("18t");
  25 + Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)");
  26 + Scanner scanner = new Scanner(System.in);
  27 + scanner.findWithinHorizon(pattern,0);
  28 + numCols = Integer.parseInt(scanner.match().group(2));
  29 + numRows = Integer.parseInt(scanner.match().group(1));
  30 + }
  31 +
20 32 public Coordinate getCurrentPos()
21 33 {
22 34 System.out.print((char)EditableBufferedReader.ESC);
23 35 System.out.print((char)EditableBufferedReader.ESC_SEQ);
24 36 System.out.print("6n");
25   - Pattern pattern = Pattern.compile("(\\d*);(\\d*)");
  37 + Pattern pattern = Pattern.compile("(\\d+);(\\d+)");
26 38 Scanner scanner = new Scanner(System.in);
27   - scanner.findWithinHorizon(pattern,0);
  39 + scanner.findWithinHorizon(pattern,0);
28 40 return new Coordinate(Integer.parseInt(scanner.match().group(2)),
29 41 Integer.parseInt(scanner.match().group(1)));
30 42 }
... ... @@ -65,6 +77,7 @@ public class Console implements Observer
65 77 public void update(Observable obs, Object arg)
66 78 {
67 79 updateCurrentPos();
  80 + updateTermSize();
68 81 Line line = (Line)obs;
69 82 Command cmd = (Command)arg;
70 83 switch(cmd.getType())
... ... @@ -78,8 +91,12 @@ public class Console implements Observer
78 91 break;
79 92  
80 93 case Command.MOVE_CURSOR:
81   - Coordinate xy = new Coordinate(cmd.getValue(),currentRow);
82   - //Gestionar si excede el numero de columnas con modulos y asi
  94 + currentRow += cmd.getValue()/numCols;
  95 + if(currentRow > numRows)
  96 + {
  97 + currentRow = numRows;
  98 + }
  99 + Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow);
83 100 moveCursorTo(xy);
84 101 break;
85 102 }
... ...
readline/src/pad/prac1/EditableBufferedReader.java
1 1 package pad.prac1;
2   -import java.io.*;
  2 +import java.io.BufferedReader;
  3 +import java.io.IOException;
  4 +import java.io.Reader;
3 5 import java.util.Scanner;
4   -import java.util.regex.Pattern;
5 6  
6 7  
7 8 public class EditableBufferedReader extends BufferedReader
... ... @@ -81,74 +82,72 @@ public class EditableBufferedReader extends BufferedReader
81 82  
82 83 private void parseKey() throws IOException
83 84 {
84   - Pattern pattern;
85 85 Scanner scanner = new Scanner(System.in);
86   - pattern = Pattern.compile("(.{1})");
87   - if(scanner.findWithinHorizon(pattern,0) != null)
  86 + if(scanner.findWithinHorizon("(\\033)?", 0).length() > 0)
88 87 {
89   - int character = (int)scanner.match().group(1).charAt(0);
90   - if(character >= 0x20 || character == EOF || character == BACKSPACE || character == RETURN_KEY)
  88 + scanner.skip("(\\033)?");
  89 + if(scanner.findWithinHorizon("(\\[)?", 0).length() > 0)
91 90 {
92   - switch(character)
93   - {
94   - case EOF:
95   - endOfFile = true;
  91 + scanner.skip("(\\[)?");
  92 + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0);
  93 + switch(character)
  94 + {
  95 + case FORWARD:
  96 + line.setCursorTo(line.getCursorPosition()+1);
  97 + break;
  98 +
  99 + case BACKWARD:
  100 + line.setCursorTo(line.getCursorPosition()-1);
96 101 break;
97 102  
98   - case BACKSPACE:
99   - case BACKSPACE_DEL:
100   - line.delChar(DEL_BACKWARD);
  103 + case DEL:
  104 + line.delChar(DEL_FORWARD);
101 105 break;
102   -
103   - case RETURN_KEY:
104   - character = LINE_FEED;
105   - default:
106   - line.addChar((char)character);
  106 +
  107 + case INSERT_KEY:
  108 + line.toggleMode();
107 109 break;
108   - }
109   - return;
  110 + }
  111 + return;
  112 + }
  113 + else if(scanner.findWithinHorizon("O?", 0).length() > 0)
  114 + {
  115 + scanner.skip("O?");
  116 + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0);
  117 + switch(character)
  118 + {
  119 + case HOME:
  120 + line.setCursorTo(1);
  121 + break;
  122 +
  123 + case END:
  124 + line.setCursorTo(line.length()+1);
  125 + break;
  126 + }
  127 + return;
110 128 }
111 129 }
112   - pattern = Pattern.compile("\\^\\[\\[(.{1}).*");
113   - if(scanner.findWithinHorizon(pattern,3) != null) //escape sequence
  130 + else
114 131 {
115   - int character = scanner.match().group(1).charAt(0);
  132 + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0);
116 133 switch(character)
117   - {
118   - case FORWARD:
119   - line.setCursorTo(line.getCursorPosition()+1);
120   - break;
121   -
122   - case BACKWARD:
123   - line.setCursorTo(line.getCursorPosition()-1);
  134 + {
  135 + case EOF:
  136 + endOfFile = true;
124 137 break;
125 138  
126   - case DEL:
127   - line.delChar(DEL_FORWARD);
128   - break;
129   -
130   - case INSERT_KEY:
131   - line.toggleMode();
  139 + case BACKSPACE:
  140 + case BACKSPACE_DEL:
  141 + line.delChar(DEL_BACKWARD);
132 142 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);
  143 +
  144 + case RETURN_KEY:
  145 + character = LINE_FEED;
  146 + default:
  147 + line.addChar((char)character);
149 148 break;
150   - }
151   - return;
  149 + }
  150 + return;
152 151 }
153 152 }
154 153  
... ... @@ -199,9 +198,6 @@ public class EditableBufferedReader extends BufferedReader
199 198 *
200 199 * up/down keys (needs text to work)
201 200 * pgup pgdown keys (goesto penultima linea visible)
202   - * read terminal size
203   - * update size dynamically
204   - * long strings wrap down
205 201 * cursor keys skip final \n on a line
206 202 * careful if overwrite mode and final character is \n
207 203 */
... ...