Skip to content

Commit

Permalink
Refactoring: removed static println method from SystemRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 20, 2020
1 parent 23c29aa commit 2a43364
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public Object getVariable(String name) {
}
return engine.get(name);
}

@Override
public boolean hasVariable(String name) {
return engine.hasVariable(name);
Expand Down Expand Up @@ -817,7 +817,7 @@ public void println(Map<String, Object> options, Object object) {
} else if (!style.isEmpty() && object instanceof String) {
highlight(width, style, (String) object);
} else if (object instanceof Exception) {
SystemRegistry.println(options.getOrDefault("exception", "stack").equals("stack"), terminal(), (Exception)object);
systemRegistry.trace(options.getOrDefault("exception", "stack").equals("stack"), (Exception)object);
} else if (object instanceof String) {
highlight(AttributedStyle.YELLOW + AttributedStyle.BRIGHT, object);
} else if (object instanceof Number) {
Expand Down Expand Up @@ -1153,4 +1153,4 @@ private List<Completer> pipeCompleter(String command) {
return completers;
}

}
}
31 changes: 7 additions & 24 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public interface SystemRegistry extends CommandRegistry {
*/
void trace(Exception exception);

/**
* Print exception on terminal
* @param stack print stack trace if stack true otherwise message
* @param exception exception to be printed
*/
void trace(boolean stack, Exception exception);

/**
* @return terminal
*/
Expand All @@ -99,30 +106,6 @@ public interface SystemRegistry extends CommandRegistry {
*/
Object invoke(String command, Object... args) throws Exception;

/**
* Print exception
* @param stack print stack trace if stack true otherwise message
* @param terminal JLine terminal
* @param exception exception to be printed
*/
static void println(boolean stack, Terminal terminal, Exception exception) {
if (exception instanceof Options.HelpException) {
Options.HelpException.highlight((exception).getMessage(), Options.HelpException.defaultStyle()).print(terminal);
} else if (stack) {
exception.printStackTrace();
} else {
String message = exception.getMessage();
AttributedStringBuilder asb = new AttributedStringBuilder();
if (message != null) {
asb.append(message, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
} else {
asb.append("Caught exception: ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
asb.append(exception.getClass().getCanonicalName(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
}
asb.toAttributedString().println(terminal);
}
}

/**
* @return systemRegistry of the current thread
*/
Expand Down
23 changes: 21 additions & 2 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ private ExecutionResult postProcess(CommandData cmd, boolean statement, Object r
outputStream.flush();
outputStream.close();
out = consoleEngine().postProcess(cmd.rawLine(), result, outputStream.getOutput());
outputStream.reset();
outputStream.reset();
} else if (cmd.variable() != null) {
if (consoleEngine().hasVariable(cmd.variable())) {
out = consoleEngine().postProcess(consoleEngine().getVariable(cmd.variable()));
Expand Down Expand Up @@ -1055,7 +1055,26 @@ public void trace(Exception exception) {
consoleEngine().putVariable("exception", exception);
consoleEngine().trace(exception);
} else {
SystemRegistry.println(false, terminal(), exception);
trace(false, exception);
}
}

@Override
public void trace(boolean stack, Exception exception) {
if (exception instanceof Options.HelpException) {
Options.HelpException.highlight((exception).getMessage(), Options.HelpException.defaultStyle()).print(terminal());
} else if (stack) {
exception.printStackTrace();
} else {
String message = exception.getMessage();
AttributedStringBuilder asb = new AttributedStringBuilder();
if (message != null) {
asb.append(message, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
} else {
asb.append("Caught exception: ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
asb.append(exception.getClass().getCanonicalName(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
}
asb.toAttributedString().println(terminal());
}
}

Expand Down

0 comments on commit 2a43364

Please sign in to comment.