Skip to content

Commit

Permalink
Add description to command registry (fixes jline#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 7, 2024
1 parent 9fb7e27 commit 20b3ecf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 9 additions & 1 deletion console/src/main/java/org/jline/console/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Stream;

import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
Expand Down Expand Up @@ -42,10 +43,17 @@ static SystemCompleter aggregateCompleters(CommandRegistry... commandRegistries)
*/
static SystemCompleter compileCompleters(CommandRegistry... commandRegistries) {
SystemCompleter out = aggregateCompleters(commandRegistries);
out.compile();
out.compile(s -> getDescription(commandRegistries, s));
return out;
}

static String getDescription(CommandRegistry[] commandRegistries, String s) {
return Stream.of(commandRegistries)
.flatMap(r -> r.hasCommand(s) ? r.commandInfo(s).stream() : Stream.empty())
.findFirst()
.orElse(null);
}

/**
* Returns the name of this registry.
* @return the name of the registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private SystemCompleter _compileCompleters() {
}
local.add(customSystemCompleter);
out.add(local);
out.compile();
out.compile(s -> CommandRegistry.getDescription(commandRegistries, s));
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package org.jline.reader.impl.completer;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
Expand All @@ -24,6 +26,7 @@
public class SystemCompleter implements Completer {
private Map<String, List<Completer>> completers = new HashMap<>();
private Map<String, String> aliasCommand = new HashMap<>();
private Map<String, String> descriptions = new HashMap<>();
private StringsCompleter commands;
private boolean compiled = false;

Expand Down Expand Up @@ -123,7 +126,7 @@ private Map<String, String> getAliases() {
return aliasCommand;
}

public void compile() {
public void compile(Function<String, String> descriptionProvider) {
if (compiled) {
return;
}
Expand All @@ -139,7 +142,9 @@ public void compile() {
completers = compiledCompleters;
Set<String> cmds = new HashSet<>(completers.keySet());
cmds.addAll(aliasCommand.keySet());
commands = new StringsCompleter(cmds);
commands = new StringsCompleter(cmds.stream()
.map(s -> new Candidate(s, s, null, descriptionProvider.apply(s), null, null, true, 0))
.collect(Collectors.toList()));
compiled = true;
}

Expand Down

0 comments on commit 20b3ecf

Please sign in to comment.