This is an automated email from the ASF dual-hosted git repository. jbonofre pushed a commit to branch karaf-4.3.x in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/karaf-4.3.x by this push: new bd812bd0f9 [KARAF-7678] Fix ShellTable multiline clipping bd812bd0f9 is described below commit bd812bd0f9aab0fd3e02b76a55837276a59ddd6a Author: Ciprian Ciubotariu <cheepe...@gmx.net> AuthorDate: Tue Feb 28 02:27:03 2023 +0200 [KARAF-7678] Fix ShellTable multiline clipping Account for multi-line cell contents when calculating cell width and clipping the text to maxSize. The cell contents might be either wrapped or originally split by the caller with \n characters (cherry picked from commit 78222c0ae567b5b504327cbce8cbabd0904e6edf) --- .../org/apache/karaf/shell/support/table/Col.java | 44 ++++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/table/Col.java b/shell/core/src/main/java/org/apache/karaf/shell/support/table/Col.java index 410de81283..442e94da11 100644 --- a/shell/core/src/main/java/org/apache/karaf/shell/support/table/Col.java +++ b/shell/core/src/main/java/org/apache/karaf/shell/support/table/Col.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.apache.karaf.shell.support.ansi.SimpleAnsi; @@ -148,8 +149,14 @@ public class Col { if (fullContent.length() == 0) { return ""; } - String finalContent = cut(fullContent, getClippedSize(fullContent.length())); - updateSize(finalContent.length()); + if (wrap && size < fullContent.length()) { + // make sure splitLines will have an estimate cell size if wrap is true + updateSize(fullContent.length()); + } + List<String> lines = splitLines(fullContent); + int maxLineSize = lines.stream().mapToInt(String::length).max().getAsInt(); // at least one line exists due to test above + updateSize(maxLineSize); // calls getClippedSize() + String finalContent = lines.stream().map(line -> cut(line, getClippedSize(line.length()))).collect(Collectors.joining("\n")); return finalContent; } @@ -158,14 +165,7 @@ public class Col { } String getContent(String content) { - List<String> lines = new ArrayList<>(Arrays.asList(content.split("\n"))); - if (wrap) { - List<String> wrapped = new ArrayList<>(); - for (String line : lines) { - wrapped.addAll(wrap(line)); - } - lines = wrapped; - } + List<String> lines = splitLines(content); String color = null; if(colorProvider != null) { @@ -182,17 +182,29 @@ public class Col { line = SimpleAnsi.INTENSITY_BOLD + line + SimpleAnsi.INTENSITY_NORMAL; } - if(color != null) - sb.append(color); - + if (color != null) + sb.append(color); + sb.append(line); - - if(color != null) - sb.append(SimpleAnsi.COLOR_DEFAULT); + + if (color != null) + sb.append(SimpleAnsi.COLOR_DEFAULT); } return sb.toString(); } + private List<String> splitLines(String content) { + List<String> lines = new ArrayList<>(Arrays.asList(content.split("\n"))); + if (wrap) { + List<String> wrapped = new ArrayList<>(); + for (String line : lines) { + wrapped.addAll(wrap(line)); + } + lines = wrapped; + } + return lines; + } + protected String cut(String content, int size) { if (content.length() <= size) { return content;