From e29239950ae0b78f7084d6a51bd8ab7d91bd0db8 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Tue, 22 Oct 2013 08:59:52 +0000 Subject: [PATCH] regex implemented --- readline/bin/pad/prac1/Console.class | Bin 2747 -> 0 bytes readline/bin/pad/prac1/EditableBufferedReader.class | Bin 4358 -> 0 bytes readline/src/pad/prac1/Console.java | 29 +++++++++++++++++++++++------ readline/src/pad/prac1/EditableBufferedReader.java | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- 4 files changed, 77 insertions(+), 64 deletions(-) diff --git a/readline/bin/pad/prac1/Console.class b/readline/bin/pad/prac1/Console.class index 6a989ea..e77f5e1 100644 Binary files a/readline/bin/pad/prac1/Console.class and b/readline/bin/pad/prac1/Console.class differ diff --git a/readline/bin/pad/prac1/EditableBufferedReader.class b/readline/bin/pad/prac1/EditableBufferedReader.class index de53197..04bc8ab 100644 Binary files a/readline/bin/pad/prac1/EditableBufferedReader.class and b/readline/bin/pad/prac1/EditableBufferedReader.class differ diff --git a/readline/src/pad/prac1/Console.java b/readline/src/pad/prac1/Console.java index 8832729..db7f01a 100644 --- a/readline/src/pad/prac1/Console.java +++ b/readline/src/pad/prac1/Console.java @@ -7,24 +7,36 @@ import java.util.regex.Pattern; public class Console implements Observer { - private int currentCol; + private int numCols; + private int numRows; private int currentRow; public void updateCurrentPos() { Coordinate pos = getCurrentPos(); - currentCol = pos.x(); currentRow = pos.y(); } + public void updateTermSize() + { + System.out.print((char)EditableBufferedReader.ESC); + System.out.print((char)EditableBufferedReader.ESC_SEQ); + System.out.print("18t"); + Pattern pattern = Pattern.compile("8;(\\d+);(\\d+)"); + Scanner scanner = new Scanner(System.in); + scanner.findWithinHorizon(pattern,0); + numCols = Integer.parseInt(scanner.match().group(2)); + numRows = Integer.parseInt(scanner.match().group(1)); + } + public Coordinate getCurrentPos() { System.out.print((char)EditableBufferedReader.ESC); System.out.print((char)EditableBufferedReader.ESC_SEQ); System.out.print("6n"); - Pattern pattern = Pattern.compile("(\\d*);(\\d*)"); + Pattern pattern = Pattern.compile("(\\d+);(\\d+)"); Scanner scanner = new Scanner(System.in); - scanner.findWithinHorizon(pattern,0); + scanner.findWithinHorizon(pattern,0); return new Coordinate(Integer.parseInt(scanner.match().group(2)), Integer.parseInt(scanner.match().group(1))); } @@ -65,6 +77,7 @@ public class Console implements Observer public void update(Observable obs, Object arg) { updateCurrentPos(); + updateTermSize(); Line line = (Line)obs; Command cmd = (Command)arg; switch(cmd.getType()) @@ -78,8 +91,12 @@ public class Console implements Observer break; case Command.MOVE_CURSOR: - Coordinate xy = new Coordinate(cmd.getValue(),currentRow); - //Gestionar si excede el numero de columnas con modulos y asi + currentRow += cmd.getValue()/numCols; + if(currentRow > numRows) + { + currentRow = numRows; + } + Coordinate xy = new Coordinate(cmd.getValue()%numCols,currentRow); moveCursorTo(xy); break; } diff --git a/readline/src/pad/prac1/EditableBufferedReader.java b/readline/src/pad/prac1/EditableBufferedReader.java index 70fe225..de24f9e 100644 --- a/readline/src/pad/prac1/EditableBufferedReader.java +++ b/readline/src/pad/prac1/EditableBufferedReader.java @@ -1,7 +1,8 @@ package pad.prac1; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; import java.util.Scanner; -import java.util.regex.Pattern; public class EditableBufferedReader extends BufferedReader @@ -81,74 +82,72 @@ public class EditableBufferedReader extends BufferedReader private void parseKey() throws IOException { - Pattern pattern; Scanner scanner = new Scanner(System.in); - pattern = Pattern.compile("(.{1})"); - if(scanner.findWithinHorizon(pattern,0) != null) + if(scanner.findWithinHorizon("(\\033)?", 0).length() > 0) { - int character = (int)scanner.match().group(1).charAt(0); - if(character >= 0x20 || character == EOF || character == BACKSPACE || character == RETURN_KEY) + scanner.skip("(\\033)?"); + if(scanner.findWithinHorizon("(\\[)?", 0).length() > 0) { - switch(character) - { - case EOF: - endOfFile = true; + scanner.skip("(\\[)?"); + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); + switch(character) + { + case FORWARD: + line.setCursorTo(line.getCursorPosition()+1); + break; + + case BACKWARD: + line.setCursorTo(line.getCursorPosition()-1); break; - case BACKSPACE: - case BACKSPACE_DEL: - line.delChar(DEL_BACKWARD); + case DEL: + line.delChar(DEL_FORWARD); break; - - case RETURN_KEY: - character = LINE_FEED; - default: - line.addChar((char)character); + + case INSERT_KEY: + line.toggleMode(); break; - } - return; + } + return; + } + else if(scanner.findWithinHorizon("O?", 0).length() > 0) + { + scanner.skip("O?"); + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); + switch(character) + { + case HOME: + line.setCursorTo(1); + break; + + case END: + line.setCursorTo(line.length()+1); + break; + } + return; } } - pattern = Pattern.compile("\\^\\[\\[(.{1}).*"); - if(scanner.findWithinHorizon(pattern,3) != null) //escape sequence + else { - int character = scanner.match().group(1).charAt(0); + int character = (int)scanner.findWithinHorizon("(?s).{1}", 0).charAt(0); switch(character) - { - case FORWARD: - line.setCursorTo(line.getCursorPosition()+1); - break; - - case BACKWARD: - line.setCursorTo(line.getCursorPosition()-1); + { + case EOF: + endOfFile = true; break; - case DEL: - line.delChar(DEL_FORWARD); - break; - - case INSERT_KEY: - line.toggleMode(); + case BACKSPACE: + case BACKSPACE_DEL: + line.delChar(DEL_BACKWARD); break; - } - return; - } - pattern = Pattern.compile("\\^\\[O(\\w{1})"); - if(scanner.findWithinHorizon(pattern,3) != null) //HOME or END - { - - int character = (int)scanner.match().group(1).charAt(0); - switch(character) - { - case HOME: - line.setCursorTo(1); - break; - - case END: - line.setCursorTo(line.length()+1); + + case RETURN_KEY: + character = LINE_FEED; + default: + line.addChar((char)character); break; - } - return; + } + return; } } @@ -199,9 +198,6 @@ public class EditableBufferedReader extends BufferedReader * * up/down keys (needs text to work) * pgup pgdown keys (goesto penultima linea visible) - * read terminal size - * update size dynamically - * long strings wrap down * cursor keys skip final \n on a line * careful if overwrite mode and final character is \n */ -- libgit2 0.22.2