Skip to content

Commit

Permalink
Let CommandRegistry create Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 7, 2024
1 parent 2cae4b6 commit 6fe6ac4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
19 changes: 12 additions & 7 deletions console/src/main/java/org/jline/console/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Stream;

import org.jline.reader.Candidate;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;

Expand Down Expand Up @@ -43,15 +43,20 @@ static SystemCompleter aggregateCompleters(CommandRegistry... commandRegistries)
*/
static SystemCompleter compileCompleters(CommandRegistry... commandRegistries) {
SystemCompleter out = aggregateCompleters(commandRegistries);
out.compile(s -> getDescription(commandRegistries, s));
out.compile(s -> createCandidate(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);
static Candidate createCandidate(CommandRegistry[] commandRegistries, String command) {
String group = null, desc = null;
for (CommandRegistry registry : commandRegistries) {
if (registry.hasCommand(command)) {
group = registry.name();
desc = registry.commandInfo(command).stream().findFirst().orElse(null);
break;
}
}
return new Candidate(command, command, group, desc, null, null, true);
}

/**
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(s -> CommandRegistry.getDescription(commandRegistries, s));
out.compile(s -> CommandRegistry.createCandidate(commandRegistries, s));
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Map<String, String> getAliases() {
return aliasCommand;
}

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

Expand Down

0 comments on commit 6fe6ac4

Please sign in to comment.