From 8a46e0c48b74316a0a2ffc8f22574c0080458530 Mon Sep 17 00:00:00 2001 From: giuseppelio Date: Thu, 11 Jul 2013 17:47:19 +0200 Subject: [PATCH 1/3] added the possibility to avoid the operation check and find in ControlOperatorParser. added a boolean in Setting to set if the check should be performed or not (default is true = perform the check) --- .../org/jboss/jreadline/console/Console.java | 5 +- .../operator/ControlOperatorParser.java | 143 +++++++++--------- .../jreadline/console/settings/Settings.java | 10 ++ 3 files changed, 83 insertions(+), 75 deletions(-) diff --git a/src/main/java/org/jboss/jreadline/console/Console.java b/src/main/java/org/jboss/jreadline/console/Console.java index abf215cc6..6a7388821 100644 --- a/src/main/java/org/jboss/jreadline/console/Console.java +++ b/src/main/java/org/jboss/jreadline/console/Console.java @@ -52,7 +52,7 @@ * A console reader. * Supports ansi terminals * - * @author Ståle W. Pedersen + * @author StÃ¥le W. Pedersen */ public class Console { @@ -354,7 +354,8 @@ public ConsoleOutput read(String prompt, Character mask) throws IOException { result = parseOperation(operation, mask); if(result != null) { - operations = ControlOperatorParser.findAllControlOperators(result); + operations = ControlOperatorParser.findAllControlOperators(result, + settings.isOperationsEnabled()); ConsoleOutput output = parseOperations(); output = processInternalCommands(output); if(output.getBuffer() != null) { diff --git a/src/main/java/org/jboss/jreadline/console/operator/ControlOperatorParser.java b/src/main/java/org/jboss/jreadline/console/operator/ControlOperatorParser.java index 43a326caa..a7ba3204c 100644 --- a/src/main/java/org/jboss/jreadline/console/operator/ControlOperatorParser.java +++ b/src/main/java/org/jboss/jreadline/console/operator/ControlOperatorParser.java @@ -7,6 +7,7 @@ package org.jboss.jreadline.console.operator; import org.jboss.jreadline.console.ConsoleOperation; +import org.jboss.jreadline.console.operator.ControlOperator; import java.util.ArrayList; import java.util.List; @@ -81,88 +82,84 @@ private static int findLastRedirectionOrPipelinePositionBeforeCursor(Pattern pat } return end; } + + /** + * Parse buffer checking redirection operations + * + * @param buffer + * text + * @return all RedirectionOperations + */ + public static List findAllControlOperators(String buffer) { + return findAllControlOperators(buffer, true); + } /** - * Parse buffer and find all RedirectionOperations + * Parse buffer * - * @param buffer text + * @param buffer + * text + * @param checkOperations if true check and find operations otherwise skip this step * @return all RedirectionOperations */ - public static List findAllControlOperators(String buffer) { - Matcher matcher = controlOperatorPattern.matcher(buffer); + public static List findAllControlOperators(String buffer, boolean checkOperations) { List reOpList = new ArrayList(); - while(matcher.find()) { - if(matcher.group(1) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.OVERWRITE_OUT_AND_ERR, - buffer.substring(0, matcher.start(1)))); - buffer = buffer.substring(matcher.end(1)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(2) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.APPEND_ERR, - buffer.substring(0, matcher.start(2)))); - buffer = buffer.substring(matcher.end(2)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(3) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.OVERWRITE_ERR, - buffer.substring(0, matcher.start(3)))); - buffer = buffer.substring(matcher.end(3)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(4) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.APPEND_OUT, - buffer.substring(0, matcher.start(4)))); - buffer = buffer.substring(matcher.end(4)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(5) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.OVERWRITE_OUT, - buffer.substring(0, matcher.start(5)))); - buffer = buffer.substring(matcher.end(5)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(6) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.OVERWRITE_IN, - buffer.substring(0, matcher.start(6)))); - buffer = buffer.substring(matcher.end(6)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(7) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.PIPE_OUT_AND_ERR, - buffer.substring(0, matcher.start(7)))); - buffer = buffer.substring(matcher.end(7)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(8) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.PIPE, - buffer.substring(0, matcher.start(8)))); - buffer = buffer.substring(matcher.end(8)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(9) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.END, - buffer.substring(0, matcher.start(9)))); - buffer = buffer.substring(matcher.end(9)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(10) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.AND, - buffer.substring(0, matcher.start(10)))); - buffer = buffer.substring(matcher.end(10)); - matcher = controlOperatorPattern.matcher(buffer); - } - else if(matcher.group(11) != null) { - reOpList.add( new ConsoleOperation(ControlOperator.AMP, - buffer.substring(0, matcher.start(11)))); - buffer = buffer.substring(matcher.end(11)); - matcher = controlOperatorPattern.matcher(buffer); + if (checkOperations) { + Matcher matcher = controlOperatorPattern.matcher(buffer); + while (matcher.find()) { + if (matcher.group(1) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.OVERWRITE_OUT_AND_ERR, buffer.substring(0, + matcher.start(1)))); + buffer = buffer.substring(matcher.end(1)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(2) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.APPEND_ERR, buffer.substring(0, matcher.start(2)))); + buffer = buffer.substring(matcher.end(2)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(3) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.OVERWRITE_ERR, buffer.substring(0, matcher.start(3)))); + buffer = buffer.substring(matcher.end(3)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(4) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.APPEND_OUT, buffer.substring(0, matcher.start(4)))); + buffer = buffer.substring(matcher.end(4)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(5) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.OVERWRITE_OUT, buffer.substring(0, matcher.start(5)))); + buffer = buffer.substring(matcher.end(5)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(6) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.OVERWRITE_IN, buffer.substring(0, matcher.start(6)))); + buffer = buffer.substring(matcher.end(6)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(7) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.PIPE_OUT_AND_ERR, buffer.substring(0, + matcher.start(7)))); + buffer = buffer.substring(matcher.end(7)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(8) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.PIPE, buffer.substring(0, matcher.start(8)))); + buffer = buffer.substring(matcher.end(8)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(9) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.END, buffer.substring(0, matcher.start(9)))); + buffer = buffer.substring(matcher.end(9)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(10) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.AND, buffer.substring(0, matcher.start(10)))); + buffer = buffer.substring(matcher.end(10)); + matcher = controlOperatorPattern.matcher(buffer); + } else if (matcher.group(11) != null) { + reOpList.add(new ConsoleOperation(ControlOperator.AMP, buffer.substring(0, matcher.start(11)))); + buffer = buffer.substring(matcher.end(11)); + matcher = controlOperatorPattern.matcher(buffer); + } } } - if(reOpList.size() == 0) - reOpList.add(new ConsoleOperation( ControlOperator.NONE, buffer)); - if(buffer.trim().length() > 0) + if (reOpList.size() == 0) + reOpList.add(new ConsoleOperation(ControlOperator.NONE, buffer)); + if (buffer.trim().length() > 0) reOpList.add(new ConsoleOperation(ControlOperator.NONE, buffer)); return reOpList; diff --git a/src/main/java/org/jboss/jreadline/console/settings/Settings.java b/src/main/java/org/jboss/jreadline/console/settings/Settings.java index 0eafce425..cda43d9bc 100644 --- a/src/main/java/org/jboss/jreadline/console/settings/Settings.java +++ b/src/main/java/org/jboss/jreadline/console/settings/Settings.java @@ -51,6 +51,7 @@ public class Settings { private KeyOperationManager operationManager = new KeyOperationManager(); private File aliasFile; private boolean aliasEnabled = true; + private boolean operationsEnabled = true; private static final Settings INSTANCE = new Settings(); @@ -80,6 +81,7 @@ public void resetToDefaults() { setQuitHandler(null); operationManager.clear(); setAliasEnabled(true); + setOperationsEnabled(true); } /** @@ -498,6 +500,14 @@ public boolean isAliasEnabled() { public void setAliasEnabled(boolean enabled) { aliasEnabled = enabled; } + + public boolean isOperationsEnabled() { + return operationsEnabled; + } + + public void setOperationsEnabled(boolean operationsEnabled) { + this.operationsEnabled = operationsEnabled; + } public void setQuitHandler(QuitHandler qh) { quitHandler = qh; From bd13987873d07364f4069d7c35f9d162bcaea128 Mon Sep 17 00:00:00 2001 From: "giuseppe.lio" Date: Tue, 27 Aug 2013 11:53:41 +0200 Subject: [PATCH 2/3] width and height of the terminal window must be calculated every time the terminal window is resized, not only the first time the getWidth and getHeight methods are executed --- .../jreadline/terminal/POSIXTerminal.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jboss/jreadline/terminal/POSIXTerminal.java b/src/main/java/org/jboss/jreadline/terminal/POSIXTerminal.java index 7021de4a7..c3b6fa104 100644 --- a/src/main/java/org/jboss/jreadline/terminal/POSIXTerminal.java +++ b/src/main/java/org/jboss/jreadline/terminal/POSIXTerminal.java @@ -158,13 +158,11 @@ public void writeToStdErr(char err) throws IOException { */ @Override public int getHeight() { - if(height < 0) { - try { - height = getTerminalProperty("rows"); - } - catch (Exception e) { - logger.severe("Failed to fetch terminal height: "+e.getMessage()); - } + try { + height = getTerminalProperty("rows"); + } + catch (Exception e) { + logger.severe("Failed to fetch terminal height: "+e.getMessage()); } //cant use height < 0 if(height < 0) @@ -178,13 +176,11 @@ public int getHeight() { */ @Override public int getWidth() { - if(width < 0) { - try { - width = getTerminalProperty("columns"); - } - catch (Exception e) { - logger.severe("Failed to fetch terminal width: "+e.getMessage()); - } + try { + width = getTerminalProperty("columns"); + } + catch (Exception e) { + logger.severe("Failed to fetch terminal width: "+e.getMessage()); } //cant use with < 0 if(width < 0) From 13e9fa67796c1740943d7faa7c5f5dcdc50fd8f6 Mon Sep 17 00:00:00 2001 From: "giuseppe.lio" Date: Tue, 27 Aug 2013 14:30:54 +0200 Subject: [PATCH 3/3] sending carriage return ("\r") instead of sending 0G command to the terminal when redrawing the line. 0G command will not work when emulating ANSI, SCOAnsi, Vshell, VT100, VT102 terminals --- src/main/java/org/jboss/jreadline/console/Console.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jboss/jreadline/console/Console.java b/src/main/java/org/jboss/jreadline/console/Console.java index 6a7388821..357d2c0e2 100644 --- a/src/main/java/org/jboss/jreadline/console/Console.java +++ b/src/main/java/org/jboss/jreadline/console/Console.java @@ -866,7 +866,7 @@ private void drawLine(String line) throws IOException { for(int i=0; i