You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2018/11/02 12:59:55 UTC
[karaf] branch master updated: Updated Col used by ShellTable.
Added support for changing color of a specific cell in a column based on
the cell content. (#638)
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/master by this push:
new f4bb14c Updated Col used by ShellTable. Added support for changing color of a specific cell in a column based on the cell content. (#638)
f4bb14c is described below
commit f4bb14cc8a273b0c49ec18af126db73325850ad6
Author: labero <jo...@gmail.com>
AuthorDate: Fri Nov 2 13:59:50 2018 +0100
Updated Col used by ShellTable. Added support for changing color of a specific cell in a column based on the cell content. (#638)
Add color support in ShellTable
---
.../karaf/shell/support/ansi/SimpleAnsi.java | 3 ++
.../org/apache/karaf/shell/support/table/Col.java | 43 ++++++++++++++++++----
.../karaf/shell/support/table/ShellTableTest.java | 30 ++++++++++++++-
3 files changed, 67 insertions(+), 9 deletions(-)
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/ansi/SimpleAnsi.java b/shell/core/src/main/java/org/apache/karaf/shell/support/ansi/SimpleAnsi.java
index a0b2566..de4db70 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/ansi/SimpleAnsi.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/ansi/SimpleAnsi.java
@@ -22,6 +22,9 @@ public class SimpleAnsi {
public static String COLOR_RED = "\u001b[31m";
public static String COLOR_CYAN = "\u001b[36m";
public static String COLOR_YELLOW = "\u001b[33m";
+ public static String COLOR_GREEN = "\u001b[32m";
+ public static String COLOR_BLUE = "\u001b[34m";
+ public static String COLOR_MAGENTA = "\u001b[35m";
public static String COLOR_DEFAULT = "\u001b[39m";
public static String INTENSITY_BOLD = "\u001b[1m";
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 29bbbb3..5476e89 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
@@ -18,6 +18,7 @@ package org.apache.karaf.shell.support.table;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -27,6 +28,12 @@ import org.apache.karaf.shell.support.ansi.SimpleAnsi;
* Column definition.
*/
public class Col {
+ // This is kept here only for backwards compatibility
+ // and is used in cyan(boolean) method
+ private static final Function<String, String> COLOR_CYAN =
+ (cellContent) -> SimpleAnsi.COLOR_CYAN;
+
+ Function<String, String> colorProvider;
/**
* Column header.
@@ -102,14 +109,26 @@ public class Col {
return cyan(true);
}
- public Col cyan(boolean cyan) {
- this.cyan = cyan;
- return this;
- }
+ public Col cyan(boolean cyan) {
+ if(cyan)
+ colorProvider(COLOR_CYAN);
+
+ // Only remove colorProvider if argument is false and
+ // member equals COLOR_CYAN
+ else if(this.colorProvider == COLOR_CYAN)
+ colorProvider(null);
+
+ return this;
+ }
public int getSize() {
return size;
}
+
+ public Col colorProvider(Function<String, String> colorProvider) {
+ this.colorProvider = colorProvider;
+ return this;
+ }
protected void updateSize(int cellSize) {
if (this.size <= cellSize) {
@@ -148,6 +167,12 @@ public class Col {
}
lines = wrapped;
}
+
+ String color = null;
+ if(colorProvider != null) {
+ color = colorProvider.apply(content);
+ }
+
StringBuilder sb = new StringBuilder();
for (String line : lines) {
if (sb.length() > 0) {
@@ -157,10 +182,14 @@ public class Col {
if (bold) {
line = SimpleAnsi.INTENSITY_BOLD + line + SimpleAnsi.INTENSITY_NORMAL;
}
- if (cyan) {
- line = SimpleAnsi.COLOR_CYAN + line + SimpleAnsi.COLOR_DEFAULT;
- }
+
+ if(color != null)
+ sb.append(color);
+
sb.append(line);
+
+ if(color != null)
+ sb.append(SimpleAnsi.COLOR_DEFAULT);
}
return sb.toString();
}
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
index dc764dc..1ef0939 100644
--- 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
@@ -18,6 +18,8 @@
*/
package org.apache.karaf.shell.support.table;
+import static org.junit.Assert.assertEquals;
+
import java.io.ByteArrayOutputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
@@ -25,10 +27,9 @@ import java.io.PrintStream;
import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
import org.apache.felix.service.threadio.ThreadIO;
+import org.apache.karaf.shell.support.ansi.SimpleAnsi;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
-
public class ShellTableTest {
@Test
@@ -107,6 +108,31 @@ public class ShellTableTest {
getString(baos));
}
+
+ @Test
+ public void testColoredTable() {
+ ShellTable table = new ShellTable().forceAscii();
+ table.separator("|");
+ table.column("State").colorProvider(cellContent -> {
+ if(cellContent.contains("Active"))
+ return SimpleAnsi.COLOR_GREEN;
+
+ if(cellContent.contains("Resolved"))
+ return SimpleAnsi.COLOR_YELLOW;
+ return null;
+ });
+
+ table.column("Description").maxSize(-1);
+ table.addRow().addContent("Normal", "This should have default color");
+ table.addRow().addContent("Active", "Green color");
+ table.addRow().addContent("This is Resolved", "Yellow color");
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ table.print(new PrintStream(baos), false);
+ assertEquals("Normal | This should have default color\n" +
+ "[32mActive [39m | Green color\n" +
+ "[33mThis is Resolved[39m | Yellow color\n", baos.toString());
+ }
private String getString(ByteArrayOutputStream stream) {
return stream.toString().replace("\r\n", "\n");