Skip to content

Commit

Permalink
DefaultPrinter: improve printing string with line breaks, #909
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 18, 2024
1 parent 7ff2fbc commit 7dddf76
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2023, the original author(s).
* Copyright (c) 2002-2024, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -466,18 +466,41 @@ private boolean doValueHighlight(Map<String, Object> options, String value) {
}

private void highlightAndPrint(
int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight, int maxrows) {
String[] rows = object.split("\\r?\\n", maxrows);
int lastRowIdx = rows.length == maxrows ? rows.length - 1 : rows.length;
for (int i = 0; i < lastRowIdx; i++) {
AttributedStringBuilder asb = new AttributedStringBuilder();
List<AttributedString> sas = asb.append(rows[i]).columnSplitLength(width);
for (AttributedString as : sas) {
highlight(width, highlighter, as.toString(), doValueHighlight).println(terminal());
int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight, int maxRows) {
String lineBreak = null;
if (object.indexOf("\r\n") >= 0) {
lineBreak = "\r\n";
} else if (object.indexOf("\n") >= 0) {
lineBreak = "\n";
} else if (object.indexOf("\r") >= 0) {
lineBreak = "\r";
}
if (lineBreak == null) {
highlightAndPrint(width, highlighter, object, doValueHighlight);
} else {
int rows = 0;
int i0 = 0;
while (rows < maxRows) {
rows++;
int i1 = object.indexOf(lineBreak, i0);
String line = i1 >= 0 ? object.substring(i0, i1) : object.substring(i0);
highlightAndPrint(width, highlighter, line, doValueHighlight);
if (i1 < 0) {
break;
}
i0 = i1 + lineBreak.length();
}
if (rows == maxRows) {
throw new TruncatedOutputException("Truncated output: " + maxRows);
}
}
if (rows.length == maxrows) {
throw new TruncatedOutputException("Truncated output: " + maxrows);
}

private void highlightAndPrint(int width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
AttributedStringBuilder asb = new AttributedStringBuilder();
List<AttributedString> sas = asb.append(object).columnSplitLength(width);
for (AttributedString as : sas) {
highlight(width, highlighter, as.toString(), doValueHighlight).println(terminal());
}
}

Expand Down

0 comments on commit 7dddf76

Please sign in to comment.