Skip to content

Commit

Permalink
Merge branch 'tailtip-highlight'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 19, 2019
2 parents 55a36a8 + 4fab621 commit 9db02fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
10 changes: 7 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ public CmdDesc commandDescription(String command) {
String d = s.substring(ind);
if (o.trim().length() > 0) {
prevOpt = o.trim();
options.put(prevOpt, new ArrayList<>(Arrays.asList(new AttributedString(d.trim()))));
options.put(prevOpt, new ArrayList<>(Arrays.asList(highlightComment(d.trim()))));
}
}
} else if (s.matches("^[\\s]{20}.*$") && prevOpt != null && options.containsKey(prevOpt)) {
int ind = s.lastIndexOf(" ");
if (ind > 0) {
options.get(prevOpt).add(new AttributedString(s.substring(ind).trim()));
options.get(prevOpt).add(highlightComment(s.substring(ind).trim()));
}
} else {
prevOpt = null;
}
if (!mainDone) {
main.add(new AttributedString(s.trim()));
main.add(HelpException.highlightSyntax(s.trim(), HelpException.defaultStyle()));
}
}
out = new CmdDesc(main, ArgDesc.doArgNames(Arrays.asList("[pN...]")), options);
Expand All @@ -230,6 +230,10 @@ public CmdDesc commandDescription(String command) {
return out;
}

private AttributedString highlightComment(String comment) {
return HelpException.highlightComment(comment, HelpException.defaultStyle());
}

private Terminal terminal() {
return reader.getTerminal();
}
Expand Down
65 changes: 40 additions & 25 deletions builtins/src/main/java/org/jline/builtins/Options.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -151,7 +151,7 @@ public List<Object> getObjectList(String name) {
if ( arg == null ) {
throw new IllegalArgumentException("option not defined with argument: " + name);
}

if (arg instanceof String) { // default value
list = new ArrayList<>();
if (!"".equals(arg))
Expand Down Expand Up @@ -229,7 +229,7 @@ public List<String> args() {
public void usage(PrintStream err) {
err.print(usage());
}

public String usage() {
StringBuilder buf = new StringBuilder();
int index = 0;
Expand Down Expand Up @@ -372,7 +372,7 @@ public Options parse(List<?> argv) {
public Options parse(Object[] argv, boolean skipArg0) {
if (null == argv)
throw new IllegalArgumentException("argv is null");

return parse(Arrays.asList(argv), skipArg0);
}

Expand Down Expand Up @@ -562,34 +562,49 @@ public static AttributedString highlight(String msg, StyleResolver resolver) {
comment = "";

}
AttributedStringBuilder asyntax = new AttributedStringBuilder().append(syntax);
// command
asyntax.styleMatches(Pattern.compile("(?:^)(?:\\s*)([a-z]+[a-z-]*){1}\\b"),
Collections.singletonList(resolver.resolve(".co")));
// argument
asyntax.styleMatches(Pattern.compile("(?:\\[|\\s|=)([A-Za-z]+[A-Za-z_-]*){1}\\b"),
Collections.singletonList(resolver.resolve(".ar")));
// option
asyntax.styleMatches(Pattern.compile("(?:\\s|\\[)(-\\$|-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"),
Collections.singletonList(resolver.resolve(".op")));
asb.append(asyntax);

AttributedStringBuilder acomment = new AttributedStringBuilder().append(comment);
// option
acomment.styleMatches(Pattern.compile("(?:\\s|\\[)(-\\$|-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"),
Collections.singletonList(resolver.resolve(".op")));
// argument in comment
acomment.styleMatches(Pattern.compile("(?:\\s)([a-z]+[-]+[a-z]+|[A-Z_]{2,}){1}(?:\\s)"),
Collections.singletonList(resolver.resolve(".ar")));
asb.append(acomment);

asb.append(_highlightSyntax(syntax, resolver));
asb.append(_highlightComment(comment, resolver));
asb.append("\n");
}
return asb.toAttributedString();
} else {
return AttributedString.fromAnsi(msg);
}
}

public static AttributedString highlightSyntax(String syntax, StyleResolver resolver) {
return _highlightSyntax(syntax, resolver).toAttributedString();
}

public static AttributedString highlightComment(String comment, StyleResolver resolver) {
return _highlightComment(comment, resolver).toAttributedString();
}

private static AttributedStringBuilder _highlightSyntax(String syntax, StyleResolver resolver) {
AttributedStringBuilder asyntax = new AttributedStringBuilder().append(syntax);
// command
asyntax.styleMatches(Pattern.compile("(?:^)(?:\\s*)([a-z]+[a-z-]*){1}\\b"),
Collections.singletonList(resolver.resolve(".co")));
// argument
asyntax.styleMatches(Pattern.compile("(?:\\[|\\s|=)([A-Za-z]+[A-Za-z_-]*){1}\\b"),
Collections.singletonList(resolver.resolve(".ar")));
// option
asyntax.styleMatches(Pattern.compile("(?:\\s|\\[)(-\\$|-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"),
Collections.singletonList(resolver.resolve(".op")));
return asyntax;
}

private static AttributedStringBuilder _highlightComment(String comment, StyleResolver resolver) {
AttributedStringBuilder acomment = new AttributedStringBuilder().append(comment);
// option
acomment.styleMatches(Pattern.compile("(?:\\s|\\[)(-\\$|-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"),
Collections.singletonList(resolver.resolve(".op")));
// argument in comment
acomment.styleMatches(Pattern.compile("(?:\\s)([a-z]+[-]+[a-z]+|[A-Z_]{2,}){1}(?:\\s)"),
Collections.singletonList(resolver.resolve(".ar")));
return acomment;

}
}

}
19 changes: 13 additions & 6 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Function;
import java.util.regex.Pattern;

import org.jline.builtins.Options.HelpException;
import org.jline.keymap.KeyMap;
import org.jline.reader.Binding;
import org.jline.reader.Buffer;
Expand Down Expand Up @@ -1200,7 +1201,7 @@ public CmdDesc(List<AttributedString> mainDesc, List<ArgDesc> argsDesc, Map<Stri
public boolean isValid() {
return valid;
}

public boolean isCommand() {
return command;
}
Expand Down Expand Up @@ -1295,7 +1296,7 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
}
}
if (matched.size() == 1) {
out.add(new AttributedString(matched.get(0), new AttributedStyle(AttributedStyle.BOLD)));
out.add(highlightOption(matched.get(0)));
for (AttributedString as: optsDesc.get(matched.get(0))) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(8);
asb.append("\t");
Expand All @@ -1305,7 +1306,7 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
} else if (matched.size() <= descriptionSize) {
for (String key: matched) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
asb.append(new AttributedString(key, new AttributedStyle(AttributedStyle.BOLD)));
asb.append(highlightOption(key));
asb.append("\t");
asb.append(optsDesc.get(key).get(0));
out.add(asb.toAttributedString());
Expand All @@ -1320,7 +1321,7 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
for (String key: matched) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
if (row < descriptionSize) {
asb.append(new AttributedString(key, new AttributedStyle(AttributedStyle.BOLD)));
asb.append(highlightOption(key));
asb.append("\t");
asb.append(optsDesc.get(key).get(0));
if (asb.columnLength() > columnWidth - 2) {
Expand All @@ -1337,7 +1338,7 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
keyList.add(asb.toAttributedString().columnSubSequence(0, columnWidth));
} else {
asb.append(keyList.get(row - descriptionSize));
asb.append(new AttributedString(key, new AttributedStyle(AttributedStyle.BOLD)));
asb.append(highlightOption(key));
asb.append("\t");
asb.append(optsDesc.get(key).get(0));
keyList.remove(row - descriptionSize);
Expand All @@ -1356,7 +1357,7 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
for (String key: matched) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
asb.append(keyList.get(row));
asb.append(key);
asb.append(highlightOption(key));
asb.append("\t");
keyList.remove(row);
keyList.add(row, asb.toAttributedString());
Expand All @@ -1369,6 +1370,12 @@ public List<AttributedString> getOptionDescription(String opt, int descriptionSi
}
return out;
}

private AttributedString highlightOption(String option) {
return new AttributedStringBuilder()
.append(option, HelpException.defaultStyle().resolve(".op"))
.toAttributedString();
}
}

static class Pair<U,V> {
Expand Down

1 comment on commit 9db02fe

@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.

fixes #472

Please sign in to comment.