Skip to content

Commit

Permalink
DefaultPrinter: improve reporting of bad option values
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 9, 2021
1 parent 977550b commit 2a95d38
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Expand Up @@ -174,16 +174,32 @@ public Map<String, Object> compileOptions(Options opt) {
}
if (opt.isSet(Printer.ROW_HIGHLIGHT)) {
try {
TableRows tr = TableRows.valueOf(opt.get(Printer.ROW_HIGHLIGHT).toUpperCase());
options.put(Printer.ROW_HIGHLIGHT, tr);
options.put(Printer.ROW_HIGHLIGHT, optionRowHighlight(opt.get(Printer.ROW_HIGHLIGHT)));
} catch (Exception e) {
throw new IllegalArgumentException(Printer.ROW_HIGHLIGHT + " bad value: " + opt.get(Printer.ROW_HIGHLIGHT));
RuntimeException exception = new BadOptionValueException(Printer.ROW_HIGHLIGHT + " has a bad value: "
+ opt.get(Printer.ROW_HIGHLIGHT));
exception.addSuppressed(e);
throw exception;
}
}
options.put("exception", "stack");
return options;
}

private TableRows optionRowHighlight(Object value) {
if (value instanceof TableRows || value == null) {
return (TableRows)value;
} else if (value instanceof String) {
String val = ((String)value).trim().toUpperCase();
if (!val.isEmpty() && !val.equals("NULL")) {
return TableRows.valueOf(val);
} else {
return null;
}
}
throw new IllegalArgumentException("rowHighlight has a bad option value type: " + value.getClass());
}

@Override
public Exception prntCommand(CommandInput input) {
Exception out = null;
Expand Down Expand Up @@ -645,6 +661,13 @@ private boolean similarSets(Set<String> ref, Set<String> c2, double threshold) {
return out;
}

@SuppressWarnings("serial")
private static class BadOptionValueException extends RuntimeException {
public BadOptionValueException(String message) {
super(message);
}
}

@SuppressWarnings("serial")
private static class TruncatedOutputException extends RuntimeException {
public TruncatedOutputException(String message) {
Expand Down Expand Up @@ -677,9 +700,9 @@ private boolean isNumber(String str) {
@SuppressWarnings("unchecked")
private void highlightAndPrint(Map<String, Object> options, Object obj) {
int width = (int)options.get(Printer.WIDTH);
boolean rownum = options.containsKey(Printer.ROWNUM);
totLines = 0;
String message = null;
RuntimeException runtimeException = null;
if (obj == null) {
// do nothing
} else if (obj instanceof Map) {
Expand All @@ -701,6 +724,17 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
highlightValue(options, null, objectToString(options, obj)).println(terminal());
}
} else {
String columnSep = "";
TableRows tableRows = null;
boolean rownum = options.containsKey(Printer.ROWNUM);
try {
columnSep = (String) options.getOrDefault(Printer.BORDER, "");
tableRows = optionRowHighlight(options.getOrDefault(Printer.ROW_HIGHLIGHT, null));
} catch (Exception e) {
runtimeException = new BadOptionValueException("Option " + Printer.BORDER + " or "
+ Printer.ROW_HIGHLIGHT + " has a bad value!");
runtimeException.addSuppressed(e);
}
try {
Object elem = collection.iterator().next();
boolean convert = canConvert(elem);
Expand Down Expand Up @@ -765,8 +799,6 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
}
}
}
String columnSep = (String)options.getOrDefault(Printer.BORDER, "");
TableRows tableRows = (TableRows)options.getOrDefault(Printer.ROW_HIGHLIGHT, null);
toTabStops(columns, collection.size(), rownum, columnSep);
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(columns);
asb.style(prntStyle.resolve(".th"));
Expand Down Expand Up @@ -828,8 +860,6 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
}
}
}
String columnSep = (String)options.getOrDefault(Printer.BORDER, "");
TableRows tableRows = (TableRows)options.getOrDefault(Printer.ROW_HIGHLIGHT, null);
toTabStops(columns, collection.size(), rownum, columnSep);
int row = 0;
int firstColumn = rownum ? 1 : 0;
Expand Down Expand Up @@ -863,7 +893,6 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
highlightList(options, collection, width);
}
} catch (Exception e) {
message = e.getMessage();
Log.debug("Stack: ", e);
highlightList(options, collection, width);
}
Expand All @@ -881,6 +910,9 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
asb.styled(prntStyle.resolve(".em"), message);
asb.println(terminal());
}
if (runtimeException != null) {
throw runtimeException;
}
}

private boolean doRowHighlight(int row, TableRows tableRows) {
Expand Down

0 comments on commit 2a95d38

Please sign in to comment.