Skip to content

Commit

Permalink
'prnt <exception>' prints now stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 20, 2020
1 parent 1e289b6 commit 82a9eb0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public CommandInput(Object[] xargs, boolean dumb) {
this.xargs = xargs;
this.args = new String[xargs.length];
for (int i = 0; i < xargs.length; i++) {
args[i] = xargs[i].toString();
args[i] = xargs[i] != null ? xargs[i].toString() : "";
}
}

Expand Down
46 changes: 25 additions & 21 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void scriptExtension(String command) {
throw new IllegalArgumentException("Command not found: " + command);
}
}

private void doArgs(String[] args) {
List<String> _args = new ArrayList<>();
if (isConsoleScript()) {
Expand All @@ -298,7 +298,7 @@ private void doArgs(String[] args) {
this.verbose = true;
}
} else {
_args.add(a);
_args.add(a);
}
}
this.args = _args.toArray(new String[0]);
Expand All @@ -315,7 +315,7 @@ private boolean isConsoleScript() {
private boolean isScript() {
return engine.getExtensions().contains(extension) || scriptExtension.equals(extension);
}

public boolean execute() throws Exception {
if (!isScript()) {
return false;
Expand All @@ -338,7 +338,7 @@ public boolean execute() throws Exception {
line = l.trim().substring(2);
}
usage.append(line).append('\n');
}
}
if (usage.length() > 0) {
usage.append("\n");
throw new HelpException(usage.toString());
Expand All @@ -351,7 +351,7 @@ public boolean execute() throws Exception {
}
return true;
}

private void internalExecute() throws Exception {
if (isEngineScript()) {
result = engine.execute(script, expandParameters(args));
Expand Down Expand Up @@ -468,7 +468,7 @@ public Object execute(ParsedLine pl) throws Exception {
out = engine.get(line);
} else if (Parser.getVariable(line) == null) {
out = engine.execute(line);
engine.put("_", out);
engine.put("_", out);
} else {
engine.execute(line);
}
Expand All @@ -482,8 +482,8 @@ public Object postProcess(String line, Object result) {
if (Parser.getVariable(line) != null) {
engine.put(Parser.getVariable(line), result);
out = null;
} else {
engine.put("_", result);
} else if (!Parser.getCommand(line).equals("show")) {
engine.put("_", result);
}
return out;
}
Expand Down Expand Up @@ -532,19 +532,23 @@ public void println(Map<String, Object> options, Object object) {
if (object == null) {
return;
}
options.putIfAbsent("width", terminal.getSize().getColumns());
String style = (String)options.getOrDefault("style", "");
int width = (int)options.get("width");
if (style.equalsIgnoreCase("JSON")) {
highlight(width, style, engine.format(options, object));
} else if (!style.isEmpty() && object instanceof String) {
highlight(width, style, (String)object);
if (object instanceof Exception) {
((Exception) object).printStackTrace();
} else {
for (AttributedString as : engine.highlight(options, object)) {
as.println(terminal);
options.putIfAbsent("width", terminal.getSize().getColumns());
String style = (String)options.getOrDefault("style", "");
int width = (int)options.get("width");
if (style.equalsIgnoreCase("JSON")) {
highlight(width, style, engine.format(options, object));
} else if (!style.isEmpty() && object instanceof String) {
highlight(width, style, (String)object);
} else {
for (AttributedString as : engine.highlight(options, object)) {
as.println(terminal);
}
}
terminal.flush();
}
terminal.flush();
}

private void highlight(int width, String style, String object) {
Expand Down Expand Up @@ -627,16 +631,16 @@ private Object prnt(Builtins.CommandInput input) {
options.put("width", opt.getNumber("width"));
}
if (opt.isSet("rownum")) {
options.put("rownum", true);
options.put("rownum", true);
}
List<Object> args = opt.argObjects();
if (args.size() > 0) {
println(options, args.get(0));
}
return null;
}
private Object slurp(Builtins.CommandInput input) {

private Object slurp(Builtins.CommandInput input) {
final String[] usage = {
"slurp - slurp file context to string",
"Usage: slurp [OPTIONS] file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ public Object invoke(String command, Object... args) throws Exception {
int id = registryId(command);
if (id > -1) {
out = commandRegistries[id].invoke(command, args);
} else if (consoleId != null) {
} else if (consoleId != null) {
out = consoleEngine().invoke(command, args);
}
return out;
}

@Override
public Object execute(ParsedLine pl) throws Exception {
if (pl.line().isEmpty() || pl.line().startsWith("#")) {
return null;
}
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object out = null;
Expand All @@ -126,7 +129,7 @@ public Object execute(ParsedLine pl) throws Exception {
out = commandRegistries[id].invoke(cmd, consoleEngine().expandParameters(argv));
out = consoleEngine().postProcess(pl.line(), out);
} else {
out = commandRegistries[id].execute(cmd, argv);
out = commandRegistries[id].execute(cmd, argv);
}
} else if (consoleId != null) {
out = consoleEngine().execute(pl);
Expand Down

0 comments on commit 82a9eb0

Please sign in to comment.