Skip to content

Commit

Permalink
Command prnt: add option --rowHighlight
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 4, 2021
1 parent 0c23173 commit 2d42d39
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Styles.java
Expand Up @@ -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";
Expand Down
19 changes: 13 additions & 6 deletions console/src/main/java/org/jline/console/Printer.java
Expand Up @@ -19,6 +19,7 @@
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public interface Printer {
enum TableRows {EVEN, ODD, ALL}
//
// option names
//
Expand Down Expand Up @@ -126,6 +127,18 @@ public interface Printer {
* Display width (default terminal width).
*/
String WIDTH = "width";
/**
* Value: String<br>
* Applies: TABLE<br>
* Table column delimiter.
*/
String DELIMITER = "delimiter";
/**
* Value: TableRows<br>
* Applies: TABLE<br>
* Highlight table rows.
*/
String ROW_HIGHLIGHT = "rowHighlight";
//
// 2) additional PRNT_OPTIONS
//
Expand Down Expand Up @@ -168,12 +181,6 @@ public interface Printer {
* Overrides the ScriptEngine toString() method.
*/
String OBJECT_TO_STRING = "objectToString";
/**
* Value: String<br>
* Applies: TABLE<br>
* Table column delimiter.
*/
String DELIMITER = "delimiter";

List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
, STRUCT_ON_TABLE, TO_STRING);
Expand Down
36 changes: 34 additions & 2 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Expand Up @@ -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",
Expand Down Expand Up @@ -170,6 +171,14 @@ public Map<String, Object> 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;
}
Expand Down Expand Up @@ -756,6 +765,7 @@ private void highlightAndPrint(Map<String, Object> 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"));
Expand All @@ -777,12 +787,15 @@ private void highlightAndPrint(Map<String, Object> 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<String, Object> m = convert ? objectToMap(options, o)
: keysToString((Map<Object, Object>) o);
for (int i = 0; i < header.size(); i++) {
Expand Down Expand Up @@ -812,17 +825,21 @@ private void highlightAndPrint(Map<String, Object> 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<Object> inner = objectToList(o);
for (int i = 0; i < inner.size(); i++) {
if (i > 0) {
Expand Down Expand Up @@ -860,6 +877,21 @@ private void highlightAndPrint(Map<String, Object> 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<String, Object> options
, List<Object> collection, int width) {
highlightList(options, collection, width, 0);
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/scripts/init.jline
Expand Up @@ -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
#
Expand Down

0 comments on commit 2d42d39

Please sign in to comment.