diff --git a/builtins/src/main/java/org/jline/builtins/Styles.java b/builtins/src/main/java/org/jline/builtins/Styles.java index e73caa6f3..f47f9e3d4 100644 --- a/builtins/src/main/java/org/jline/builtins/Styles.java +++ b/builtins/src/main/java/org/jline/builtins/Styles.java @@ -19,7 +19,7 @@ public class Styles { private static final String DEFAULT_LS_COLORS = "di=1;91:ex=1;92:ln=1;96:fi="; private static final String DEFAULT_HELP_COLORS = "ti=1;34:co=1:ar=3:op=33"; - private static final String DEFAULT_PRNT_COLORS = "th=1;34:rn=1;34:mk=1;34:em=31:vs=32"; + private static final String DEFAULT_PRNT_COLORS = "th=1;34:rn=1;34:rs=,~grey15:mk=1;34:em=31:vs=32"; private static final String LS_COLORS = "LS_COLORS"; private static final String HELP_COLORS = "HELP_COLORS"; private static final String PRNT_COLORS = "PRNT_COLORS"; diff --git a/console/src/main/java/org/jline/console/Printer.java b/console/src/main/java/org/jline/console/Printer.java index 6fc9e8966..0beddc0f0 100644 --- a/console/src/main/java/org/jline/console/Printer.java +++ b/console/src/main/java/org/jline/console/Printer.java @@ -19,6 +19,7 @@ * @author Matti Rinta-Nikkola */ public interface Printer { + enum TableRows {EVEN, ODD, ALL} // // option names // @@ -126,6 +127,18 @@ public interface Printer { * Display width (default terminal width). */ String WIDTH = "width"; + /** + * Value: String
+ * Applies: TABLE
+ * Table column delimiter. + */ + String DELIMITER = "delimiter"; + /** + * Value: TableRows
+ * Applies: TABLE
+ * Highlight table rows. + */ + String ROW_HIGHLIGHT = "rowHighlight"; // // 2) additional PRNT_OPTIONS // @@ -168,12 +181,6 @@ public interface Printer { * Overrides the ScriptEngine toString() method. */ String OBJECT_TO_STRING = "objectToString"; - /** - * Value: String
- * Applies: TABLE
- * Table column delimiter. - */ - String DELIMITER = "delimiter"; List BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS , STRUCT_ON_TABLE, TO_STRING); diff --git a/console/src/main/java/org/jline/console/impl/DefaultPrinter.java b/console/src/main/java/org/jline/console/impl/DefaultPrinter.java index f210fcb19..88042877c 100644 --- a/console/src/main/java/org/jline/console/impl/DefaultPrinter.java +++ b/console/src/main/java/org/jline/console/impl/DefaultPrinter.java @@ -93,6 +93,7 @@ public String[] appendUsage(String[] customUsage) { " -d --maxDepth=DEPTH Maximum depth objects are resolved", " --maxrows=ROWS Maximum number of lines to display", " --oneRowTable Display one row data on table", + " -h --rowHighlight=ROW Highlight table rows. ROW = EVEN, ODD, ALL", " -r --rownum Display table row numbers", " --shortNames Truncate table column names (property.field -> field)", " --skipDefaultOptions Ignore all options defined in PRNT_OPTIONS", @@ -170,6 +171,14 @@ public Map compileOptions(Options opt) { if (opt.isSet(Printer.DELIMITER)) { options.put(Printer.DELIMITER, opt.get(Printer.DELIMITER)); } + if (opt.isSet(Printer.ROW_HIGHLIGHT)) { + try { + TableRows tr = TableRows.valueOf(opt.get(Printer.ROW_HIGHLIGHT).toUpperCase()); + options.put(Printer.ROW_HIGHLIGHT, tr); + } catch (Exception e) { + // ignore + } + } options.put("exception", "stack"); return options; } @@ -756,6 +765,7 @@ private void highlightAndPrint(Map options, Object obj) { } } String columnSep = (String)options.getOrDefault(Printer.DELIMITER, ""); + TableRows tableRows = (TableRows)options.getOrDefault(Printer.ROW_HIGHLIGHT, null); toTabStops(columns, collection.size(), rownum, columnSep); AttributedStringBuilder asb = new AttributedStringBuilder().tabs(columns); asb.style(prntStyle.resolve(".th")); @@ -777,12 +787,15 @@ private void highlightAndPrint(Map options, Object obj) { int row = 0; for (Object o : collection) { AttributedStringBuilder asb2 = new AttributedStringBuilder().tabs(columns); + if (doRowHighlight(row, tableRows)) { + asb2.style(prntStyle.resolve(".rs")); + } if (rownum) { asb2.styled(prntStyle.resolve(".rn") , addPadding(Integer.toString(row), columns.get(0) - 1)); asb2.append("\t"); - row++; } + row++; Map m = convert ? objectToMap(options, o) : keysToString((Map) o); for (int i = 0; i < header.size(); i++) { @@ -812,17 +825,21 @@ private void highlightAndPrint(Map options, Object obj) { } } String columnSep = (String)options.getOrDefault(Printer.DELIMITER, ""); + TableRows tableRows = (TableRows)options.getOrDefault(Printer.ROW_HIGHLIGHT, null); toTabStops(columns, collection.size(), rownum, columnSep); int row = 0; int firstColumn = rownum ? 1 : 0; for (Object o : collection) { AttributedStringBuilder asb = new AttributedStringBuilder().tabs(columns); + if (doRowHighlight(row, tableRows)) { + asb.style(prntStyle.resolve(".rs")); + } if (rownum) { asb.styled(prntStyle.resolve(".rn") , addPadding(Integer.toString(row), columns.get(0) - 1)); asb.append("\t"); - row++; } + row++; List inner = objectToList(o); for (int i = 0; i < inner.size(); i++) { if (i > 0) { @@ -860,6 +877,21 @@ private void highlightAndPrint(Map options, Object obj) { } } + private boolean doRowHighlight(int row, TableRows tableRows) { + if (tableRows == null) { + return false; + } + switch (tableRows) { + case EVEN: + return row % 2 == 0; + case ODD: + return row % 2 == 1; + case ALL: + return true; + } + return false; + } + private void highlightList(Map options , List collection, int width) { highlightList(options, collection, width, 0); diff --git a/demo/src/main/scripts/init.jline b/demo/src/main/scripts/init.jline index 254c2bd76..85158d8f6 100644 --- a/demo/src/main/scripts/init.jline +++ b/demo/src/main/scripts/init.jline @@ -19,7 +19,7 @@ CONSOLE_OPTIONS['docs'].put('java.*',['https://docs.oracle.com/javase/8/docs/api CONSOLE_OPTIONS['docs'].put('.*/java.*','https://docs.oracle.com/javase/8/docs/api/') CONSOLE_OPTIONS['docs'].put('org/jline/.*','https://www.javadoc.io/doc/org.jline/jline/latest/') -CONSOLE_OPTIONS.PRNT_COLORS = 'th=bold,!blue,~grey7,underline:rn=bold,!blue,~grey7:mk=1;34:em=31:vs=32' +CONSOLE_OPTIONS.PRNT_COLORS = 'th=bold,!blue,~grey7,underline:rn=bold,!blue,~grey7:rs=,~grey15:mk=1;34:em=31:vs=32' # # customize prnt command #