From 325a7c25076136997049b6a6e40d8cf57bac9eea Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:53:43 +0700 Subject: [PATCH] console command tab completion lol --- .../chomens_bot/plugins/ConsolePlugin.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java index f282a40..bbe7a53 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java @@ -3,18 +3,18 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Configuration; import land.chipmunk.chayapak.chomens_bot.Main; +import land.chipmunk.chayapak.chomens_bot.command.Command; import land.chipmunk.chayapak.chomens_bot.command.ConsoleCommandContext; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import net.dv8tion.jda.api.JDA; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import org.jline.reader.LineReader; -import org.jline.reader.LineReaderBuilder; +import org.jline.reader.*; import java.util.ArrayList; import java.util.List; -public class ConsolePlugin { +public class ConsolePlugin implements Completer { private final List allBots; public final LineReader reader; @@ -27,7 +27,10 @@ public class ConsolePlugin { public ConsolePlugin (List allBots, Configuration discordConfig, JDA jda) { this.allBots = allBots; - this.reader = LineReaderBuilder.builder().build(); + this.reader = LineReaderBuilder + .builder() + .completer(this) + .build(); reader.option(LineReader.Option.DISABLE_EVENT_EXPANSION, true); @@ -59,6 +62,25 @@ public class ConsolePlugin { for (Listener listener : listeners) { listener.ready(); } } + @Override + public void complete(LineReader reader, ParsedLine line, List candidates) { + if (!line.line().startsWith(".")) return; + + final String command = line.line().substring(prefix.length()); + + final List commands = CommandHandlerPlugin.commands; + + final List commandNames = commands.stream().map((eachCommand) -> eachCommand.name).toList(); + + final List filteredCommands = commandNames + .stream() + .filter((eachCommand) -> eachCommand.startsWith(command)) + .map((eachCommand) -> new Candidate(prefix + eachCommand)) + .toList(); + + candidates.addAll(filteredCommands); + } + public void handleLine (String line) { if (line == null) return;