[FEATURE] Автодополнение для параметров комманд

This commit is contained in:
Gravit 2019-07-06 17:58:25 +07:00
parent fd9e2f9bd8
commit 752fb9e879
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 40 additions and 6 deletions

View file

@ -1,7 +1,10 @@
package pro.gravit.utils.command;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.jline.reader.Candidate;
import pro.gravit.utils.helper.VerifyHelper;
public abstract class Command {
@ -29,6 +32,29 @@ protected static UUID parseUUID(String s) throws CommandException {
public abstract String getUsageDescription();
public Candidate buildCandidate(CommandHandler.Category category, String commandName)
{
return new Candidate(commandName);
}
public List<Candidate> complete(List<String> words, int wordIndex, String word)
{
return new ArrayList<>();
}
public List<Candidate> subCommandComplete(String word, List<String> commands)
{
List<Candidate> candidates = new ArrayList<>();
for(String s : commands)
{
if(word.startsWith(s))
{
candidates.add(new Candidate(s));
}
}
return candidates;
}
public abstract void invoke(String... args) throws Exception;

View file

@ -36,13 +36,21 @@ public class JLineConsoleCompleter implements Completer {
@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
String completeWord = line.word();
if (line.wordIndex() != 0) return;
if (line.wordIndex() == 0)
{
walk((category, name, command) -> {
if (name.startsWith(completeWord)) {
candidates.add(new Candidate(name));
candidates.add(command.buildCandidate(category, name));
}
});
}
else
{
Command target = findCommand(line.words().get(0));
List<Candidate> candidates1 = target.complete(line.words(), line.wordIndex(), completeWord);
candidates.addAll(candidates1);
}
}
}
public JLineCommandHandler() throws IOException {