Skip to content

Commit

Permalink
PRNT_OPTIONS: add option valueStyleAll
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 30, 2021
1 parent 6ed371c commit e103deb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 8 additions & 1 deletion console/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,16 @@ enum TableRows {EVEN, ODD, ALL}
* Overrides the ScriptEngine toString() method.
*/
String OBJECT_TO_STRING = "objectToString";
/**
* Value: Boolean<br>
* Applies: MAP and TABLE<br>
* Highlight everything also strings with spaces
* DEFAULT: highlight only strings without spaces or enclosed by quotes or brackets
*/
String VALUE_STYLE_ALL = "valueStyleAll";

List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
, STRUCT_ON_TABLE, TO_STRING);
, STRUCT_ON_TABLE, TO_STRING, VALUE_STYLE_ALL);

default void println(Object object) {
println(new HashMap<>(), object);
Expand Down
23 changes: 11 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 @@ -320,20 +320,22 @@ private void internalPrintln(Map<String, Object> options, Object object) {
String style = (String) options.getOrDefault(Printer.STYLE, "");
int width = (int) options.get(Printer.WIDTH);
if (!style.isEmpty() && object instanceof String) {
highlightAndPrint(width, style, (String) object);
highlightAndPrint(width, style, (String) object, doValueHighlight(options, (String) object));
} else if (style.equalsIgnoreCase("JSON")) {
if (engine == null) {
throw new IllegalArgumentException("JSON style not supported!");
}
highlightAndPrint(width, style, engine.toJson(object));
String json = engine.toJson(object);
highlightAndPrint(width, style, json, doValueHighlight(options, json));
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
highlightAndPrint(options, object);
} else if (object instanceof Exception) {
highlightAndPrint(options, (Exception)object);
} else if (object instanceof CmdDesc) {
highlight((CmdDesc)object).println(terminal());
} else if (object instanceof String || object instanceof Number) {
highlightAndPrint(width, valueStyle, object.toString());
String str = object.toString();
highlightAndPrint(width, valueStyle, str, doValueHighlight(options, str));
} else {
highlightAndPrint(options, object);
}
Expand Down Expand Up @@ -405,10 +407,6 @@ private String truncate4nanorc(String obj) {
return val;
}

private AttributedString highlight(Integer width, SyntaxHighlighter highlighter, String object) {
return highlight(width, highlighter, object, isValue(object));
}

private AttributedString highlight(Integer width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
AttributedString out;
AttributedStringBuilder asb = new AttributedStringBuilder();
Expand All @@ -428,8 +426,9 @@ private AttributedString highlight(Integer width, SyntaxHighlighter highlighter,
return out;
}

private boolean isValue(String value) {
if(value.matches("\"(\\.|[^\"])*\"|'(\\.|[^'])*'")
private boolean doValueHighlight(Map<String,Object> options, String value) {
if (options.containsKey(Printer.VALUE_STYLE_ALL)
|| value.matches("\"(\\.|[^\"])*\"|'(\\.|[^'])*'")
|| (value.startsWith("[") && value.endsWith("]"))
|| (value.startsWith("(") && value.endsWith(")"))
|| (value.startsWith("{") && value.endsWith("}"))
Expand All @@ -441,9 +440,8 @@ private boolean isValue(String value) {
}
}

private void highlightAndPrint(int width, String style, String object) {
private void highlightAndPrint(int width, String style, String object, boolean doValueHighlight) {
SyntaxHighlighter highlighter = valueHighlighter(style);
boolean doValueHighlight = isValue(object);
for (String s: object.split("\\r?\\n")) {
AttributedStringBuilder asb = new AttributedStringBuilder();
List<AttributedString> sas = asb.append(s).columnSplitLength(width);
Expand Down Expand Up @@ -629,7 +627,8 @@ private AttributedString highlightValue(Map<String, Object> options, String colu
}
}
if (options.containsKey(Printer.VALUE_STYLE) && !isHighlighted(out)) {
out = highlight(null, (SyntaxHighlighter)options.get(Printer.VALUE_STYLE), out.toString());
out = highlight(null, (SyntaxHighlighter)options.get(Printer.VALUE_STYLE), out.toString()
, doValueHighlight(options, out.toString()));
}
return truncateValue(options, out);
}
Expand Down

0 comments on commit e103deb

Please sign in to comment.