From c1415305c9325528c5b3b94cd19ff9d5f2c8f9e0 Mon Sep 17 00:00:00 2001 From: mattirn Date: Wed, 26 Feb 2020 12:47:08 +0100 Subject: [PATCH] ConsoleEngineImpl: refactoring & minor fixes and improvements --- .../org/jline/builtins/ConsoleEngineImpl.java | 49 +++++++++++-------- demo/src/main/scripts/init.jline | 1 + 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java index 5531c2b30..496aef0fd 100644 --- a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java +++ b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java @@ -253,8 +253,15 @@ public Map scripts() { int idx = name.lastIndexOf("."); out.put(name.substring(0, idx), name.substring(idx + 1).equals(scriptExtension)); } + } catch (NoSuchFileException e) { + error("Failed reading PATH. No file found: " + e.getMessage()); + } catch (InvalidPathException e) { + error("Failed reading PATH. Invalid path:"); + error(e.toString()); } catch (Exception e) { + error("Failed reading PATH:"); trace(e); + engine.put("exception", e); } return out; } @@ -372,7 +379,7 @@ public ScriptFile(String command, String cmdLine, String[] args) { public ScriptFile(File script, String cmdLine, String[] args) { if (!script.exists()) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Script file not found!"); } this.script = script; this.cmdLine = cmdLine; @@ -471,12 +478,17 @@ private void internalExecute() throws Exception { result = engine.execute(script, expandParameters(args)); } else if (isConsoleScript()) { executing = true; - boolean done = false; + boolean done = true; String line = ""; try (BufferedReader br = new BufferedReader(new FileReader(script))) { for (String l; (l = br.readLine()) != null;) { + if (l.trim().isEmpty() || l.trim().startsWith("#")) { + done = true; + continue; + } try { line += l; + done = false; parser().parse(line, line.length() + 1, ParseContext.ACCEPT_LINE); done = true; for (int i = 1; i < args.length; i++) { @@ -777,7 +789,6 @@ private Map defaultPrntOptions() { return out; } - @SuppressWarnings("unchecked") @Override public void trace(final Object object) { Object toPrint = object; @@ -798,6 +809,10 @@ public void trace(final Object object) { println(options, toPrint); } + private void error(String message) { + highlight(AttributedStyle.RED, message).println(terminal()); + } + @Override public void println(Object object) { Map options = defaultPrntOptions(); @@ -813,15 +828,15 @@ public void println(Map options, Object object) { String style = (String) options.getOrDefault("style", ""); int width = (int) options.get("width"); if (style.equalsIgnoreCase("JSON")) { - highlight(width, style, engine.toJson(object)); + highlightAndPrint(width, style, engine.toJson(object)); } else if (!style.isEmpty() && object instanceof String) { - highlight(width, style, (String) object); + highlightAndPrint(width, style, (String) object); } else if (object instanceof Exception) { systemRegistry.trace(options.getOrDefault("exception", "stack").equals("stack"), (Exception)object); } else if (object instanceof String) { - highlight(AttributedStyle.YELLOW + AttributedStyle.BRIGHT, object); + highlight(AttributedStyle.YELLOW + AttributedStyle.BRIGHT, object).println(terminal()); } else if (object instanceof Number) { - highlight(AttributedStyle.BLUE + AttributedStyle.BRIGHT, object); + highlight(AttributedStyle.BLUE + AttributedStyle.BRIGHT, object).println(terminal()); } else { for (AttributedString as : highlight(options, object)) { as.println(terminal()); @@ -830,16 +845,18 @@ public void println(Map options, Object object) { terminal().flush(); } - private void highlight(int attrStyle, Object obj) { + private AttributedString highlight(int attrStyle, Object obj) { + AttributedString out = new AttributedString(""); AttributedStringBuilder asb = new AttributedStringBuilder(); String tp = obj.toString(); if (!tp.isEmpty()) { asb.append(tp, AttributedStyle.DEFAULT.foreground(attrStyle)); - asb.toAttributedString().println(terminal()); + out = asb.toAttributedString(); } + return out; } - private void highlight(int width, String style, String object) { + private void highlightAndPrint(int width, String style, String object) { Path nanorc = configPath != null ? configPath.getConfig("jnanorc") : null; if (engine.hasVariable(VAR_NANORC)) { nanorc = Paths.get((String)engine.get(VAR_NANORC)); @@ -860,18 +877,8 @@ private void highlight(int width, String style, String object) { } } - private List highlight(Map options, Object obj) { - List out = new ArrayList<>(); - try { - out = internalHighlight(options, obj); - } catch (Exception e) { - out = internalHighlight(options, engine.convert(obj)); - } - return out; - } - @SuppressWarnings("unchecked") - private List internalHighlight(Map options, Object obj) { + private List highlight(Map options, Object obj) { List out = new ArrayList<>(); int width = (int)options.getOrDefault("width", Integer.MAX_VALUE); boolean rownum = options.containsKey("rownum"); diff --git a/demo/src/main/scripts/init.jline b/demo/src/main/scripts/init.jline index 13eb66c86..0798778a7 100644 --- a/demo/src/main/scripts/init.jline +++ b/demo/src/main/scripts/init.jline @@ -31,6 +31,7 @@ pipe |& '.' ' ' pipe grep '.collect{it.toString()}.findAll{it=~/' '/}' alias null '|& identity{}' alias xargs '|; %{0} %{1} %{2} %{3} %{4} %{5} %{6} %{7} %{8} %{9}' +alias to '|; %{0} =' # # create test-widget and bind it to ctrl-alt-x # It will read widget name from buffer and execute it