You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ld...@apache.org on 2011/03/02 12:38:04 UTC
svn commit: r1076175 - in /karaf/trunk: ./
shell/console/src/main/java/org/apache/karaf/shell/console/table/
Author: ldywicki
Date: Wed Mar 2 11:38:03 2011
New Revision: 1076175
URL: http://svn.apache.org/viewvc?rev=1076175&view=rev
Log:
[KARAF-374] Table formatting stuff
Added:
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Cell.java
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/table/DataTable.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/HAlign.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Row.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Style.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StyleCalculator.java
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/TableElement.java
Modified:
karaf/trunk/pom.xml
Modified: karaf/trunk/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/pom.xml?rev=1076175&r1=1076174&r2=1076175&view=diff
==============================================================================
--- karaf/trunk/pom.xml (original)
+++ karaf/trunk/pom.xml Wed Mar 2 11:38:03 2011
@@ -139,7 +139,7 @@
<aries.proxy.version>0.3</aries.proxy.version>
<aries.transaction.version>0.3</aries.transaction.version>
<aries.util.version>0.3</aries.util.version>
- <jansi.version>1.4</jansi.version>
+ <jansi.version>1.5-SNAPSHOT</jansi.version>
<jline.version>2.5</jline.version>
<jsw.version>3.2.3</jsw.version>
<log4j.version>1.2.16</log4j.version>
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Cell.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Cell.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Cell.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Cell.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,71 @@
+/*
+ * 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.table;
+
+/**
+ * Cell information.
+ *
+ * @author ldywicki
+ */
+public class Cell extends TableElement {
+
+ private String value;
+ private int colSpan;
+ private HAlign align;
+
+ public Cell(Object value) {
+ this(value, 0);
+ }
+
+ public Cell(Object value, HAlign align) {
+ this(value, align, 0);
+ }
+
+ public Cell(Object value, int colSpan) {
+ this(value, null, colSpan);
+ }
+
+ public Cell(Object value, HAlign align, int colSpan) {
+ this.value = value.toString();
+ this.colSpan = colSpan;
+ this.align = align;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setColSpan(int colSpan) {
+ this.colSpan = colSpan;
+ }
+
+ public int getColSpan() {
+ return colSpan;
+ }
+
+ public HAlign getAlign() {
+ return align;
+ }
+
+ public void setAlign(HAlign align) {
+ this.align = align;
+ }
+
+ @Override
+ public String toString() {
+ return "[Cell: " + value +"]";
+ }
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Column.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,121 @@
+/*
+ * 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.table;
+
+/**
+ * Column definition.
+ *
+ * @author ldywicki
+ */
+public class Column extends TableElement {
+
+ /**
+ * Column header.
+ */
+ private String header;
+
+ /**
+ * Preferred size.
+ */
+ private int size;
+
+ /**
+ * This flag allows to grow column when value in row is longer than initial column size. After growing 'size' will
+ * be increased.
+ */
+ private boolean mayGrow;
+
+ /**
+ * Default align.
+ */
+ private HAlign align = HAlign.left;
+
+ /**
+ * Optional element which allows to set style dynamically.
+ */
+ private StyleCalculator styleCalculator;
+
+ public Column(HAlign align) {
+ this.align = align;
+ }
+
+ public Column() {
+ }
+
+ public Column(int size, boolean mayGrow, HAlign align) {
+ this.size = size;
+ this.mayGrow = mayGrow;
+ this.align = align;
+ }
+
+ public Column(int size, boolean mayGrow) {
+ this.size = size;
+ this.mayGrow = mayGrow;
+ }
+
+ public Column(int size, HAlign align) {
+ this(size, false, align);
+ }
+
+ public Column(boolean mayGrow) {
+ this(0, true);
+ }
+
+ public Column(int size) {
+ this(size, false);
+ }
+
+ public String getHeader() {
+ return header;
+ }
+
+ public void setHeader(String header) {
+ this.header = header;
+ }
+
+ public int getSize() {
+ return size;
+ }
+
+ public void setSize(int size) {
+ this.size = size;
+ }
+
+ public boolean isMayGrow() {
+ return mayGrow;
+ }
+
+ public void setMayGrow(boolean mayGrow) {
+ this.mayGrow = mayGrow;
+ }
+
+ public HAlign getAlign() {
+ return align;
+ }
+
+ public void setAlign(HAlign align) {
+ this.align = align;
+ }
+
+ public StyleCalculator getStyleCalculator() {
+ return styleCalculator;
+ }
+
+ public void setStyleCalculator(StyleCalculator styleCalculator) {
+ this.styleCalculator = styleCalculator;
+ }
+
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/DataTable.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/DataTable.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/DataTable.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/DataTable.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,242 @@
+/*
+ * 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.table;
+
+import static org.apache.karaf.shell.console.table.StringUtil.*;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Simple data table.
+ *
+ * This class contains set of formatting code needed to print tabular data in console. There are two ways to construct
+ * table.
+ * 1) Provide output and number of columns
+ * 2) Provide output and manually configure columns
+ * First way is very fast and takes small amount of code, but you cannot configure column behavior. All columns will be
+ * expandable and aligned to left side.
+ * Second way takes more code but allows you to control column format.
+ *
+ * To avoid performance issues data table flushes output once per 10 rows. If you wish to change this - just set value
+ * for flushAfter field.
+ * Important notice - last column always don't have borders. It is designed to put mark last column as expandable and
+ * put there longer values.
+ *
+ * Code samples:
+ * <code>
+ * DataTable table = new DataTable(System.out);
+ *
+ * table.addColumn(new Column(3, true));
+ * table.addColumn(new Column(5));
+ * table.addColumn(new Column(12));
+ * table.addColumn(new Column(12));
+ * table.addColumn(new Column(5));
+ * table.addColumn(new Column(true));
+ *
+ * Row row = new Row();
+ * row.addCell(new Cell("OSGi", HAlign.center, 2));
+ * row.addCell(new Cell("Extender", HAlign.center, 2));
+ * row.addCell(new Cell("Misc", 2));
+ * table.addRow(row);
+ *
+ * row = new Row();
+ * row.addCell("ID");
+ * row.addCell("State");
+ * row.addCell("Spring");
+ * row.addCell("Blueprint");
+ * row.addCell("Level");
+ * row.addCell("Name");
+ * table.addRow(row);
+ *
+ * // load sample data
+ * for (int i = 0; i < 5; i++) {
+ * table.addRow(new Object[] {i, i, i, i, i, i});
+ * }
+ * table.flush();
+ * </code>
+ * And expected output:
+ * <code>
+ * [ OSGi ][ Extender ] Misc
+ * [ID ][State][Spring ][Blueprint ][Level] Name
+ * [0 ][0 ][0 ][0 ][0 ] 0
+ * [1 ][1 ][1 ][1 ][1 ] 1
+ * [2 ][2 ][2 ][2 ][2 ] 2
+ * [3 ][3 ][3 ][3 ][3 ] 3
+ * [4 ][4 ][4 ][4 ][4 ] 4
+ * </code>
+ *
+ * @author ldywicki
+ */
+public class DataTable extends TableElement {
+
+ /**
+ * Output destination.
+ */
+ private final Appendable target;
+
+ /**
+ * Table border style.
+ */
+ private Style borderStyle = new Style();
+
+ private List<Column> columns = new ArrayList<Column>();
+ private List<Row> rows = new ArrayList<Row>();
+
+ /**
+ * Number of rows to add before flushing rows to stream.
+ */
+ private int flushAfter = 10;
+
+ public DataTable(Appendable target) {
+ this.target = target;
+ }
+
+ public DataTable(PrintStream out, int colCount) {
+ this(out);
+
+ for (int i = 0; i < colCount; i++) {
+ addColumn(new Column(true));
+ }
+ }
+
+ public void addColumn(Column column) {
+ this.columns.add(column);
+ }
+
+ private void printBorder(Row row, String border) {
+ if (row.isBorders()) {
+ append(borderStyle.apply(border));
+ } else {
+ append(repeat(" ", border.length()));
+ }
+ }
+
+ public void flush() {
+ printRows();
+ rows.clear();
+ }
+
+ private void printRows() {
+ for (Row row : rows) {
+ List<Cell> cells = row.getCells();
+
+ for (int i = 0, colIndex = 0, size = cells.size(); i < size; i++) {
+ Cell cell = cells.get(i);
+ int colSpan = cell.getColSpan();
+
+ Column column = columns.get(colIndex);
+ int colSize = 0;
+
+ boolean first = i == 0;
+ boolean last = i + 1 == size;
+
+ if (colSpan > 1) {
+ for (int j = 0; j < colSpan; j++) {
+ colSize += columns.get(colIndex + j).getSize();
+ }
+ colSize += colSpan;
+ colIndex += colSpan;
+ } else {
+ colSize = column.getSize();
+ colIndex++;
+ }
+
+ Style style = calculateStyle(column, row, cell);
+
+ if (first) {
+ printBorder(row, "[");
+ } else if (!last) {
+ printBorder(row, "][");
+ } else {
+ printBorder(row, "] ");
+ }
+
+ String value = cell.getValue();
+
+ if (value.length() > colSize) {
+ if (column.isMayGrow()) {
+ column.setSize(value.length());
+ } else {
+ value = value.substring(value.length() - 2) + "..";
+ }
+ }
+
+ append(style.apply(calculateAlign(column, cell).position(value, colSize)));
+
+ }
+ append("\n");
+ }
+ }
+
+ private HAlign calculateAlign(Column column, Cell cell) {
+ if (cell.getAlign() != null) {
+ return cell.getAlign();
+ }
+ return column.getAlign();
+ }
+
+ private Style calculateStyle(Column column, Row row, Cell cell) {
+ StyleCalculator styleCalculator = column.getStyleCalculator();
+ Style dynamic = null;
+
+ if (styleCalculator != null) {
+ dynamic = styleCalculator.calculate(cell.getValue());
+ }
+
+ if (!cell.getStyle().isClean()) {
+ return cell.getStyle();
+ } else if (dynamic != null) {
+ return dynamic;
+ } else if (!row.getStyle().isClean()) {
+ return row.getStyle();
+ } else if (!column.getStyle().isClean()) {
+ return column.getStyle();
+ }
+ return new Style();
+ }
+
+ private void append(String string) {
+ try {
+ target.append(string);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addRow(Object[] row) {
+ addRow(new Row(row));
+ }
+
+ public void addRow(Row row) {
+ rows.add(row);
+
+ if ((rows.size() % flushAfter) == 0) {
+ printRows();
+ rows.clear();
+ }
+ }
+
+ public void setBorderStyle(Style style) {
+ this.borderStyle = style;
+ }
+
+ public void setFlushAfter(int flushAfter) {
+ this.flushAfter = flushAfter;
+ }
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/HAlign.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/HAlign.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/HAlign.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/HAlign.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,72 @@
+/*
+ * 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.table;
+
+import static org.apache.karaf.shell.console.table.StringUtil.*;
+
+/**
+ * Enumeration type which contains all possible horizontal alignments.
+ *
+ * @author ldywicki
+ */
+public enum HAlign {
+
+ /**
+ * Center align.
+ */
+ center {
+ @Override
+ public String position(String text, int colWidth) {
+ int width = colWidth - length(text);
+ text = repeat(" ", width / 2) + text + repeat(" ", width / 2);
+ if (length(text) < colWidth) {
+ // if colWidth is odd we add space at the end.
+ text += " ";
+ }
+ return text;
+ }
+ },
+
+ /**
+ * Left align.
+ */
+ left {
+ @Override
+ public String position(String text, int colWidth) {
+ return text + repeat(" ", colWidth - length(text));
+ }
+ },
+
+ /**
+ * Right align.
+ */
+ right {
+ @Override
+ public String position(String text, int colWidth) {
+ return repeat(" ", colWidth - length(text)) + text;
+ }
+ };
+
+ /**
+ * Calculate text position.
+ *
+ * @param text Text
+ * @param colWidth
+ * @return
+ */
+ public abstract String position(String text, int colWidth);
+
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Row.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Row.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Row.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Row.java Wed Mar 2 11:38:03 2011
@@ -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.table;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Row information.
+ *
+ * @author ldywicki
+ */
+public class Row extends TableElement {
+
+ /**
+ * List of cells.
+ */
+ private List<Cell> cells = new ArrayList<Cell>();
+
+ /**
+ * Add borders?
+ */
+ private boolean borders;
+
+ public Row(Object[] row) {
+ this(true);
+ for (Object object : row) {
+ addCell(new Cell(object));
+ }
+ }
+
+ public Row() {
+ this(true);
+ }
+
+ public Row(boolean borders) {
+ this.borders = borders;
+ }
+
+ public void addCell(Cell cell) {
+ cells.add(cell);
+ }
+
+ public List<Cell> getCells() {
+ return cells;
+ }
+
+ public boolean isBorders() {
+ return borders;
+ }
+
+ public void addCell(Object value) {
+ addCell(new Cell(value));
+ }
+
+}
Added: 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=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StringUtil.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,33 @@
+package org.apache.karaf.shell.console.table;
+
+public class StringUtil {
+
+ /**
+ * Returns length of the string.
+ *
+ * @param string String.
+ * @return Length.
+ */
+ public static int length(String string) {
+ return string == null ? 0 : string.length();
+ }
+
+ /**
+ * Utility method to repeat string.
+ *
+ * @param string String to repeat.
+ * @param times Number of times.
+ * @return Repeat string.
+ */
+ public static String repeat(String string, int times) {
+ if (times <= 0) {
+ return "";
+ }
+ else if (times % 2 == 0) {
+ return repeat(string+string, times/2);
+ }
+ else {
+ return string + repeat(string+string, times/2);
+ }
+ }
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Style.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Style.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Style.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/Style.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,160 @@
+/*
+ * 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.table;
+
+import static org.fusesource.jansi.Ansi.Attribute.*;
+
+import org.fusesource.jansi.Ansi;
+import org.fusesource.jansi.Ansi.Color;
+
+/**
+ * Class which covers {@link org.fusesource.jansi.Ansi} class with standard fluent-api.
+ *
+ * @author ldywicki
+ */
+public class Style {
+
+ /**
+ * We register all styles here.
+ */
+ private Ansi style = new Ansi();
+ private boolean clean = true;
+
+ /**
+ * Mark text as bold.
+ *
+ * @return Style.
+ */
+ public Style bold() {
+ unclean().a(INTENSITY_BOLD);
+ return this;
+ }
+
+ /**
+ * Mark text as italic.
+ *
+ * @return Style.
+ */
+ public Style italic() {
+ unclean().a(ITALIC);
+ return this;
+ }
+
+ /**
+ * Sets text color.
+ *
+ * @param c Color.
+ * @return Style.
+ */
+ public Style color(Color c) {
+ unclean().fg(c);
+ return this;
+ }
+
+ /**
+ * Sets background color.
+ *
+ * @param c Color.
+ * @return Style.
+ */
+ public Style background(Color c) {
+ unclean().bg(c);
+ return this;
+ }
+
+ /**
+ * Strike text.
+ *
+ * @return Style.
+ */
+ public Style strike() {
+ unclean().a(STRIKETHROUGH_ON);
+ return this;
+ }
+
+ /**
+ * Underline text.
+ *
+ * @return Style.
+ */
+ public Style underline() {
+ unclean().a(UNDERLINE);
+ return this;
+ }
+
+ /**
+ * Make text blink.
+ *
+ * @return Style.
+ */
+ public Style blink() {
+ unclean().a(BLINK_SLOW);
+ return this;
+ }
+
+ /**
+ * Change foreground with background.
+ *
+ * @return Style.
+ */
+ public Style inverse() {
+ unclean().a(NEGATIVE_ON);
+ return this;
+ }
+
+ /**
+ * Mark text as invisible;
+ *
+ * @return Style.
+ */
+ public Style invisible() {
+ unclean().a(NEGATIVE_ON);
+ return this;
+ }
+
+ /**
+ * Is any values was set?
+ *
+ * @return Boolean.
+ */
+ public boolean isClean() {
+ return clean;
+ }
+
+ /**
+ * Apply style to given text.
+ *
+ * @param text Text to stylish.
+ * @return Styled text - with ansi espace codes.
+ */
+ public String apply(String text) {
+ return new Ansi(style).a(text).reset().toString();
+ }
+
+ /**
+ * Sets dirty flag and return ansi object.
+ */
+ private Ansi unclean() {
+ clean = false;
+ return style;
+ }
+
+ @Override
+ public String toString() {
+ return isClean() ? "[no style]" : "[ansi: " + new Ansi(style).a("x").reset() + "]";
+ }
+
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StyleCalculator.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StyleCalculator.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StyleCalculator.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/StyleCalculator.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,27 @@
+/*
+ * 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.table;
+
+/**
+ * Dynamic style calculator.
+ *
+ * @author ldywicki
+ */
+public interface StyleCalculator {
+
+ Style calculate(String values);
+
+}
Added: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/TableElement.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/TableElement.java?rev=1076175&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/TableElement.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/table/TableElement.java Wed Mar 2 11:38:03 2011
@@ -0,0 +1,48 @@
+/*
+ * 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.table;
+
+/**
+ * Table element.
+ *
+ * @author ldywicki
+ */
+public class TableElement {
+
+ /**
+ * Style to apply.
+ */
+ private Style style = new Style();
+
+ public TableElement() {
+ super();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setStyle(Style style) {
+ this.style = style;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Style getStyle() {
+ return style;
+ }
+
+}
\ No newline at end of file