Skip to content

Commit

Permalink
prnt command: added more checks in table print decision
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 27, 2020
1 parent 38a909c commit 8467b07
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,27 @@ 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);
}
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)) {
matches += 1;
}
}
int max = c1.size() > c2.size() ? c1.size() : c2.size();
double r = (1.0*matches)/max;
out = r > threshold;
}
return out;
}

@SuppressWarnings("unchecked")
private List<AttributedString> highlight(Map<String, Object> options, Object obj) {
List<AttributedString> out = new ArrayList<>();
Expand Down Expand Up @@ -1239,6 +1260,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();
for (int i = 0; i < _header.size(); i++) {
if (!map.containsKey(_header.get(i).split("\\.")[0])) {
continue;
Expand All @@ -1258,13 +1280,23 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
break;
}
}
if (header.size() == 0) {
throw new Exception("No columns for table!");
}
double mapSimilarity = 0.8;
if (options.containsKey("mapSimilarity")) {
mapSimilarity = ((java.math.BigDecimal)options.get("mapSimilarity")).doubleValue();
}
for (Object o : collection) {
if (o.getClass() != elem.getClass()) {
throw new Exception();
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!");
}
for (int i = 0; i < header.size(); i++) {
Map<String, Object> m = convert ? objectToMap(options, o)
: keysToString((Map<Object, Object>) o);
int cw = highlightMapValue(options, header.get(i), m).columnLength();
if (cw > columns.get(i) - 1) {
columns.set(i, cw + 1);
Expand Down Expand Up @@ -1295,9 +1327,9 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
asb2.append("\t");
row++;
}
Map<String, Object> m = convert ? objectToMap(options, o)
: keysToString((Map<Object, Object>) o);
for (int i = 0; i < header.size(); i++) {
Map<String, Object> m = convert ? objectToMap(options, o)
: keysToString((Map<Object, Object>) o);
AttributedString v = highlightMapValue(options, header.get(i), m);
if (isNumber(v.toString())) {
v = addPadding(v,
Expand Down Expand Up @@ -1367,7 +1399,8 @@ 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<>();
Integer row = 0;
Integer tabsize = ((Integer) collection.size()).toString().length() + 1;
Integer tabsize = digits(collection.size()) + 2;
options.remove("maxColumnWidth");
for (Object o : collection) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabsize);
if (options.containsKey("rownum")) {
Expand Down

0 comments on commit 8467b07

Please sign in to comment.