mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
[FEATURE] Субкомманды и автодополнение для них
This commit is contained in:
parent
752fb9e879
commit
5b7ae04fbd
3 changed files with 40 additions and 16 deletions
|
@ -1,14 +1,12 @@
|
|||
package pro.gravit.utils.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
import org.jline.reader.Candidate;
|
||||
import pro.gravit.utils.helper.VerifyHelper;
|
||||
|
||||
public abstract class Command {
|
||||
|
||||
public Map<String, Command> childCommands = new HashMap<>();
|
||||
|
||||
protected static String parseUsername(String username) throws CommandException {
|
||||
try {
|
||||
|
@ -39,20 +37,31 @@ public Candidate buildCandidate(CommandHandler.Category category, String command
|
|||
|
||||
public List<Candidate> complete(List<String> words, int wordIndex, String word)
|
||||
{
|
||||
return new ArrayList<>();
|
||||
if(wordIndex == 0)
|
||||
{
|
||||
List<Candidate> candidates = new ArrayList<>();
|
||||
childCommands.forEach((k,v) -> {
|
||||
if(k.startsWith(word))
|
||||
{
|
||||
candidates.add(new Candidate(k));
|
||||
}
|
||||
});
|
||||
return candidates;
|
||||
}
|
||||
else
|
||||
{
|
||||
Command cmd = childCommands.get(words.get(0));
|
||||
if(cmd == null) return new ArrayList<>();
|
||||
return cmd.complete(words.subList(1, words.size()), wordIndex - 1, word);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Candidate> subCommandComplete(String word, List<String> commands)
|
||||
public void invokeSubcommands(String... args) throws Exception
|
||||
{
|
||||
List<Candidate> candidates = new ArrayList<>();
|
||||
for(String s : commands)
|
||||
{
|
||||
if(word.startsWith(s))
|
||||
{
|
||||
candidates.add(new Candidate(s));
|
||||
}
|
||||
}
|
||||
return candidates;
|
||||
verifyArgs(args, 1);
|
||||
Command command = childCommands.get(args[0]);
|
||||
if(command == null) throw new CommandException(String.format("Unknown sub command: '%s'", args[0]));
|
||||
command.invoke(Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package pro.gravit.utils.command;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.jline.reader.Candidate;
|
||||
|
@ -47,7 +48,8 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
|
|||
else
|
||||
{
|
||||
Command target = findCommand(line.words().get(0));
|
||||
List<Candidate> candidates1 = target.complete(line.words(), line.wordIndex(), completeWord);
|
||||
List<String> words = line.words();
|
||||
List<Candidate> candidates1 = target.complete(words.subList(1, words.size()), line.wordIndex() - 1, completeWord);
|
||||
candidates.addAll(candidates1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package pro.gravit.utils.command;
|
||||
|
||||
public abstract class SubCommand extends Command {
|
||||
@Override
|
||||
public String getArgsDescription() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsageDescription() {
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue