From 4fab621b2dbcba0aa33ffb69e17b8935c8693540 Mon Sep 17 00:00:00 2001 From: mattirn Date: Sat, 16 Nov 2019 09:07:14 +0100 Subject: [PATCH] TailTipWidgets: highlight command descriptions --- .../java/org/jline/builtins/Builtins.java | 10 ++- .../main/java/org/jline/builtins/Options.java | 65 ++++++++++++------- .../main/java/org/jline/builtins/Widgets.java | 19 ++++-- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Builtins.java b/builtins/src/main/java/org/jline/builtins/Builtins.java index 83eab41e8..67d2cc1ff 100644 --- a/builtins/src/main/java/org/jline/builtins/Builtins.java +++ b/builtins/src/main/java/org/jline/builtins/Builtins.java @@ -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); @@ -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(); } diff --git a/builtins/src/main/java/org/jline/builtins/Options.java b/builtins/src/main/java/org/jline/builtins/Options.java index 966335237..88aedb900 100644 --- a/builtins/src/main/java/org/jline/builtins/Options.java +++ b/builtins/src/main/java/org/jline/builtins/Options.java @@ -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. @@ -151,7 +151,7 @@ public List 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)) @@ -229,7 +229,7 @@ public List args() { public void usage(PrintStream err) { err.print(usage()); } - + public String usage() { StringBuilder buf = new StringBuilder(); int index = 0; @@ -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); } @@ -562,27 +562,8 @@ 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(); @@ -590,6 +571,40 @@ public static AttributedString highlight(String msg, StyleResolver resolver) { 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; + + } } } diff --git a/builtins/src/main/java/org/jline/builtins/Widgets.java b/builtins/src/main/java/org/jline/builtins/Widgets.java index 543e298f8..8ff10911c 100644 --- a/builtins/src/main/java/org/jline/builtins/Widgets.java +++ b/builtins/src/main/java/org/jline/builtins/Widgets.java @@ -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; @@ -1200,7 +1201,7 @@ public CmdDesc(List mainDesc, List argsDesc, Map 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"); @@ -1305,7 +1306,7 @@ public List 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()); @@ -1320,7 +1321,7 @@ public List 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) { @@ -1337,7 +1338,7 @@ public List 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); @@ -1356,7 +1357,7 @@ public List 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()); @@ -1369,6 +1370,12 @@ public List 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 {