Skip to content

Commit

Permalink
Command prnt style option does not work, fixes #661
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 5, 2021
1 parent d22d576 commit 91c205b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public String[] appendUsage(String[] customUsage) {
" --indention=INDENTION Indention size",
" --maxColumnWidth=WIDTH Maximum column width",
" -d --maxDepth=DEPTH Maximum depth objects are resolved",
" --maxrows=ROWS Maximum number of lines to display",
" -n --maxrows=ROWS Maximum number of lines to display",
" --oneRowTable Display one row data on table",
" -h --rowHighlight=ROW Highlight table rows. ROW = EVEN, ODD, ALL",
" -r --rownum Display table row numbers",
Expand Down Expand Up @@ -311,22 +311,20 @@ private void internalPrintln(Map<String, Object> options, Object object) {
colIn.addAll((List<String>)options.get(Printer.COLUMNS_IN));
options.put(Printer.COLUMNS_IN, colIn);
}
String valueStyle = (String)options.get(Printer.VALUE_STYLE);
if (options.containsKey(Printer.VALUE_STYLE)) {
options.put(Printer.VALUE_STYLE, valueHighlighter(valueStyle));
}
options.put(Printer.VALUE_STYLE, valueHighlighter((String)options.getOrDefault(Printer.VALUE_STYLE, null)));
prntStyle = Styles.prntStyle();
options.putIfAbsent(Printer.WIDTH, terminal().getSize().getColumns());
String style = (String) options.getOrDefault(Printer.STYLE, "");
options.put(Printer.STYLE, valueHighlighter(style));
int width = (int) options.get(Printer.WIDTH);
if (!style.isEmpty() && object instanceof String) {
highlightAndPrint(width, style, (String) object, doValueHighlight(options, (String) object));
highlightAndPrint(width, (SyntaxHighlighter)options.get(Printer.STYLE), (String) object, true);
} else if (style.equalsIgnoreCase("JSON")) {
if (engine == null) {
throw new IllegalArgumentException("JSON style not supported!");
}
String json = engine.toJson(object);
highlightAndPrint(width, style, json, doValueHighlight(options, json));
highlightAndPrint(width, (SyntaxHighlighter)options.get(Printer.STYLE), json, true);
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
highlightAndPrint(options, object);
} else if (object instanceof Exception) {
Expand All @@ -335,7 +333,8 @@ private void internalPrintln(Map<String, Object> options, Object object) {
highlight((CmdDesc)object).println(terminal());
} else if (object instanceof String || object instanceof Number) {
String str = object.toString();
highlightAndPrint(width, valueStyle, str, doValueHighlight(options, str));
SyntaxHighlighter highlighter = (SyntaxHighlighter)options.getOrDefault(Printer.VALUE_STYLE, null);
highlightAndPrint(width, highlighter, str, doValueHighlight(options, str));
} else {
highlightAndPrint(options, object);
}
Expand Down Expand Up @@ -382,7 +381,7 @@ private AttributedString highlight(CmdDesc cmdDesc) {

private SyntaxHighlighter valueHighlighter(String style) {
SyntaxHighlighter out;
if (style == null) {
if (style == null || style.isEmpty()) {
out = null;
} else if (style.matches("[a-z]+:.*")) {
out = SyntaxHighlighter.build(style);
Expand Down Expand Up @@ -441,7 +440,10 @@ private boolean doValueHighlight(Map<String,Object> options, String value) {
}

private void highlightAndPrint(int width, String style, String object, boolean doValueHighlight) {
SyntaxHighlighter highlighter = valueHighlighter(style);
highlightAndPrint(width, valueHighlighter(style), object, doValueHighlight);
}

private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
for (String s: object.split("\\r?\\n")) {
AttributedStringBuilder asb = new AttributedStringBuilder();
List<AttributedString> sas = asb.append(s).columnSplitLength(width);
Expand Down Expand Up @@ -741,8 +743,10 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
Object elem = collection.iterator().next();
if (elem instanceof Map) {
highlightMap(options, keysToString((Map<Object, Object>)elem), width);
} else if (canConvert(elem) && !options.containsKey(Printer.TO_STRING)){
} else if (canConvert(elem) && !options.containsKey(Printer.TO_STRING)) {
highlightMap(options, objectToMap(options, elem), width);
} else if (elem instanceof String && options.get(Printer.STYLE) != null) {
highlightAndPrint(width, (SyntaxHighlighter)options.get(Printer.STYLE), (String)elem, true);
} else {
highlightValue(options, null, objectToString(options, obj)).println(terminal());
}
Expand Down Expand Up @@ -969,6 +973,7 @@ private void highlightList(Map<String, Object> options
tabs.add(indent*depth + digits(collection.size()) + 2);
}
options.remove(Printer.MAX_COLUMN_WIDTH);
SyntaxHighlighter highlighter = depth == 0 ? (SyntaxHighlighter)options.get(Printer.STYLE) : null;
for (Object o : collection) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
if (depth > 0) {
Expand All @@ -979,7 +984,11 @@ private void highlightList(Map<String, Object> options
asb.append("\t");
row++;
}
asb.append(highlightValue(options, null, o));
if (highlighter != null && o instanceof String) {
asb.append(highlighter.highlight((String)o));
} else {
asb.append(highlightValue(options, null, o));
}
println(asb.columnSubSequence(0, width), maxrows);
}
}
Expand Down

0 comments on commit 91c205b

Please sign in to comment.