Skip to content

Commit

Permalink
prnt command: improved Map collection value printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 24, 2020
1 parent 070d32c commit c166189
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
44 changes: 35 additions & 9 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1440,22 +1440,33 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj

public List<AttributedString> highlightList(Map<String, Object> options, List<Object> collection, int width) {
List<AttributedString> out = new ArrayList<>();
highlightList(options, collection, width, 0, out);
return out;
}

public void highlightList(Map<String, Object> options
, List<Object> collection, int width, int depth, List<AttributedString> out) {
Integer row = 0;
Integer tabsize = digits(collection.size()) + 2;
int indent = (int)options.get(Printer.INDENTION);
int tabsize = indent*depth;
if (options.containsKey(Printer.ROWNUM)) {
tabsize += digits(collection.size()) + 2;
}
options.remove(Printer.MAX_COLUMN_WIDTH);
for (Object o : collection) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabsize);
if (options.containsKey(Printer.ROWNUM)) {
asb.append(row.toString(), AttributedStyle.DEFAULT
.foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
asb.append(":");
asb.append("\t");
row++;
}
if (tabsize != 0) {
asb.append("\t");
}
asb.append(highlightValue(options, null, objectToString(options, o)));
out.add(truncate(asb, width));
}
return out;
}

private boolean collectionObject(Object obj) {
Expand Down Expand Up @@ -1530,12 +1541,27 @@ private void highlightMap(Map<String, Object> options
, AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
Object elem = entry.getValue();
boolean convert = canConvert(elem);
if (depth < maxDepth && (elem instanceof Map || convert) && !options.containsKey(Printer.TO_STRING)) {
out.add(truncate(asb, width));
Map<String, Object> childMap = convert ? objectToMap(options, elem)
: keysToString((Map<Object, Object>) elem);
highlightMap(options, childMap, width, depth + 1, out);
} else {
boolean highlightValue = true;
if (depth < maxDepth && !options.containsKey(Printer.TO_STRING)) {
if (elem instanceof Map || convert) {
out.add(truncate(asb, width));
Map<String, Object> childMap = convert ? objectToMap(options, elem)
: keysToString((Map<Object, Object>) elem);
highlightMap(options, childMap, width, depth + 1, out);
highlightValue = false;
} else if (collectionObject(elem)) {
List<Object> collection = objectToList(elem);
if (!collection.isEmpty()) {
out.add(truncate(asb, width));
Map<String, Object> listOptions = new HashMap<>();
listOptions.putAll(options);
listOptions.put(Printer.TO_STRING, true);
highlightList(listOptions, collection, width, depth + 1, out);
highlightValue = false;
}
}
}
if (highlightValue) {
AttributedString val = highlightMapValue(mapOptions, entry.getKey(), map, defaultStyle);
if (map.size() == 1) {
asb.append("\t");
Expand Down
2 changes: 2 additions & 0 deletions groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public Object inspect(CommandInput input) {
Map<String,Object> options = new HashMap<>();
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
options.put(Printer.COLUMNS, ObjectInspector.METHOD_COLUMNS);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
printer.println(options, out);
return null;
}
Expand Down

0 comments on commit c166189

Please sign in to comment.