Skip to content

Commit

Permalink
Added javadocs in Printer and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 25, 2020
1 parent 43748c7 commit 51b8bcf
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 21 deletions.
31 changes: 12 additions & 19 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,14 @@ private void internalPrintln(Map<String, Object> options, Object object) {
highlightAndPrint(width, style, engine.toJson(object));
} else if (!style.isEmpty() && object instanceof String) {
highlightAndPrint(width, style, (String) object);
} else if (!options.containsKey(Printer.SKIP_DEFAULT_OPTIONS) && object instanceof Exception) {
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
for (AttributedString as : highlight(options, object)) {
as.println(terminal());
}
} else if (object instanceof Exception) {
systemRegistry.trace(options.getOrDefault("exception", "stack").equals("stack"), (Exception)object);
} else if (!options.containsKey(Printer.SKIP_DEFAULT_OPTIONS) && object instanceof CmdDesc) {
printHelp((CmdDesc)object);
} else if (object instanceof CmdDesc) {
highlight((CmdDesc)object).println(terminal());
} else if (object instanceof String) {
highlight(AttributedStyle.YELLOW + AttributedStyle.BRIGHT, object).println(terminal());
} else if (object instanceof Number) {
Expand All @@ -946,16 +950,11 @@ private void internalPrintln(Map<String, Object> options, Object object) {
Log.debug("println: ", new Date().getTime() - start, " msec");
}

private void printHelp(CmdDesc cmdDesc) {
boolean highlight = !isHighlighted(cmdDesc.getMainDesc().get(0));
private AttributedString highlight(CmdDesc cmdDesc) {
StringBuilder sb = new StringBuilder();
for (AttributedString as : cmdDesc.getMainDesc()) {
if (!highlight) {
as.println(terminal());
} else {
sb.append(as.toString());
sb.append("\n");
}
sb.append(as.toString());
sb.append("\n");
}
List<Integer> tabs = Arrays.asList(0, 2, 33);
for (Map.Entry<String, List<AttributedString>> entry : cmdDesc.getOptsDesc().entrySet()) {
Expand All @@ -974,15 +973,9 @@ private void printHelp(CmdDesc cmdDesc) {
asb.append("\n");
first = false;
}
if (!highlight) {
asb.toAttributedString().print(terminal());
} else {
sb.append(asb.toString());
}
}
if (highlight) {
Options.HelpException.highlight(sb.toString(), Options.HelpException.defaultStyle()).println(terminal());
sb.append(asb.toString());
}
return Options.HelpException.highlight(sb.toString(), Options.HelpException.defaultStyle());
}

private AttributedString highlight(int attrStyle, Object obj) {
Expand Down
1 change: 0 additions & 1 deletion demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.jline.terminal.Terminal.Signal;
import org.jline.utils.InfoCmp;
import org.jline.utils.InfoCmp.Capability;

import org.jline.utils.OSUtils;

/**
Expand Down
2 changes: 1 addition & 1 deletion groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public Object inspect(CommandInput input) {
try {
Object obj = input.xargs()[id];
ObjectInspector inspector = new ObjectInspector(obj);
Object out = option;
Object out = null;
if (option.equals("-m") || option.equals("--methods")) {
out = inspector.methods();
} else if (option.equals("-n") || option.equals("--metaMethods")) {
Expand Down
113 changes: 113 additions & 0 deletions reader/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,148 @@
import java.util.HashMap;
import java.util.Map;

/**
* Print object to the console.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public interface Printer {
//
// option names
//
// 1) command options
//
/**
* Value: not meaningful<br>
* Applies: TABLE<br>
* Ignore columnsOut configuration.
*/
final static String ALL = "all";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Display given columns on table.
*/
final static String COLUMNS = "columns";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Exclude given columns on table.
*/
final static String EXCLUDE = "exclude";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Include given columns on table.
*/
final static String INCLUDE = "include";
/**
* Value: Integer<br>
* Applies: MAP<br>
* Indention size.
*/
final static String INDENTION = "indention";
/**
* Value: Integer<br>
* Applies: MAP and TABLE<br>
* Maximum column width.
*/
final static String MAX_COLUMN_WIDTH = "maxColumnWidth";
/**
* Value: Integer<br>
* Applies: MAP<br>
* Maximum depth objects are resolved.
*/
final static String MAX_DEPTH = "maxDepth";
/**
* Value: Integer<br>
* Applies: TABLE<br>
* Maximum number of rows on table.
*/
final static String MAXROWS = "maxrows";
/**
* Value: not meaningful<br>
* Applies: TABLE<br>
* Display one row data on table.
*/
final static String ONE_ROW_TABLE = "oneRowTable";
/**
* Value: Integer<br>
* Applies: TABLE<br>
* Display table row numbers.
*/
final static String ROWNUM = "rownum";
/**
* Value: not meaningful<br>
* Applies: MAP and TABLE<br>
* Ignore all options defined in PRNT_OPTIONS.
*/
final static String SKIP_DEFAULT_OPTIONS = "skipDefaultOptions";
/**
* Value: not meaningful<br>
* Applies: TABLE<br>
* Display structs and lists on table.
*/
final static String STRUCT_ON_TABLE = "structsOnTable";
/**
* Value: String<br>
* Use nanorc STYLE<br>
*/
final static String STYLE = "style";
/**
* Value: not meaningful<br>
* Applies: MAP and TABLE<br>
* Use object's toString() method to get print value
* DEFAULT: object's fields are put to property map before printing
*/
final static String TO_STRING = "toString";
/**
* Value: Integer<br>
* Applies: MAP and TABLE<br>
* Display width (default terminal width).
*/
final static String WIDTH = "width";
//
// 2) additional PRNT_OPTIONS
//
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* These map values will be added to the table before all the other keys.
*/
final static String COLUMNS_IN = "columnsIn";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* These map values will not be inserted to the table.
*/
final static String COLUMNS_OUT = "columnsOut";
/**
* Value: {@code Map<regex, function>}.<br>
* Applies: TABLE<br>
* If command result map key matches with regex the highlight function is applied
* to the corresponding map value. The regex =* is processed after all the other regexes and the highlight
* function will be applied to all map values that have not been already highlighted.
*/
final static String HIGHLIGHT_VALUE = "highlightValue";
/**
* Value: Double<br>
* Applies: MAP and TABLE<br>
* default value 0.8 i.e. if at least of 4 of the 5 results map keys match with reference key set the
* result will be printed out as a table.
*/
final static String MAP_SIMILARITY = "mapSimilarity";
/**
* Value: {@code Map<class, function>}<br>
* Applies: MAP and TABLE<br>
* Overrides the ScriptEngine toMap() method.
*/
final static String OBJECT_TO_MAP = "objectToMap";
/**
* Value: {@code Map<class, function>}<br>
* Applies: MAP and TABLE<br>
* Overrides the ScriptEngine toString() method.
*/
final static String OBJECT_TO_STRING = "objectToString";

default void println(Object object) {
Expand Down

0 comments on commit 51b8bcf

Please sign in to comment.