mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
[FEATURE] Автодополнение для параметров комманд
This commit is contained in:
parent
fd9e2f9bd8
commit
752fb9e879
2 changed files with 40 additions and 6 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -36,12 +36,20 @@ 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;
|
||||
walk((category, name, command) -> {
|
||||
if (name.startsWith(completeWord)) {
|
||||
candidates.add(new Candidate(name));
|
||||
}
|
||||
});
|
||||
if (line.wordIndex() == 0)
|
||||
{
|
||||
walk((category, name, command) -> {
|
||||
if (name.startsWith(completeWord)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue