CLI is now its own thread

This commit is contained in:
Marc Baloup 2022-12-02 12:45:53 +01:00
parent 862c164cf3
commit f3efe00b8b
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893

View File

@ -12,9 +12,9 @@ import fr.pandacube.lib.util.Log;
/** /**
* Class to hangle general standard IO operation for a CLI application. It uses Jlines {@link ConsoleReader} for the * Class to hangle general standard IO operation for a CLI application. It uses Jlines {@link ConsoleReader} for the
* console rendering, a JUL {@link Logger} for logging, and Brigadier for the command management. * console rendering, a JUL {@link Logger} for logging, and Brigadier to handle commands.
*/ */
public class CLI { public class CLI extends Thread {
private final ConsoleReader reader; private final ConsoleReader reader;
private final Logger logger; private final Logger logger;
@ -25,9 +25,11 @@ public class CLI {
* @throws IOException if an IO error occurs. * @throws IOException if an IO error occurs.
*/ */
public CLI() throws IOException { public CLI() throws IOException {
super("Console Thread");
setDaemon(true);
AnsiConsole.systemInstall(); AnsiConsole.systemInstall();
reader = new ConsoleReader(); reader = new ConsoleReader();
reader.setBellEnabled(false);
reader.setPrompt("\r>"); reader.setPrompt("\r>");
reader.addCompleter(CLIBrigadierDispatcher.instance); reader.addCompleter(CLIBrigadierDispatcher.instance);
@ -56,9 +58,9 @@ public class CLI {
/** /**
* Runs the main loop of the console interface. This method will not return until the input stream is closed. * 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() { @Override
public void run() {
int i = 0; int i = 0;
String line; String line;
@ -66,8 +68,7 @@ public class CLI {
while((line = reader.readLine()) != null) { while((line = reader.readLine()) != null) {
if (line.trim().equals("")) if (line.trim().equals(""))
continue; continue;
String cmdLine = line; CLIBrigadierDispatcher.instance.execute(line);
new Thread(() -> CLIBrigadierDispatcher.instance.execute(cmdLine), "CLICmdThread #"+(i++)).start();
} }
} catch (IOException e) { } catch (IOException e) {
Log.severe(e); Log.severe(e);