You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2012/03/23 13:31:07 UTC

svn commit: r1304314 - in /karaf/trunk/shell/console/src: main/java/org/apache/karaf/shell/console/table/ main/java/org/apache/karaf/shell/console/table2/ test/java/org/apache/karaf/shell/console/tabletest/

Author: cschneider
Date: Fri Mar 23 12:31:07 2012
New Revision: 1304314

URL: http://svn.apache.org/viewvc?rev=1304314&view=rev
Log:
KARAF-1281 Add a new ShellTable impl

Added:
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java
      - copied unchanged from r1303469, karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Cell.java
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Col.java
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Row.java
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/ShellTable.java
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/ShellTableTest.java
Modified:
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java

Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java?rev=1304314&r1=1304313&r2=1304314&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java Fri Mar 23 12:31:07 2012
@@ -15,7 +15,7 @@
  */
 package org.apache.karaf.shell.console.table;
 
-public class StringUtil {
+class StringUtil {
 
     /**
      * Returns length of the string.

Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Cell.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Cell.java?rev=1304314&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Cell.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Cell.java Fri Mar 23 12:31:07 2012
@@ -0,0 +1,43 @@
+/*
+ * 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.console.table2;
+
+import java.io.PrintStream;
+
+
+public class Cell {
+    private Object content;
+    private boolean isHeader;
+    private Col col;
+    
+    public Cell(Object content, boolean isHeader, Col col) {
+        this.isHeader = isHeader;
+        this.content = content;
+        this.col = col;
+    }
+
+    public boolean isHeader() {
+        return isHeader;
+    }
+
+    public Object getContent() {
+        return content;
+    }
+    
+    public void print(PrintStream out, int size) {
+        out.print("");
+    }
+}

Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Col.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Col.java?rev=1304314&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Col.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Col.java Fri Mar 23 12:31:07 2012
@@ -0,0 +1,115 @@
+/*
+ * 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.console.table2;
+
+import java.text.Format;
+
+import org.apache.karaf.shell.console.table.HAlign;
+
+/**
+ * Column definition.
+ */
+public class Col {
+
+    /**
+     * Column header.
+     */
+    private String header;
+
+    /**
+     * Maximum size of this column. The default -1 means the column
+     * may grow indefinately
+     */
+    private int maxSize = -1;
+    
+    private int size = 0;
+
+    
+    /**
+     * Alignment
+     */
+    private HAlign align = HAlign.left;
+    
+    public Col(String header) {
+        this.header = header;
+    }
+
+    public Col align(HAlign align) {
+        this.align = align;
+        return this;
+    }
+    
+    public Col alignLeft() {
+        this.align = HAlign.left;
+        return this;
+    }
+    
+    public Col alignRight() {
+        this.align = HAlign.right;
+        return this;
+    }
+    
+    public Col alignCenter() {
+        this.align = HAlign.center;
+        return this;
+    }
+    
+    public Col maxSize(int maxSize) {
+        this.maxSize = maxSize;
+        return this;
+    }
+    
+    protected void updateSize(int cellSize) {
+        if (this.size <= cellSize) {
+            this.size = getClippedSize(cellSize);
+        }
+    }
+    
+    private int getClippedSize(int cellSize) {
+        return this.maxSize == -1 ? cellSize : Math.min(cellSize, this.maxSize);
+    }
+
+    String format(Object cellData) {
+        String fullContent = String.format("%s", cellData);
+        if (fullContent.length() == 0) {
+            return "";
+        }
+        String finalContent = fullContent.substring(0, getClippedSize(fullContent.length()));
+        updateSize(finalContent.length());
+        return finalContent;
+    }
+
+    String getHeader() {
+        return header;
+    }
+
+    int getMaxSize() {
+        return maxSize;
+    }
+
+    Format getFormat() {
+        return null;
+    }
+
+    int getSize() {
+        return this.size;
+    }
+
+    String getContent(String content) {
+        return this.align.position(content, this.size);
+    }
+
+}

Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Row.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Row.java?rev=1304314&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Row.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/Row.java Fri Mar 23 12:31:07 2012
@@ -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.console.table2;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class Row {
+    private List<Object> data;
+    private List<String> content;
+    
+    Row() {
+        data = new ArrayList<Object>();
+        content = new ArrayList<String>();
+    }
+    
+    Row(List<Col> cols) {
+        this();
+        for (Col col : cols) {
+            data.add(col.getHeader());
+        }
+    }
+
+    public void addContent(Object ... cellDataAr) {
+        data.addAll(Arrays.asList(cellDataAr));
+    }
+    
+    void formatContent(List<Col> cols) {
+        content.clear();
+        int c = 0;
+        for (Col col : cols) {
+            content.add(col.format(data.get(c)));
+            c++;
+        }
+    }
+    
+    String getContent(List<Col> cols) {
+        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");
+        }
+        st.append("| ");
+        for (Col col : cols) {
+            st.append(col.getContent(content.get(c)));
+            if (c + 1 < cols.size()) {
+                st.append(" | ");
+            }
+            c++;
+        }
+        st.append(" |");
+        return st.toString();
+    }
+
+}

Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/ShellTable.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/ShellTable.java?rev=1304314&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/ShellTable.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table2/ShellTable.java Fri Mar 23 12:31:07 2012
@@ -0,0 +1,64 @@
+/*
+ * 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.console.table2;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class ShellTable {
+    private List<Col> cols = new ArrayList<Col>();
+    private List<Row> rows = new ArrayList<Row>();
+    
+    public ShellTable() {
+        
+    }
+    
+    public ShellTable column(Col colunmn) {
+        cols.add(colunmn);
+        return this;
+    }
+    
+    public Row addRow() {
+        Row row = new Row();
+        rows.add(row);
+        return row;
+    }
+    
+    public void print(PrintStream out)  {
+        Row headerRow = new Row(cols);
+        headerRow.formatContent(cols);
+        for (Row row : rows) {
+            row.formatContent(cols);
+        }
+
+        String headerLine = headerRow.getContent(cols);
+        out.println(headerLine);
+        out.println(underline(headerLine.length()));
+        for (Row row : rows) {
+            out.println(row.getContent(cols));
+        }
+    }
+
+    private String underline(int length) {
+        char[] exmarks = new char[length];
+        Arrays.fill(exmarks, '-');
+        return new String(exmarks);
+    }
+
+}

Added: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/ShellTableTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/ShellTableTest.java?rev=1304314&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/ShellTableTest.java (added)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/tabletest/ShellTableTest.java Fri Mar 23 12:31:07 2012
@@ -0,0 +1,42 @@
+/*
+ * 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.console.tabletest;
+
+import org.apache.karaf.shell.console.table2.Col;
+import org.apache.karaf.shell.console.table2.Row;
+import org.apache.karaf.shell.console.table2.ShellTable;
+import org.junit.Test;
+
+public class ShellTableTest {
+
+    @Test
+    public void testTable() {
+        ShellTable table = new ShellTable();
+        table.column(new Col("id").alignRight().maxSize(5));
+        table.column(new Col("Name").maxSize(20));
+        table.column(new Col("Centered").alignCenter());
+        
+        table.addRow().addContent(1, "Test", "Description");
+        table.addRow().addContent(20, "My name", "Description");
+        
+        Row row = table.addRow();
+        row.addContent(123456789);
+        row.addContent("A very long text that should be cut");
+        row.addContent("A very long text that should not be cut");
+        
+        table.print(System.out);
+    }
+}