2021-08-20 01:48:49 +02:00
|
|
|
package fr.pandacube.lib.cli;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2021-08-23 03:23:16 +02:00
|
|
|
import java.util.logging.Logger;
|
2021-08-23 02:23:01 +02:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import jline.console.ConsoleReader;
|
2021-08-23 02:23:01 +02:00
|
|
|
import org.fusesource.jansi.AnsiConsole;
|
2021-08-20 01:48:49 +02:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import fr.pandacube.lib.util.Log;
|
2021-08-20 01:48:49 +02:00
|
|
|
|
2021-08-23 02:23:01 +02:00
|
|
|
public class CLI {
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final ConsoleReader reader;
|
|
|
|
private final Logger logger;
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
|
2021-08-23 02:23:01 +02:00
|
|
|
public CLI() throws IOException {
|
|
|
|
AnsiConsole.systemInstall();
|
2021-08-20 01:48:49 +02:00
|
|
|
reader = new ConsoleReader();
|
|
|
|
reader.setBellEnabled(false);
|
2021-08-23 03:23:16 +02:00
|
|
|
reader.setPrompt("\r>");
|
2021-08-20 01:48:49 +02:00
|
|
|
reader.addCompleter(BrigadierDispatcher.instance);
|
|
|
|
|
|
|
|
// configuration du formatteur pour le logger
|
|
|
|
System.setProperty("net.md_5.bungee.log-date-format", "yyyy-MM-dd HH:mm:ss");
|
2021-08-23 03:23:16 +02:00
|
|
|
logger = CLILogger.getLogger(this);
|
2021-08-20 01:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ConsoleReader getConsoleReader() {
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-23 03:23:16 +02:00
|
|
|
public Logger getLogger() {
|
2021-08-23 02:23:01 +02:00
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
public void loop() {
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
String line;
|
|
|
|
try {
|
|
|
|
while((line = reader.readLine()) != null) {
|
|
|
|
if (line.trim().equals(""))
|
|
|
|
continue;
|
|
|
|
String cmdLine = line;
|
2022-07-10 00:55:56 +02:00
|
|
|
new Thread(() -> BrigadierDispatcher.instance.execute(cmdLine), "CLICmdThread #"+(i++)).start();
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.severe(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|