Skip to content

Commit

Permalink
DefaultPrinter: use method columnSubSequence() to truncate long lines…
Browse files Browse the repository at this point in the history
… and values
  • Loading branch information
mattirn committed Jan 8, 2021
1 parent 59d7e69 commit 977550b
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Expand Up @@ -597,7 +597,7 @@ private AttributedString highlightValue(Map<String, Object> options, String colu
private AttributedString truncateValue(Map<String, Object> options, AttributedString value) {
if (value.columnLength() > (int)options.getOrDefault(Printer.MAX_COLUMN_WIDTH, Integer.MAX_VALUE)) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(value.subSequence(0, (int)options.get(Printer.MAX_COLUMN_WIDTH) - 3));
asb.append(value.columnSubSequence(0, (int)options.get(Printer.MAX_COLUMN_WIDTH) - 3));
asb.append("...");
return asb.toAttributedString();
}
Expand Down Expand Up @@ -786,7 +786,7 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
asb.append("\t");
first = false;
}
truncate(asb, width).println(terminal());
asb.columnSubSequence(0, width).println(terminal());
int row = 0;
for (Object o : collection) {
AttributedStringBuilder asb2 = new AttributedStringBuilder().tabs(columns);
Expand All @@ -813,7 +813,7 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
asb2.append(v);
asb2.append("\t");
}
asb2.subSequence(0, width).println(terminal());
asb2.columnSubSequence(0, width).println(terminal());
}
} else if (collectionObject(elem) && !options.containsKey(Printer.TO_STRING)) {
List<Integer> columns = new ArrayList<>();
Expand Down Expand Up @@ -857,12 +857,13 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
asb.append(v);
asb.append("\t");
}
truncate(asb, width).println(terminal());
asb.columnSubSequence(0, width).println(terminal());
}
} else {
highlightList(options, collection, width);
}
} catch (Exception e) {
message = e.getMessage();
Log.debug("Stack: ", e);
highlightList(options, collection, width);
}
Expand Down Expand Up @@ -924,7 +925,7 @@ private void highlightList(Map<String, Object> options
row++;
}
asb.append(highlightValue(options, null, o));
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
}
}

Expand All @@ -942,10 +943,6 @@ private boolean canConvert(Object obj) {
&& !collectionObject(obj);
}

private AttributedString truncate(AttributedStringBuilder asb, int width) {
return asb.columnLength() > width ? asb.subSequence(0, width) : asb.toAttributedString();
}

private int digits(int number) {
if (number < 100) {
return number < 10 ? 1 : 2;
Expand Down Expand Up @@ -1007,14 +1004,14 @@ private void highlightMap(Map<String, Object> options
Map<String, Object> childMap = convert ? objectToMap(options, elem)
: keysToString((Map<Object, Object>) elem);
if (!childMap.isEmpty()) {
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
highlightMap(options, childMap, width, depth + 1);
highlightValue = false;
}
} else if (collectionObject(elem)) {
List<Object> collection = objectToList(elem);
if (!collection.isEmpty()) {
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
Map<String, Object> listOptions = new HashMap<>(options);
listOptions.put(Printer.TO_STRING, true);
highlightList(listOptions, collection, width, depth + 1);
Expand All @@ -1029,12 +1026,12 @@ private void highlightMap(Map<String, Object> options
if (val.contains('\n')) {
for (String v : val.toString().split("\\r?\\n")) {
asb.append(highlightValue(options, entry.getKey(), v));
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
}
} else {
asb.append(val);
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
}
} else {
if (val.contains('\n')) {
Expand All @@ -1043,7 +1040,7 @@ private void highlightMap(Map<String, Object> options
} else {
asb.append(val);
}
println(truncate(asb, width), maxrows);
println(asb.columnSubSequence(0, width), maxrows);
}
}
}
Expand Down

0 comments on commit 977550b

Please sign in to comment.