diff --git a/pom.xml b/pom.xml index 04ef543..b29fc46 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,12 @@ sqlite-jdbc 3.41.2.1 + + + org.jline + jline + 3.23.0 + diff --git a/src/me/koply/pikap/api/cli/Console.java b/src/me/koply/pikap/api/cli/Console.java index c7cace8..0be98d7 100644 --- a/src/me/koply/pikap/api/cli/Console.java +++ b/src/me/koply/pikap/api/cli/Console.java @@ -2,11 +2,15 @@ import com.github.tomaslanger.chalk.Chalk; import me.koply.pikap.Constants; +import org.jline.reader.LineReader; +import org.jline.reader.LineReaderBuilder; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; +import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Scanner; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.LogRecord; @@ -14,11 +18,20 @@ public class Console { - public static final Scanner SC = new Scanner(System.in); public static final Logger log = Logger.getLogger("NPlayer"); + public static LineReader LINE_READER; public static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS"); static { + try { + Terminal terminal = TerminalBuilder.builder().build(); + LINE_READER = LineReaderBuilder.builder().terminal(terminal).build(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + + log.setUseParentHandlers(false); ConsoleHandler consoleHandler = new ConsoleHandler(); consoleHandler.setFormatter(new Formatter() { diff --git a/src/me/koply/pikap/api/cli/command/CommandHandler.java b/src/me/koply/pikap/api/cli/command/CommandHandler.java index bfdb17a..75fb31b 100644 --- a/src/me/koply/pikap/api/cli/command/CommandHandler.java +++ b/src/me/koply/pikap/api/cli/command/CommandHandler.java @@ -3,6 +3,7 @@ import com.github.tomaslanger.chalk.Chalk; import me.koply.pikap.api.cli.Console; import me.koply.pikap.sound.SoundManager; +import org.jline.reader.UserInterruptException; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; @@ -11,6 +12,8 @@ public class CommandHandler { public static final Map COMMAND_CLASSES = new HashMap<>(); + private static final String CONSOLE_PREFIX = "> "; + private final CommandInitializer initializer = new CommandInitializer(COMMAND_CLASSES); public CommandHandler(String...packages) { @@ -28,8 +31,7 @@ public void registerInstance(CLICommand command) { */ public void startNewHandler() { while (isRunning) { - System.out.print(Chalk.on("> ").yellow()); - String entry = Console.SC.nextLine().trim(); + String entry = this.readUserInput(); String[] args = entry.split(" "); if (entry.equalsIgnoreCase("exit") || entry.equalsIgnoreCase("quit")) { @@ -57,5 +59,13 @@ private void callCommandMethod(CommandEvent event, CommandClassData ccd) { } } - + private String readUserInput() { + try { + String prefix = Chalk.on(CONSOLE_PREFIX).yellow().toString(); + return Console.LINE_READER.readLine(prefix).trim(); + } catch (UserInterruptException e) { + isRunning = false; + return "exit"; + } + } } \ No newline at end of file diff --git a/src/me/koply/pikap/util/Util.java b/src/me/koply/pikap/util/Util.java index 2c70083..5c23125 100644 --- a/src/me/koply/pikap/util/Util.java +++ b/src/me/koply/pikap/util/Util.java @@ -88,7 +88,7 @@ public static int readInteger(String text) { Console.println(text + " (Number)"); while (true) { Console.print("> "); - String entry = Console.SC.nextLine(); + String entry = Console.LINE_READER.readLine(); Integer value = parseInt(entry); if (value != null) return value; } @@ -98,7 +98,7 @@ public static int readInteger(String text, int min, int max) { Console.println(text + " (Only numbers with minimum: " + min + ", maximum: " + max + " )"); while (true) { Console.print("> "); - String entry = Console.SC.nextLine(); + String entry = Console.LINE_READER.readLine(); Integer value = parseInt(entry); if (value != null && value >= min && value <= max) return value; }