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-08-08 01:42:11 +02:00
|
|
|
|
import fr.pandacube.lib.cli.commands.CLIBrigadierDispatcher;
|
|
|
|
|
import fr.pandacube.lib.cli.log.CLILogger;
|
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
|
|
|
|
|
2022-08-11 01:32:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* Class to hangle general standard IO operation for a CLI application. It uses Jline’s {@link ConsoleReader} for the
|
2022-12-02 12:45:53 +01:00
|
|
|
|
* console rendering, a JUL {@link Logger} for logging, and Brigadier to handle commands.
|
2022-08-11 01:32:37 +02:00
|
|
|
|
*/
|
2022-12-02 12:45:53 +01:00
|
|
|
|
public class CLI extends Thread {
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
|
private final ConsoleReader reader;
|
|
|
|
|
private final Logger logger;
|
2022-08-11 01:32:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new instance of {@link CLI}.
|
|
|
|
|
* @throws IOException if an IO error occurs.
|
|
|
|
|
*/
|
2021-08-23 02:23:01 +02:00
|
|
|
|
public CLI() throws IOException {
|
2022-12-02 12:45:53 +01:00
|
|
|
|
super("Console Thread");
|
|
|
|
|
setDaemon(true);
|
|
|
|
|
|
2021-08-23 02:23:01 +02:00
|
|
|
|
AnsiConsole.systemInstall();
|
2021-08-20 01:48:49 +02:00
|
|
|
|
reader = new ConsoleReader();
|
2023-04-10 19:54:11 +02:00
|
|
|
|
reader.setPrompt(">");
|
2022-08-08 01:42:11 +02:00
|
|
|
|
reader.addCompleter(CLIBrigadierDispatcher.instance);
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-08-11 01:32:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the Jline {@link ConsoleReader} of this CLI instance.
|
|
|
|
|
* @return the Jline {@link ConsoleReader} of this CLI instance.
|
|
|
|
|
*/
|
2021-08-20 01:48:49 +02:00
|
|
|
|
public ConsoleReader getConsoleReader() {
|
|
|
|
|
return reader;
|
|
|
|
|
}
|
2022-08-11 01:32:37 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the {@link Logger} of this CLI instance.
|
|
|
|
|
* @return the {@link Logger} of this CLI instance.
|
|
|
|
|
*/
|
2021-08-23 03:23:16 +02:00
|
|
|
|
public Logger getLogger() {
|
2021-08-23 02:23:01 +02:00
|
|
|
|
return logger;
|
|
|
|
|
}
|
2022-08-11 01:32:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runs the main loop of the console interface. This method will not return until the input stream is closed.
|
|
|
|
|
*/
|
2022-12-02 12:45:53 +01:00
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2021-08-20 01:48:49 +02:00
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
String line;
|
|
|
|
|
try {
|
|
|
|
|
while((line = reader.readLine()) != null) {
|
|
|
|
|
if (line.trim().equals(""))
|
|
|
|
|
continue;
|
2022-12-02 13:06:04 +01:00
|
|
|
|
String cmdLine = line;
|
|
|
|
|
new Thread(() -> CLIBrigadierDispatcher.instance.execute(cmdLine), "CLICmdThread #"+(i++)).start();
|
2021-08-20 01:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.severe(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|