Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line arrow keys support #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>sqlite-jdbc</artifactId>
<version>3.41.2.1</version>
</dependency>

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.23.0</version>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 15 additions & 2 deletions src/me/koply/pikap/api/cli/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@

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;
import java.util.logging.Logger;

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() {
Expand Down
16 changes: 13 additions & 3 deletions src/me/koply/pikap/api/cli/command/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,8 @@
public class CommandHandler {

public static final Map<String, CommandClassData> COMMAND_CLASSES = new HashMap<>();
private static final String CONSOLE_PREFIX = "> ";

private final CommandInitializer initializer = new CommandInitializer(COMMAND_CLASSES);

public CommandHandler(String...packages) {
Expand All @@ -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")) {
Expand Down Expand Up @@ -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";
}
}
}
4 changes: 2 additions & 2 deletions src/me/koply/pikap/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down