karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [2/3] karaf git commit: [KARAF-3538] Support multiline values in ShellTable
Date Wed, 18 Feb 2015 08:44:27 GMT
[KARAF-3538] Support multiline values in ShellTable


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/def7c6d5
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/def7c6d5
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/def7c6d5

Branch: refs/heads/master
Commit: def7c6d5ccedc7d45d783e56cc87942baad96c51
Parents: 715a72a
Author: Guillaume Nodet <gnodet@gmail.com>
Authored: Tue Feb 17 18:27:16 2015 +0100
Committer: Guillaume Nodet <gnodet@gmail.com>
Committed: Wed Feb 18 09:39:46 2015 +0100

----------------------------------------------------------------------
 .../apache/karaf/shell/support/table/Col.java   | 82 +++++++++++++++++++-
 .../apache/karaf/shell/support/table/Row.java   | 35 +++++++--
 .../karaf/shell/support/table/ShellTable.java   |  7 +-
 .../shell/support/table/ShellTableTest.java     | 69 ++++++++++++++++
 4 files changed, 178 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/def7c6d5/shell/core/src/main/java/org/apache/karaf/shell/support/table/Col.java
----------------------------------------------------------------------
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 e3cc51d..95dba51 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
@@ -15,6 +15,14 @@
  */
 package org.apache.karaf.shell.support.table;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.karaf.shell.support.ansi.SimpleAnsi;
+
 /**
  * Column definition.
  */
@@ -32,7 +40,10 @@ public class Col {
     int maxSize = -1;
     
     int size = 0;
-    
+
+    boolean wrap;
+    boolean bold;
+
     /**
      * Alignment
      */
@@ -67,6 +78,24 @@ public class Col {
         return this;
     }
 
+    public Col wrap() {
+        return wrap(true);
+    }
+
+    public Col wrap(boolean wrap) {
+        this.wrap = wrap;
+        return this;
+    }
+
+    public Col bold() {
+        return bold(true);
+    }
+
+    public Col bold(boolean bold) {
+        this.bold = bold;
+        return this;
+    }
+
     public int getSize() {
         return size;
     }
@@ -89,7 +118,7 @@ public class Col {
         if (fullContent.length() == 0) {
             return "";
         }
-        String finalContent = fullContent.substring(0, getClippedSize(fullContent.length()));
+        String finalContent = cut(fullContent, getClippedSize(fullContent.length()));
         updateSize(finalContent.length());
         return finalContent;
     }
@@ -99,10 +128,30 @@ public class Col {
     }
 
     String getContent(String content) {
-        return this.align.position(cut(content, this.size), this.size);
+        List<String> lines = new ArrayList<>();
+        lines.addAll(Arrays.asList(content.split("\n")));
+        if (wrap) {
+            List<String> wrapped = new ArrayList<>();
+            for (String line : lines) {
+                wrapped.addAll(wrap(line));
+            }
+            lines = wrapped;
+        }
+        StringBuilder sb = new StringBuilder();
+        for (String line : lines) {
+            if (sb.length() > 0) {
+                sb.append("\n");
+            }
+            line = this.align.position(cut(line, size), this.size);
+            if (bold) {
+                line = SimpleAnsi.INTENSITY_BOLD + line + SimpleAnsi.INTENSITY_NORMAL;
+            }
+            sb.append(line);
+        }
+        return sb.toString();
     }
 
-    private String cut(String content, int size) {
+    protected String cut(String content, int size) {
         if (content.length() <= size) {
             return content;
         } else {
@@ -110,4 +159,29 @@ public class Col {
         }
     }
 
+    protected List<String> wrap(String str) {
+        List<String> result = new ArrayList<>();
+        Pattern wrap = Pattern.compile("(\\S\\S{" + size + ",}|.{1," + size + "})(\\s+|$)");
+        int cur = 0;
+        while (cur >= 0) {
+            int lst = str.indexOf('\n', cur);
+            String s = (lst >= 0) ? str.substring(cur, lst) : str.substring(cur);
+            if (s.length() == 0) {
+                result.add(s);
+            } else {
+                Matcher m = wrap.matcher(s);
+                while (m.find()) {
+                    result.add(m.group());
+                }
+            }
+            if (lst >= 0) {
+                cur = lst + 1;
+            } else {
+                break;
+            }
+        }
+        return result;
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/def7c6d5/shell/core/src/main/java/org/apache/karaf/shell/support/table/Row.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/table/Row.java b/shell/core/src/main/java/org/apache/karaf/shell/support/table/Row.java
index 771eb25..4c07f5b 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/table/Row.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/table/Row.java
@@ -49,20 +49,39 @@ public class Row {
     }
     
     String getContent(List<Col> cols, String separator) {
-        StringBuilder st = new StringBuilder();
-        int c = 0;
         if (cols.size() != content.size()) {
             throw new RuntimeException("Number of columns and number of content elements
do not match");
         }
 
-        for (Col col : cols) {
-            st.append(col.getContent(content.get(c)));
-            if (c + 1 < cols.size()) {
-                st.append(separator);
+        List<String[]> contents = new ArrayList<>();
+        int lines = 0;
+        for (int col = 0; col < cols.size(); col++) {
+            String[] cnt = cols.get(col).getContent(content.get(col)).split("\n");
+            lines = Math.max(lines, cnt.length);
+            contents.add(cnt);
+        }
+        StringBuilder st = new StringBuilder();
+        for (int line = 0; line < lines; line++) {
+            if (line > 0) {
+                st.append("\n");
             }
-            c++;
+            StringBuilder st2 = new StringBuilder();
+            for (int col = 0; col < cols.size(); col++) {
+                String[] strings = contents.get(col);
+                if (col > 0) {
+                    st2.append(separator);
+                }
+                if (line < strings.length) {
+                    st2.append(strings[line]);
+                } else {
+                    st2.append(StringUtil.repeat(" ", cols.get(col).getSize()));
+                }
+            }
+            while (st2.charAt(st2.length() - 1) == ' ') {
+                st2.setLength(st2.length() - 1);
+            }
+            st.append(st2);
         }
-
         return st.toString();
     }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/def7c6d5/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
b/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
index 257014f..2cb6899 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
@@ -90,7 +90,7 @@ public class ShellTable {
         }
 
         if (size > 0) {
-            tryGrowToMaxSize();
+            adjustSize();
         }
 
         if (format && showHeaders) {
@@ -117,7 +117,7 @@ public class ShellTable {
         }
     }
 
-    private void tryGrowToMaxSize() {
+    private void adjustSize() {
         int currentSize = 0;
         for (Col col : cols) {
             currentSize += col.size + separator.length();
@@ -125,7 +125,8 @@ public class ShellTable {
         currentSize -= separator.length();
         int sizeToGrow = size - currentSize;
 
-        for (Col col : cols) {
+        for (int i = cols.size() - 1; i >= 0; i--) {
+            Col col = cols.get(i);
             if (col.maxSize == -1) {
                 col.size = Math.max(0, col.size + sizeToGrow);
                 return;

http://git-wip-us.apache.org/repos/asf/karaf/blob/def7c6d5/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
----------------------------------------------------------------------
diff --git a/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
b/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
new file mode 100644
index 0000000..7a250ca
--- /dev/null
+++ b/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.karaf.shell.support.table;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ShellTableTest {
+
+    @Test
+    public void testLongValueFull() {
+        ShellTable table = new ShellTable();
+        table.separator("|");
+        table.column("col1");
+        table.column("col2").maxSize(-1);
+        table.addRow().addContent("my first column value", "my second column value is quite
long");
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        table.print(new PrintStream(baos), false);
+        assertEquals("my first column value|my second column value is quite long\n", baos.toString());
+    }
+
+    @Test
+    public void testLongValueCut() {
+        ShellTable table = new ShellTable();
+        table.separator("|");
+        table.column("col1");
+        table.column("col2").maxSize(-1);
+        table.addRow().addContent("my first column value", "my second column value is quite
long");
+        table.size(50);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        table.print(new PrintStream(baos), false);
+        assertEquals("my first column value|my second column value is q\n", baos.toString());
+    }
+
+    @Test
+    public void testLongValueMultiline() {
+        ShellTable table = new ShellTable();
+        table.separator("|");
+        table.column("col1");
+        table.column("col2").maxSize(-1).wrap();
+        table.addRow().addContent("my first column value", "my second column value is quite
long");
+        table.size(50);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        table.print(new PrintStream(baos), false);
+        assertEquals("my first column value|my second column value is\n" +
+                "                     |quite long\n", baos.toString());
+    }
+
+}


Mime
View raw message