Skip to content

Commit

Permalink
PicocliCommands.commandDescription(): resolve also sub-command descs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn authored and remkop committed Jun 3, 2020
1 parent fb11907 commit 6dbaf3a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ allprojects {
compileTestJava.options.encoding = "UTF-8"

repositories {
mavenLocal()
jcenter()
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ivyVersion = 2.4.0
jacocoVersion = 0.8.2
jansiVersion = 1.15
jlineVersion = 2.14.6
jline3Version = 3.14.1
jline3Version = 3.14.2-SNAPSHOT
junitDepVersion = 4.11
junitVersion = 4.12
springBootVersion = 2.2.2.RELEASE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
assert candidates != null;
String word = commandLine.word();
List<String> words = commandLine.words();
CommandLine sub = cmd;
for (int i = 0; i < commandLine.wordIndex(); i++) {
if (!words.get(i).startsWith("-")) {
sub = findSubcommandLine(sub,words.get(i));
if (sub == null) {
return;
}
}
CommandLine sub = findSubcommandLine(words, commandLine.wordIndex());
if (sub == null) {
return;
}
if (word.startsWith("-")) {
String buffer = word.substring(0, commandLine.wordCursor());
Expand Down Expand Up @@ -128,23 +123,42 @@ private void addCandidates(List<Candidate> candidates, Iterable<String> cands, S
}
}

private CommandLine findSubcommandLine(CommandLine cmdline, String command) {
for (CommandLine s : cmdline.getSubcommands().values()) {
if (s.getCommandName().equals(command) || Arrays.asList(s.getCommandSpec().aliases()).contains(command)) {
return s;
}

private CommandLine findSubcommandLine(List<String> args, int lastIdx) {
CommandLine out = cmd;
for (int i = 0; i < lastIdx; i++) {
if (!args.get(i).startsWith("-")) {
out = findSubcommandLine(out, args.get(i));
if (out == null) {
break;
}
}
return null;
}
return out;
}

private CommandLine findSubcommandLine(CommandLine cmdline, String command) {
for (CommandLine s : cmdline.getSubcommands().values()) {
if (s.getCommandName().equals(command) || Arrays.asList(s.getCommandSpec().aliases()).contains(command)) {
return s;
}
}
return null;
}

/**
*
* @param command
* @return command description for JLine TailTipWidgets to be displayed in terminal status bar.
*/
public CmdDesc commandDescription(String command) {
CommandSpec spec = cmd.getSubcommands().get(command).getCommandSpec();
@Override
public CmdDesc commandDescription(List<String> args) {
CommandLine sub = findSubcommandLine(args, args.size());
if (sub == null) {
return null;
}
CommandSpec spec = sub.getCommandSpec();
Help cmdhelp= new picocli.CommandLine.Help(spec);
List<AttributedString> main = new ArrayList<>();
Map<String, List<AttributedString>> options = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public static void main(String[] args) {
.build();
builtins.setLineReader(reader);
commands.setReader(reader);
new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
TailTipWidgets ttw = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
ttw.setDescriptionCache(false);
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));

Expand All @@ -194,6 +195,7 @@ public static void main(String[] args) {
} catch (EndOfFileException e) {
return;
} catch (Exception e) {
e.printStackTrace();
systemRegistry.trace(e);
}
}
Expand Down

0 comments on commit 6dbaf3a

Please sign in to comment.