Javadoc and some API changes
This commit is contained in:
@@ -10,40 +10,20 @@ import org.fusesource.jansi.AnsiConsole;
|
||||
|
||||
import fr.pandacube.lib.util.Log;
|
||||
|
||||
/**
|
||||
* Class to hangle general standard IO operation for a CLI application. It uses Jline’s {@link ConsoleReader} for the
|
||||
* console rendering, a JUL {@link Logger} for logging, and Brigadier for the command management.
|
||||
*/
|
||||
public class CLI {
|
||||
|
||||
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
|
||||
public static final String ANSI_BLACK = "\u001B[30m";
|
||||
public static final String ANSI_DARK_RED = "\u001B[31m";
|
||||
public static final String ANSI_DARK_GREEN = "\u001B[32m";
|
||||
public static final String ANSI_GOLD = "\u001B[33m";
|
||||
public static final String ANSI_DARK_BLUE = "\u001B[34m";
|
||||
public static final String ANSI_DARK_PURPLE = "\u001B[35m";
|
||||
public static final String ANSI_DARK_AQUA = "\u001B[36m";
|
||||
public static final String ANSI_GRAY = "\u001B[37m";
|
||||
|
||||
public static final String ANSI_DARK_GRAY = "\u001B[30;1m";
|
||||
public static final String ANSI_RED = "\u001B[31;1m";
|
||||
public static final String ANSI_GREEN = "\u001B[32;1m";
|
||||
public static final String ANSI_YELLOW = "\u001B[33;1m";
|
||||
public static final String ANSI_BLUE = "\u001B[34;1m";
|
||||
public static final String ANSI_LIGHT_PURPLE = "\u001B[35;1m";
|
||||
public static final String ANSI_AQUA = "\u001B[36;1m";
|
||||
public static final String ANSI_WHITE = "\u001B[37;1m";
|
||||
|
||||
public static final String ANSI_BOLD = "\u001B[1m";
|
||||
|
||||
public static final String ANSI_CLEAR_SCREEN = "\u001B[2J\u001B[1;1H";
|
||||
|
||||
|
||||
|
||||
|
||||
private final ConsoleReader reader;
|
||||
private final Logger logger;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance of {@link CLI}.
|
||||
* @throws IOException if an IO error occurs.
|
||||
*/
|
||||
public CLI() throws IOException {
|
||||
AnsiConsole.systemInstall();
|
||||
reader = new ConsoleReader();
|
||||
@@ -55,22 +35,29 @@ public class CLI {
|
||||
System.setProperty("net.md_5.bungee.log-date-format", "yyyy-MM-dd HH:mm:ss");
|
||||
logger = CLILogger.getLogger(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the Jline {@link ConsoleReader} of this CLI instance.
|
||||
* @return the Jline {@link ConsoleReader} of this CLI instance.
|
||||
*/
|
||||
public ConsoleReader getConsoleReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the {@link Logger} of this CLI instance.
|
||||
* @return the {@link Logger} of this CLI instance.
|
||||
*/
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Runs the main loop of the console interface. This method will not return until the input stream is closed.
|
||||
* Every command will be send to the command handler asynchronously.
|
||||
*/
|
||||
public void loop() {
|
||||
|
||||
int i = 0;
|
||||
@@ -81,7 +68,6 @@ public class CLI {
|
||||
continue;
|
||||
String cmdLine = line;
|
||||
new Thread(() -> CLIBrigadierDispatcher.instance.execute(cmdLine), "CLICmdThread #"+(i++)).start();
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.severe(e);
|
||||
|
@@ -1,25 +1,22 @@
|
||||
package fr.pandacube.lib.cli.commands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import fr.pandacube.lib.commands.BrigadierCommand;
|
||||
import fr.pandacube.lib.commands.SuggestionsSupplier;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.ParsedCommandNode;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
|
||||
import fr.pandacube.lib.commands.BrigadierCommand;
|
||||
import fr.pandacube.lib.commands.BrigadierSuggestionsUtil;
|
||||
import fr.pandacube.lib.commands.SuggestionsSupplier;
|
||||
import fr.pandacube.lib.util.Log;
|
||||
|
||||
/**
|
||||
* Abstract class that holds the logic of a specific command to be registered in {@link CLIBrigadierDispatcher}.
|
||||
*/
|
||||
public abstract class CLIBrigadierCommand extends BrigadierCommand<Object> {
|
||||
|
||||
/**
|
||||
* Instanciate this command instance.
|
||||
*/
|
||||
public CLIBrigadierCommand() {
|
||||
LiteralCommandNode<Object> commandNode = buildCommand().build();
|
||||
postBuildCommand(commandNode);
|
||||
@@ -28,7 +25,7 @@ public abstract class CLIBrigadierCommand extends BrigadierCommand<Object> {
|
||||
aliases = new String[0];
|
||||
|
||||
CLIBrigadierDispatcher.instance.register(commandNode);
|
||||
|
||||
|
||||
|
||||
for (String alias : aliases) {
|
||||
CLIBrigadierDispatcher.instance.register(literal(alias)
|
||||
@@ -66,6 +63,11 @@ public abstract class CLIBrigadierCommand extends BrigadierCommand<Object> {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Wraps the provided {@link SuggestionsSupplier} into a Brigadier’s {@link SuggestionProvider}.
|
||||
* @param suggestions the suggestions to wrap.
|
||||
* @return a {@link SuggestionProvider} generating the suggestions from the provided {@link SuggestionsSupplier}.
|
||||
*/
|
||||
protected SuggestionProvider<Object> wrapSuggestions(SuggestionsSupplier<Object> suggestions) {
|
||||
return wrapSuggestions(suggestions, Function.identity());
|
||||
}
|
||||
|
@@ -11,14 +11,25 @@ import fr.pandacube.lib.util.Log;
|
||||
import jline.console.completer.Completer;
|
||||
import net.kyori.adventure.text.ComponentLike;
|
||||
|
||||
/**
|
||||
* Implementation of {@link BrigadierDispatcher} that integrates the commands into the JLine CLI interface.
|
||||
*/
|
||||
public class CLIBrigadierDispatcher extends BrigadierDispatcher<Object> implements Completer {
|
||||
|
||||
|
||||
/**
|
||||
* The instance of {@link CLIBrigadierDispatcher}.
|
||||
*/
|
||||
public static final CLIBrigadierDispatcher instance = new CLIBrigadierDispatcher();
|
||||
|
||||
|
||||
private static final Object sender = new Object();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the provided command.
|
||||
* @param commandWithoutSlash the command, without the eventual slash at the begining.
|
||||
* @return the value returned by the executed command.
|
||||
*/
|
||||
public int execute(String commandWithoutSlash) {
|
||||
return execute(sender, commandWithoutSlash);
|
||||
}
|
||||
@@ -37,7 +48,12 @@ public class CLIBrigadierDispatcher extends BrigadierDispatcher<Object> implemen
|
||||
|
||||
return completeResult.getRange().getStart();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the suggestions for the currently being typed command.
|
||||
* @param buffer the command that is being typed.
|
||||
* @return the suggestions for the currently being typed command.
|
||||
*/
|
||||
public Suggestions getSuggestions(String buffer) {
|
||||
return getSuggestions(sender, buffer);
|
||||
}
|
||||
|
@@ -11,10 +11,18 @@ import net.md_5.bungee.log.ColouredWriter;
|
||||
import net.md_5.bungee.log.ConciseFormatter;
|
||||
import net.md_5.bungee.log.LoggingOutputStream;
|
||||
|
||||
/**
|
||||
* Initializer for the logging system of a CLI application.
|
||||
*/
|
||||
public class CLILogger {
|
||||
|
||||
private static Logger logger = null;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize and return the logger for this application.
|
||||
* @param cli the CLI instance to use
|
||||
* @return the logger of this application.
|
||||
*/
|
||||
public static synchronized Logger getLogger(CLI cli) {
|
||||
if (logger == null) {
|
||||
logger = Logger.getGlobal();
|
||||
|
Reference in New Issue
Block a user