From 752fb9e87932f3b4949dcbd5c16c8902a3b874e3 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 6 Jul 2019 17:58:25 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=90=D0=B2=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B0=D0=BD=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/utils/command/Command.java | 26 +++++++++++++++++++ .../utils/command/JLineCommandHandler.java | 20 +++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java index 6deff0d9..ebeb96dc 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java @@ -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 complete(List words, int wordIndex, String word) + { + return new ArrayList<>(); + } + + public List subCommandComplete(String word, List commands) + { + List 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; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java index 97566b37..a795c571 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java @@ -36,12 +36,20 @@ public class JLineConsoleCompleter implements Completer { @Override public void complete(LineReader reader, ParsedLine line, List 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 candidates1 = target.complete(line.words(), line.wordIndex(), completeWord); + candidates.addAll(candidates1); + } } }