mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +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;
|
package pro.gravit.utils.command;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.jline.reader.Candidate;
|
import org.jline.reader.Candidate;
|
||||||
import pro.gravit.utils.helper.VerifyHelper;
|
import pro.gravit.utils.helper.VerifyHelper;
|
||||||
|
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
public Map<String, Command> childCommands = new HashMap<>();
|
||||||
|
|
||||||
protected static String parseUsername(String username) throws CommandException {
|
protected static String parseUsername(String username) throws CommandException {
|
||||||
try {
|
try {
|
||||||
|
@ -39,20 +37,31 @@ public Candidate buildCandidate(CommandHandler.Category category, String command
|
||||||
|
|
||||||
public List<Candidate> complete(List<String> words, int wordIndex, String word)
|
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<>();
|
verifyArgs(args, 1);
|
||||||
for(String s : commands)
|
Command command = childCommands.get(args[0]);
|
||||||
{
|
if(command == null) throw new CommandException(String.format("Unknown sub command: '%s'", args[0]));
|
||||||
if(word.startsWith(s))
|
command.invoke(Arrays.copyOfRange(args, 1, args.length));
|
||||||
{
|
|
||||||
candidates.add(new Candidate(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return candidates;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pro.gravit.utils.command;
|
package pro.gravit.utils.command;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jline.reader.Candidate;
|
import org.jline.reader.Candidate;
|
||||||
|
@ -47,7 +48,8 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Command target = findCommand(line.words().get(0));
|
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);
|
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