Skip to content

Commit

Permalink
prnt command: added shortNames option
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed May 21, 2020
1 parent ab29ae2 commit 2ed9941
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 19 additions & 4 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,15 @@ private void println(AttributedString line, int maxrows) {
}
}

private String columnName(String name, boolean shortName) {
String out = name;
if (shortName) {
String[] p = name.split("\\.");
out = p[p.length - 1];
}
return out;
}

@SuppressWarnings("unchecked")
private void highlightAndPrint(Map<String, Object> options, Object obj) {
int width = (int)options.get(Printer.WIDTH);
Expand Down Expand Up @@ -1376,8 +1385,9 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
String rk = map.containsKey(_header.get(i)) ? _header.get(i) : _header.get(i).split("\\.")[0];
refKeys.add(rk);
header.add(_header.get(i));
columns.add(_header.get(i).length() + 1);
headerWidth += _header.get(i).length() + 1;
String cn = columnName(_header.get(i), options.containsKey(Printer.SHORT_NAMES));
columns.add(cn.length() + 1);
headerWidth += cn.length() + 1;
if (headerWidth > width) {
break;
}
Expand Down Expand Up @@ -1411,7 +1421,8 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
firstColumn = 1;
}
for (int i = 0; i < header.size(); i++) {
asb.styled(prntStyle.resolve(".th"), header.get(i));
asb.styled(prntStyle.resolve(".th")
, columnName(header.get(i), options.containsKey(Printer.SHORT_NAMES)));
asb.append("\t");
}
truncate(asb, width).println(terminal());
Expand Down Expand Up @@ -1482,7 +1493,7 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
}
}
} else {
highlightValue(options, null, objectToString(options, obj)).println(terminal());
highlightValue(options, null, objectToString(options, obj)).println(terminal());
}
} else if (canConvert(obj) && !options.containsKey(Printer.TO_STRING)) {
highlightMap(options, objectToMap(options, obj), width);
Expand Down Expand Up @@ -1693,6 +1704,7 @@ private Object prnt(CommandInput input) {
" --maxrows=ROWS Maximum number of lines to display",
" --oneRowTable Display one row data on table",
" -r --rownum Display table row numbers",
" --shortNames Truncate table column names (property.field -> field)",
" --skipDefaultOptions Ignore all options defined in PRNT_OPTIONS",
" --structsOnTable Display structs and lists on table",
" -s --style=STYLE Use nanorc STYLE",
Expand Down Expand Up @@ -1720,6 +1732,9 @@ private Object prnt(CommandInput input) {
if (opt.isSet(Printer.ONE_ROW_TABLE)) {
options.put(Printer.ONE_ROW_TABLE, true);
}
if (opt.isSet(Printer.SHORT_NAMES)) {
options.put(Printer.SHORT_NAMES, true);
}
if (opt.isSet(Printer.STRUCT_ON_TABLE)) {
options.put(Printer.STRUCT_ON_TABLE, true);
}
Expand Down
6 changes: 6 additions & 0 deletions reader/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public interface Printer {
* Display table row numbers.
*/
final static String ROWNUM = "rownum";
/**
* Value: not meaningful<br>
* Applies: TABLE<br>
* Truncate table column names: property.field -> field.
*/
final static String SHORT_NAMES = "shortNames";
/**
* Value: not meaningful<br>
* Applies: MAP and TABLE<br>
Expand Down

0 comments on commit 2ed9941

Please sign in to comment.