diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java index f5373c81..45696e2b 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; public abstract class CommandHandler implements Runnable { private final List categories = new ArrayList<>(); @@ -130,6 +131,21 @@ public void run() { LogHelper.error(e); } } + @FunctionalInterface + public interface CommandWalk + { + void walk(Category category, String name, Command command); + } + public void walk(CommandWalk callback) + { + for(CommandHandler.Category category : getCategories()) + { + for (Map.Entry entry : category.category.commandsMap().entrySet()) + callback.walk(category, entry.getKey(), entry.getValue()); + } + for (Map.Entry entry : getBaseCategory().commandsMap().entrySet()) + callback.walk(null, entry.getKey(), entry.getValue()); + } public CommandCategory getBaseCategory() { return baseCategory; diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java index 83b68b4a..bdd83e81 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java @@ -1,7 +1,6 @@ package ru.gravit.utils.command; -import org.jline.reader.LineReader; -import org.jline.reader.LineReaderBuilder; +import org.jline.reader.*; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; import org.jline.utils.InfoCmp; @@ -9,6 +8,7 @@ import ru.gravit.utils.helper.LogHelper.Output; import java.io.IOException; +import java.util.List; public class JLineCommandHandler extends CommandHandler { /*private final class JLineOutput implements Output { @@ -26,14 +26,30 @@ public void println(String message) { private final Terminal terminal; private final TerminalBuilder terminalBuilder; + private final Completer completer; private final LineReader reader; + 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)); + } + }); + } + } public JLineCommandHandler() throws IOException { super(); terminalBuilder = TerminalBuilder.builder(); terminal = terminalBuilder.build(); + completer = new JLineConsoleCompleter(); reader = LineReaderBuilder.builder() .terminal(terminal) + .completer(completer) .build(); // Set reader @@ -47,7 +63,7 @@ public JLineCommandHandler() throws IOException { @Override public void bell() throws IOException { - + terminal.puts(InfoCmp.Capability.bell); //reader.beep(); }