Skip to content

Commit

Permalink
ConsoleEngineImpl: refactoring & minor fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 26, 2020
1 parent 6b8d8bc commit c141530
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
49 changes: 28 additions & 21 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,15 @@ public Map<String, Boolean> 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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -777,7 +789,6 @@ private Map<String,Object> defaultPrntOptions() {
return out;
}

@SuppressWarnings("unchecked")
@Override
public void trace(final Object object) {
Object toPrint = object;
Expand All @@ -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<String,Object> options = defaultPrntOptions();
Expand All @@ -813,15 +828,15 @@ public void println(Map<String, Object> 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());
Expand All @@ -830,16 +845,18 @@ public void println(Map<String, Object> 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));
Expand All @@ -860,18 +877,8 @@ private void highlight(int width, String style, String object) {
}
}

private List<AttributedString> highlight(Map<String, Object> options, Object obj) {
List<AttributedString> out = new ArrayList<>();
try {
out = internalHighlight(options, obj);
} catch (Exception e) {
out = internalHighlight(options, engine.convert(obj));
}
return out;
}

@SuppressWarnings("unchecked")
private List<AttributedString> internalHighlight(Map<String, Object> options, Object obj) {
private List<AttributedString> highlight(Map<String, Object> options, Object obj) {
List<AttributedString> out = new ArrayList<>();
int width = (int)options.getOrDefault("width", Integer.MAX_VALUE);
boolean rownum = options.containsKey("rownum");
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c141530

Please sign in to comment.