Skip to content

Commit

Permalink
prnt command: improved map printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 13, 2020
1 parent 4523408 commit 9430ba0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
43 changes: 27 additions & 16 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ private Map<String,Object> keysToString(Map<Object,Object> map) {
}

@SuppressWarnings("unchecked")
private Object mapValue(String key, Map<String,Object> map) {
private Object mapValue(Map<String, Object> options, String key, Map<String,Object> map) {
String[] keys = key.split("\\.");
Object out = map.get(keys[0]);
if (keys.length > 1) {
Expand All @@ -968,6 +968,9 @@ private Object mapValue(String key, Map<String,Object> map) {
Map<String,Object> m = keysToString((Map<Object,Object>)out);
out = m.get(keys[i]);
} else if (out == null) {
if (i == 1) {
out = map.get(key);
}
break;
} else if (canConvert(out)) {
out = engine.toMap(out).get(keys[i]);
Expand All @@ -976,6 +979,9 @@ private Object mapValue(String key, Map<String,Object> map) {
}
}
}
if (!(out instanceof Map) && canConvert(out)){
out = objectToMap(options, out);
}
return out;
}

Expand Down Expand Up @@ -1145,7 +1151,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
if (options.containsKey("columns")) {
// do nothing
} else if (!options.containsKey("structsOnTable")) {
Object val = mapValue(_header.get(i), map);
Object val = mapValue(options, _header.get(i), map);
if (val == null || !simpleObject(val)) {
continue;
}
Expand All @@ -1161,7 +1167,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
for (int i = 0; i < header.size(); i++) {
Map<String, Object> m = convert ? objectToMap(options, o) : keysToString((Map<Object, Object>)o);
if (engine.toString(m.get(header.get(i))).length() > columns.get(i) - 1) {
columns.set(i, highlightValue(options, header.get(i), mapValue(header.get(i), m)).columnLength() + 1);
columns.set(i, highlightValue(options, header.get(i), mapValue(options, header.get(i), m)).columnLength() + 1);
}
}
}
Expand All @@ -1188,7 +1194,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
}
for (int i = 0; i < header.size(); i++) {
Map<String, Object> m = convert ? objectToMap(options, o) : keysToString((Map<Object, Object>)o);
AttributedString v = highlightValue(options, header.get(i), mapValue(header.get(i), m));
AttributedString v = highlightValue(options, header.get(i), mapValue(options, header.get(i), m));
if (isNumber(v.toString())) {
v = addPadding(v, columns.get(firstColumn + i + 1) - columns.get(firstColumn + i) - 1);
}
Expand Down Expand Up @@ -1264,11 +1270,11 @@ private boolean collectionObject(Object obj) {

private boolean simpleObject(Object obj) {
return obj instanceof Number || obj instanceof String || obj instanceof Date || obj instanceof File
|| obj instanceof Boolean || obj instanceof Enum;
|| obj instanceof Boolean || obj instanceof Enum || obj instanceof Class;
}

private boolean canConvert(Object obj) {
if (simpleObject(obj) || collectionObject(obj)) {
if (obj == null || simpleObject(obj) || collectionObject(obj)) {
return false;
}
return true;
Expand All @@ -1289,25 +1295,30 @@ private void toTabStops(List<Integer> columns, boolean rownum) {

private List<AttributedString> highlightMap(Map<String, Object> options, Map<String, Object> map, int width) {
List<AttributedString> out = new ArrayList<>();
AttributedStyle defaultStyle = AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW);
int max = map.keySet().stream().map(String::length).max(Integer::compareTo).get();
for (Map.Entry<String, Object> entry : map.entrySet()) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
asb.append(entry.getKey(), AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
AttributedString val = highlightValue(options, entry.getKey(), mapValue(options, entry.getKey(), map), defaultStyle);
if (map.size() == 1) {
for (String v : engine.toString(entry.getValue()).split("\\r?\\n")) {
asb.append("\t");
asb.append(v, AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW));
out.add(truncate(asb, width));
asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
asb.append("\t");
if (val.contains('\n')) {
for (String v : val.toString().split("\\r?\\n")) {
asb.append(v, defaultStyle);
out.add(truncate(asb, width));
asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
}
} else {
asb.append(val);
out.add(truncate(asb, width));
}
} else {
AttributedStyle defaultStyle = AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW);
AttributedString v = highlightValue(options, entry.getKey(), entry.getValue(), defaultStyle);
if (v.contains('\n')) {
v = new AttributedString(Arrays.asList(v.toString().split("\\r?\\n")).toString(), defaultStyle);
if (val.contains('\n')) {
val = new AttributedString(Arrays.asList(val.toString().split("\\r?\\n")).toString(), defaultStyle);
}
asb.append("\t");
asb.append(v);
asb.append(val);
out.add(truncate(asb, width));
}
}
Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _reader2map(reader){
out['othersGroupName'] = reader.othersGroupName
out['keyMap'] = reader.keyMap
out['autosuggestion'] = reader.autosuggestion
out['terminalDumb'] = reader.terminalDumb
out['terminal.kind'] = reader.terminal.kind
out
}

Expand All @@ -35,7 +35,7 @@ def _reader2string(reader){
out += 'othersGroupName:' + reader.othersGroupName + ', '
out += 'keyMap:' + reader.keyMap + ', '
out += 'autosuggestion:' + reader.autosuggestion + ', '
out += 'terminalDumb:' + reader.terminalDumb
out += 'terminal.kind:' + reader.terminal.kind
out += ']'
out
}
Expand Down
8 changes: 7 additions & 1 deletion groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public class Utils {
}

static Map<String,Object> toMap(Object object) {
object != null ? object.properties : null
def out = [:]
if (object instanceof Closure) {
out['closure'] = object.getClass().getName()
} else {
out = object != null ? object.properties : null
}
out
}

static String toJson(Object object) {
Expand Down

0 comments on commit 9430ba0

Please sign in to comment.