Skip to content

Commit

Permalink
multi-instanciable HelpPrinter with the JLine's StyleResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 17, 2019
1 parent cb027ad commit 7b37f7f
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 215 deletions.
4 changes: 4 additions & 0 deletions builtins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>org.jline</groupId>
<artifactId>jline-reader</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-style</artifactId>
</dependency>

<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
Expand Down
50 changes: 21 additions & 29 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.jline.builtins.Completers.CompletionData;
import org.jline.builtins.Options;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Source.StdInSource;
import org.jline.builtins.Source.URLSource;
import org.jline.keymap.KeyMap;
Expand Down Expand Up @@ -64,8 +65,7 @@ public static void tmux(Terminal terminal, PrintStream out, PrintStream err,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
// Tmux with no args
if (argv.length == 0) {
Expand Down Expand Up @@ -101,8 +101,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
Nano edit = new Nano(terminal, currentDir);
edit.open(opt.args());
Expand All @@ -111,7 +110,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,

public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
Path currentDir,
String[] argv) throws IOException, InterruptedException {
String[] argv) throws Exception {
final String[] usage = {
"less - file pager",
"Usage: less [OPTIONS] [FILES]",
Expand All @@ -130,8 +129,7 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
Options opt = Options.compile(usage).parse(argv);

if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}

Less less = new Less(terminal);
Expand Down Expand Up @@ -161,7 +159,7 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
}

public static void history(LineReader reader, PrintStream out, PrintStream err,
String[] argv) throws IOException, IllegalArgumentException {
String[] argv) throws Exception {
final String[] usage = {
"history - list history of commands",
"Usage: history [-dnrfEi] [-m match] [first] [last]",
Expand All @@ -174,24 +172,23 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
" -m match If option -m is present the first argument is taken as a pattern",
" and only the history events matching the pattern will be shown",
" -d Print timestamps for each event",
" -f Print full time-date stamps in the US format",
" -E Print full time-date stamps in the European format",
" -i Print full time-date stamps in ISO8601 format",
" -f Print full time date stamps in the US format",
" -E Print full time date stamps in the European format",
" -i Print full time date stamps in ISO8601 format",
" -n Suppresses command numbers",
" -r Reverses the order of the commands",
" -A Appends the history out to the given file",
" -R Reads the history from the given file",
" -W Writes the history out to the given file",
" -I If added to -R, only the events that are not contained within the internal list are added",
" If added to -W/A, only the events that are new since the last incremental operation to",
" the file are added",
" If added to -W or -A, only the events that are new since the last incremental operation",
" to the file are added",
" [first] [last] These optional arguments are numbers. A negative number is",
" used as an offset to the current history event number"};
Options opt = Options.compile(usage).parse(argv);

if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
History history = reader.getHistory();
boolean done = true;
Expand Down Expand Up @@ -298,7 +295,7 @@ private static int parseInteger(String s) throws IllegalArgumentException {

public static void complete(LineReader reader, PrintStream out, PrintStream err,
Map<String, List<CompletionData>> completions,
String[] argv) {
String[] argv) throws HelpException {
final String[] usage = {
"complete - edit command specific tab-completions",
"Usage: complete",
Expand All @@ -315,8 +312,7 @@ public static void complete(LineReader reader, PrintStream out, PrintStream err,
Options opt = Options.compile(usage).parse(argv);

if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}

String command = opt.get("command");
Expand Down Expand Up @@ -370,8 +366,7 @@ public static void widget(LineReader reader, PrintStream out, PrintStream err,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}

int actions = (opt.isSet("N") ? 1 : 0)
Expand Down Expand Up @@ -435,7 +430,7 @@ else if (opt.args().size() == 1) {
public static void keymap(LineReader reader,
PrintStream out,
PrintStream err,
String[] argv) {
String[] argv) throws HelpException {
final String[] usage = {
"keymap - manipulate keymaps",
"Usage: keymap [options] -l [-L] [keymap ...]",
Expand Down Expand Up @@ -466,8 +461,7 @@ public static void keymap(LineReader reader,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}

Map<String, KeyMap<Binding>> keyMaps = reader.getKeyMaps();
Expand Down Expand Up @@ -782,7 +776,7 @@ else if (opt.isSet("s") || opt.args().size() > 1) {
public static void setopt(LineReader reader,
PrintStream out,
PrintStream err,
String[] argv) {
String[] argv) throws HelpException {
final String[] usage = {
"setopt - set options",
"Usage: setopt [-m] option ...",
Expand All @@ -792,8 +786,7 @@ public static void setopt(LineReader reader,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
if (opt.args().isEmpty()) {
for (Option option : Option.values()) {
Expand All @@ -811,7 +804,7 @@ public static void setopt(LineReader reader,
public static void unsetopt(LineReader reader,
PrintStream out,
PrintStream err,
String[] argv) {
String[] argv) throws HelpException {
final String[] usage = {
"unsetopt - unset options",
"Usage: unsetopt [-m] option ...",
Expand All @@ -821,8 +814,7 @@ public static void unsetopt(LineReader reader,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
if (opt.args().isEmpty()) {
for (Option option : Option.values()) {
Expand Down
96 changes: 73 additions & 23 deletions builtins/src/main/java/org/jline/builtins/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStyle;
import org.jline.utils.AttributedString;
import org.jline.utils.StyleResolver;

/**
* Yet another GNU long options parser. This one is configured by parsing its Usage string.
Expand Down Expand Up @@ -220,7 +227,12 @@ public List<String> args() {
return args;
}

// Added for backword compability
public void usage(PrintStream err) {
err.print(usage());
}

public String usage() {
StringBuilder buf = new StringBuilder();
int index = 0;

Expand All @@ -235,9 +247,7 @@ public void usage(PrintStream err) {
buf.append(NL);
}

String msg = buf.toString();

HelpPrinter.getInstance().print(err, msg);
return buf.toString();
}

/**
Expand Down Expand Up @@ -506,44 +516,77 @@ public String toString() {
}

public static class HelpPrinter {
private static HelpPrinter instance = new HelpPrinter();

private final List<String> names = Arrays.asList("ti", "co", "ar", "op");
private final Pattern patternCommand = Pattern.compile("(^\\s*)([a-z]+[a-z-]*){1}\\b");
private final Pattern patternArgument = Pattern.compile("(\\[|\\s|=)([A-Za-z]+[A-Za-z_-]*){1}\\b");
private final Pattern patternArgumentInComment = Pattern.compile("(\\s)([a-z]+[-]+[a-z]+|[A-Z_]{2,}){1}(\\s)");
private final Pattern patternOption = Pattern.compile("(\\s|\\[)(-\\?|[-]{1,2}[A-Za-z-]+\\b){1}");
private final String title = "Usage";
private final String ansiReset = "\033[0m";
private String ansi4title = "\033[34;1m";
private String ansi4command = "\033[1m";
private String ansi4argument = "\033[3m";
private String ansi4option = "\033[33m";
private boolean color = false;

private HelpPrinter() {}
private String ansi4title;
private String ansi4command;
private String ansi4argument;
private String ansi4option;
private boolean color = true;
private Terminal terminal;

public static HelpPrinter getInstance() {
return instance;
public HelpPrinter() {
this(null);
}


public HelpPrinter(Terminal terminal) {
this.terminal = terminal;
setColors("ti=1;34:co=1:ar=3:op=33");
}

public void setColor(boolean color) {
this.color = color;
}

public void setAnsi4title(String ansicode) {
this.ansi4title = ansicode;
public void setColor4title(AttributedStyle style) {
this.ansi4title = styleToAnsiCode(style);
}

public void setAnsi4command(String ansicode) {
this.ansi4command = ansicode;
public void setColor4command(AttributedStyle style) {
this.ansi4command = styleToAnsiCode(style);
}

public void setAnsi4argument(String ansicode) {
this.ansi4argument = ansicode;
public void setColor4argument(AttributedStyle style) {
this.ansi4argument = styleToAnsiCode(style);
}

public void setAnsi4option(String ansicode) {
this.ansi4option = ansicode;
public void setColor4option(AttributedStyle style) {
this.ansi4option = styleToAnsiCode(style);
}

public void setColors (Map<String, String> colors) {
for (String n: names) {
if (colors.containsKey(n)) {
AttributedStyle s = new StyleResolver(colors::get).resolve(colors.get(n));
if (n.equals("ti")) {
setColor4title(s);
} else if (n.equals("co")) {
setColor4command(s);
} else if (n.equals("ar")) {
setColor4argument(s);
} else if (n.equals("op")) {
setColor4option(s);
}
}
}
}

public void setColors (String str) {
String sep = str.matches("[a-z]{2}=[0-9]*(;[0-9]+)*(:[a-z]{2}=[0-9]*(;[0-9]+)*)*") ? ":" : " ";
setColors(Arrays.stream(str.split(sep))
.collect(Collectors.toMap(s -> s.substring(0, s.indexOf('=')),
s -> s.substring(s.indexOf('=') + 1))));

}

private String styleToAnsiCode(AttributedStyle style) {
String[] as = new AttributedString("HP", style).toAnsi(terminal).split("HP");
return as.length > 0 ? as[0] : ansiReset;
}

public void print(PrintStream err, String msg) {
Expand Down Expand Up @@ -603,4 +646,11 @@ private String highlightComment(String comment) {
}
}

@SuppressWarnings("serial")
public static class HelpException extends Exception {
public HelpException(String message) {
super(message);
}
}

}
4 changes: 2 additions & 2 deletions builtins/src/main/java/org/jline/builtins/TTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.builtins;

import org.jline.builtins.Options.HelpException;
import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.terminal.Attributes;
Expand Down Expand Up @@ -84,8 +85,7 @@ public static void ttop(Terminal terminal, PrintStream out, PrintStream err,
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(err);
return;
throw new HelpException(opt.usage());
}
TTop ttop = new TTop(terminal);
ttop.sort = opt.isSet("order") ? Arrays.asList(opt.get("order").split(",")) : null;
Expand Down

0 comments on commit 7b37f7f

Please sign in to comment.