PicocliCommands.commandDescription(): resolve also sub-command descs

This commit is contained in:
mattirn
2020-04-10 18:22:53 +02:00
committed by Remko Popma
parent fb11907175
commit 6dbaf3a472
4 changed files with 34 additions and 17 deletions

View File

@@ -74,6 +74,7 @@ allprojects {
compileTestJava.options.encoding = "UTF-8"
repositories {
mavenLocal()
jcenter()
}

View File

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

View File

@@ -84,14 +84,9 @@ public class PicocliCommands implements CommandRegistry {
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());
@@ -128,14 +123,28 @@ public class PicocliCommands implements CommandRegistry {
}
}
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;
}
/**
@@ -143,8 +152,13 @@ public class PicocliCommands implements CommandRegistry {
* @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<>();

View File

@@ -175,7 +175,8 @@ public class Example {
.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"));
@@ -194,6 +195,7 @@ public class Example {
} catch (EndOfFileException e) {
return;
} catch (Exception e) {
e.printStackTrace();
systemRegistry.trace(e);
}
}