Skip to content

Commit

Permalink
TailTipWidgets improvement: show option description in status bar when
Browse files Browse the repository at this point in the history
typing short option value.
  • Loading branch information
mattirn committed Dec 8, 2019
1 parent 43c8928 commit 536d841
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
75 changes: 57 additions & 18 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Expand Up @@ -801,20 +801,31 @@ private boolean doTailTip(String widget) {

private void doCommandTailTip(String widget, CmdDesc cmdDesc, List<String> args) {
int argnum = 0;
String prevArg = "";
for (String a : args) {
if (!a.startsWith("-")) {
argnum++;
if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) {
argnum++;
}
}
prevArg = a;
}
String lastArg = "";
prevArg = args.get(args.size() - 1);
if (!prevChar().equals(" ")) {
lastArg = args.get(args.size() - 1);
prevArg = args.get(args.size() - 2);
}
String lastArg = !prevChar().equals(" ") ? args.get(args.size() - 1) : "";
int bpsize = argnum;
boolean doTailTip = true;
boolean noCompleters = false;
if (widget.endsWith(LineReader.BACKWARD_DELETE_CHAR)) {
setSuggestionType(SuggestionType.TAIL_TIP);
noCompleters = true;
if (!lastArg.startsWith("-")) {
bpsize--;
if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) {
bpsize--;
}
}
if (prevChar().equals(" ")) {
bpsize++;
Expand All @@ -824,12 +835,23 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List<String> args)
}
if (cmdDesc != null) {
if (lastArg.startsWith("-")) {
doDescription(cmdDesc.getOptionDescription(lastArg, descriptionSize));
if (!lastArg.contains("=")) {
setSuggestionType(SuggestionType.TAIL_TIP);
noCompleters = true;
if (lastArg.matches("-[a-zA-Z]{1}[a-zA-Z0-9]+")) {
if (cmdDesc.optionWithValue(lastArg.substring(0,2))) {
doDescription(cmdDesc.getOptionDescription(lastArg.substring(0,2), descriptionSize));
setTipType(tipType);
} else {
doDescription(cmdDesc.getOptionDescription("-" + lastArg.substring(lastArg.length() - 1), descriptionSize));
setSuggestionType(SuggestionType.TAIL_TIP);
noCompleters = true;
}
} else {
setTipType(tipType);
doDescription(cmdDesc.getOptionDescription(lastArg, descriptionSize));
if (!lastArg.contains("=")) {
setSuggestionType(SuggestionType.TAIL_TIP);
noCompleters = true;
} else {
setTipType(tipType);
}
}
} else if (!widget.endsWith(LineReader.BACKWARD_DELETE_CHAR)){
setTipType(tipType);
Expand All @@ -841,9 +863,13 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List<String> args)
}
if (bpsize - 1 < params.size()) {
if (!lastArg.startsWith("-")) {
List<AttributedString> d = params.get(bpsize - 1)
.getDescription();
if (d.isEmpty()) {
List<AttributedString> d = null;
if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) {
d = params.get(bpsize - 1).getDescription();
} else {
d = cmdDesc.getOptionDescription(prevArg, descriptionSize);
}
if (d == null || d.isEmpty()) {
d = cmdDesc.getMainDescription(descriptionSize);
}
doDescription(d);
Expand Down Expand Up @@ -1202,11 +1228,11 @@ public CmdDesc(List<AttributedString> mainDesc, List<ArgDesc> argsDesc, Map<Stri
this.command = true;
}

public boolean isValid() {
protected boolean isValid() {
return valid;
}

public boolean isCommand() {
protected boolean isCommand() {
return command;
}

Expand Down Expand Up @@ -1235,11 +1261,11 @@ public int getErrorIndex() {
return errorIndex;
}

public List<ArgDesc> getArgsDesc() {
protected List<ArgDesc> getArgsDesc() {
return argsDesc;
}

public List<AttributedString> getMainDescription(int descriptionSize) {
protected List<AttributedString> getMainDescription(int descriptionSize) {
List<AttributedString> out = new ArrayList<>();
if (mainDesc == null) {
// do nothing
Expand Down Expand Up @@ -1276,11 +1302,11 @@ public List<AttributedString> getMainDescription(int descriptionSize) {
return out;
}

public List<AttributedString> getOptionDescription(String opt, int descriptionSize) {
protected List<AttributedString> getOptionDescription(String opt, int descriptionSize) {
List<AttributedString> out = new ArrayList<>();
if (!opt.startsWith("-")) {
return out;
} else if (opt.startsWith("--")) {
} else {
int ind = opt.indexOf("=");
if (ind > 0) {
opt = opt.substring(0, ind);
Expand Down Expand Up @@ -1374,7 +1400,20 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
}
return out;
}


protected boolean optionWithValue(String option) {
for (String key: optsDesc.keySet()) {
if (key.matches("(^|.*\\s)" + option + "($|=.*|\\s.*)")) {
if (key.contains("=")) {
return true;
} else {
return false;
}
}
}
return false;
}

private AttributedString optionDescription(String key) {
return optsDesc.get(key).size() > 0 ? optsDesc.get(key).get(0) : new AttributedString("");
}
Expand Down
2 changes: 1 addition & 1 deletion builtins/src/test/java/org/jline/example/Example.java
Expand Up @@ -150,7 +150,7 @@ private static Map<String,CmdDesc> compileTailTips() {
tailTips.put("foo11", new CmdDesc(Arrays.asList(
new ArgDesc("param1",Arrays.asList(new AttributedString("Param1 description...")
, new AttributedString("line 2: This is a very long line that does exceed the terminal width."
+" The line will be truncated automatically (by Status class) be before printing out.")
+" The line will be truncated automatically (by Status class) before printing out.")
, new AttributedString("line 3")
, new AttributedString("line 4")
, new AttributedString("line 5")
Expand Down

1 comment on commit 536d841

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.