Skip to content

Commit

Permalink
prnt command: reviewed map similarity comparison and value highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 31, 2020
1 parent f6e3c08 commit f0d7f23
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,14 @@ private Map<String,Object> objectToMap(Map<String, Object> options, Object obj)
private String objectToString(Map<String, Object> options, Object obj) {
Map<Class<?>, Object> toString = options.containsKey("objectToString") ? (Map<Class<?>, Object>)options.get("objectToString")
: new HashMap<>();
if (obj == null) {
return "null";
} else if (toString.containsKey(obj.getClass())) {
return (String)engine.execute(toString.get(obj.getClass()), obj);
} else if (objectToString.containsKey(obj.getClass())) {
return objectToString.get(obj.getClass()).apply(obj);
} else if (obj instanceof Class) {
return ((Class<?>)obj).getName();
if (obj != null) {
if (toString.containsKey(obj.getClass())) {
return (String) engine.execute(toString.get(obj.getClass()), obj);
} else if (objectToString.containsKey(obj.getClass())) {
return objectToString.get(obj.getClass()).apply(obj);
} else if (obj instanceof Class) {
return ((Class<?>) obj).getName();
}
}
return engine.toString(obj);
}
Expand All @@ -1115,19 +1115,23 @@ private AttributedString highlightMapValue(Map<String,Object> options
return highlightValue(options, key, mapValue(options, key, map), defaultStyle);
}

private boolean isHighlighted(AttributedString value) {
for (int i = 0; i < value.columnLength(); i++) {
if (value.styleAt(i).getStyle() != AttributedStyle.DEFAULT.getStyle()) {
return true;
}
}
return false;
}

private AttributedString highlightValue(Map<String, Object> options
, String column
, Object obj, AttributedStyle defaultStyle) {
AttributedString out = highlightValue(options, column, obj);
boolean doDefault = true;
if (defaultStyle != null) {
if (simpleObject(obj)) {
for (int i = 0; i < out.columnLength(); i++) {
if (out.styleAt(i).getStyle() != AttributedStyle.DEFAULT.getStyle()) {
doDefault = false;
break;
}
}
doDefault = !isHighlighted(out);
}
} else {
doDefault = false;
Expand All @@ -1138,7 +1142,7 @@ private AttributedString highlightValue(Map<String, Object> options
@SuppressWarnings("unchecked")
private AttributedString highlightValue(Map<String, Object> options, String column, Object obj) {
AttributedString out = null;
Object raw = options.containsKey("toString") ? objectToString(options, obj) : obj;
Object raw = options.containsKey("toString") && obj != null ? objectToString(options, obj) : obj;
Map<String, Object> hv = options.containsKey("highlightValue") ? (Map<String, Object>)options.get("highlightValue")
: new HashMap<>();
if (column != null && simpleObject(raw)) {
Expand All @@ -1164,10 +1168,12 @@ private AttributedString highlightValue(Map<String, Object> options, String colu
out = new AttributedString(columnValue(objectToString(options, raw)));
}
}
if (simpleObject(raw)) {
if ((simpleObject(raw) || raw == null) && (hv.containsKey("*") || highlightValue.containsKey("*"))
&& !isHighlighted(out)) {
if (hv.containsKey("*")) {
out = (AttributedString) engine.execute(hv.get("*"), out);
} else if (highlightValue.containsKey("*")) {
}
if (highlightValue.containsKey("*")) {
out = highlightValue.get("*").apply(out);
}
}
Expand Down Expand Up @@ -1213,22 +1219,16 @@ private List<Object> objectToList(Object obj) {
return out;
}

private boolean similarSets(Set<String> c1, Set<String> c2, double threshold) {
boolean out;
if (c1.size() > c2.size()) {
out = c1.containsAll(c2);
} else {
out = c2.containsAll(c1);
}
private boolean similarSets(Set<String> ref, Set<String> c2, double threshold) {
boolean out = c2.containsAll(ref);
if (!out) {
int matches = 0;
for (String s : c1.size() > c2.size() ? c1 : c2) {
if (c1.size() > c2.size() ? c2.contains(s) : c1.contains(s)) {
for (String s : ref) {
if (c2.contains(s)) {
matches += 1;
}
}
int max = c1.size() > c2.size() ? c1.size() : c2.size();
double r = (1.0*matches)/max;
double r = (1.0*matches)/ref.size();
out = r > threshold;
}
return out;
Expand Down Expand Up @@ -1281,7 +1281,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
List<String> header = new ArrayList<>();
List<Integer> columns = new ArrayList<>();
int headerWidth = 0;
Set<String> refKeys = map.keySet();
Set<String> refKeys = new HashSet<>();
for (int i = 0; i < _header.size(); i++) {
if (!map.containsKey(_header.get(i).split("\\.")[0])) {
continue;
Expand All @@ -1294,6 +1294,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
continue;
}
}
refKeys.add(_header.get(i).split("\\.")[0]);
header.add(_header.get(i));
columns.add(_header.get(i).length() + 1);
headerWidth += _header.get(i).length() + 1;
Expand All @@ -1309,13 +1310,10 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
mapSimilarity = ((java.math.BigDecimal)options.get("mapSimilarity")).doubleValue();
}
for (Object o : collection) {
if (o.getClass() != elem.getClass()) {
throw new Exception("Not homogenous object list!");
}
Map<String, Object> m = convert ? objectToMap(options, o)
: keysToString((Map<Object, Object>) o);
if (o instanceof Map && !similarSets(m.keySet(), refKeys, mapSimilarity)) {
throw new Exception("Not homogenous map list!");
if (o instanceof Map && !similarSets(refKeys, m.keySet(), mapSimilarity)) {
throw new Exception("Not homogenous list!");
}
for (int i = 0; i < header.size(); i++) {
int cw = highlightMapValue(options, header.get(i), m).columnLength();
Expand Down

0 comments on commit f0d7f23

Please sign in to comment.