use of org.jline.reader.LineReader in project MinecraftForge by MinecraftForge.
the class TerminalHandler method handleCommands.
public static boolean handleCommands(DedicatedServer server) {
final Terminal terminal = TerminalConsoleAppender.getTerminal();
if (terminal == null)
return false;
LineReader reader = LineReaderBuilder.builder().appName("Forge").terminal(terminal).completer(new ConsoleCommandCompleter(server)).build();
reader.setOpt(LineReader.Option.DISABLE_EVENT_EXPANSION);
reader.unsetOpt(LineReader.Option.INSERT_TAB);
TerminalConsoleAppender.setReader(reader);
try {
String line;
while (!server.isStopped() && server.isRunning()) {
try {
line = reader.readLine("> ");
} catch (EndOfFileException ignored) {
// Continue reading after EOT
continue;
}
if (line == null)
break;
line = line.trim();
if (!line.isEmpty()) {
server.handleConsoleInput(line, server.createCommandSourceStack());
}
}
} catch (UserInterruptException e) {
server.halt(true);
} finally {
TerminalConsoleAppender.setReader(null);
}
return true;
}
use of org.jline.reader.LineReader in project LanternServer by LanternPowered.
the class ConsoleManager method start.
public void start() {
final Terminal terminal = TerminalConsoleAppender.getTerminal();
if (terminal != null) {
final LineReader reader = LineReaderBuilder.builder().appName(this.pluginContainer.getName()).terminal(terminal).completer(new ConsoleCommandCompleter()).build();
reader.unsetOpt(Option.INSERT_TAB);
reader.setVariable(LineReader.HISTORY_FILE, this.consoleHistoryFile);
TerminalConsoleAppender.setReader(reader);
}
active = true;
final Thread thread = new Thread(this::readCommandTask, "console");
thread.setDaemon(true);
thread.start();
this.scheduler.createAsyncExecutor(this.pluginContainer).scheduleAtFixedRate(this::saveHistory, 120, 120, TimeUnit.SECONDS);
}
use of org.jline.reader.LineReader in project LanternServer by LanternPowered.
the class ConsoleManager method readCommandTask.
/**
* This task handles the commands that are executed through the console.
*/
private void readCommandTask() {
final LineReader lineReader = TerminalConsoleAppender.getReader();
final Supplier<String> consoleReader;
if (lineReader != null) {
consoleReader = () -> {
try {
return lineReader.readLine("> ");
} catch (EndOfFileException e) {
return null;
}
};
} else {
this.logger.info("Falling back to non jline console.");
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
consoleReader = () -> {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
};
}
final SpongeExecutorService executor = this.scheduler.createSyncExecutor(this.pluginContainer);
try {
String command;
while (active) {
command = consoleReader.get();
if (command != null) {
command = command.trim();
if (!command.isEmpty()) {
final String runCommand = command.startsWith("/") ? command.substring(1) : command;
executor.execute(() -> this.commandManager.process(LanternConsoleSource.INSTANCE, runCommand));
}
}
}
} catch (UserInterruptException e) {
// Already set the reader to null, to avoid printing a new line
TerminalConsoleAppender.setReader(null);
// When a user interrupts the console, for example Ctrl-C
// Shutdown the server
executor.execute(() -> Lantern.getServer().shutdown());
}
}
use of org.jline.reader.LineReader in project flink by apache.
the class CliClient method executeInteractive.
// --------------------------------------------------------------------------------------------
/**
* Execute statement from the user input and prints status information and/or errors on the
* terminal.
*/
private void executeInteractive() {
// make space from previous output and test the writer
terminal.writer().println();
terminal.writer().flush();
// print welcome
terminal.writer().append(CliStrings.MESSAGE_WELCOME);
LineReader lineReader = createLineReader(terminal);
getAndExecuteStatements(lineReader, ExecutionMode.INTERACTIVE_EXECUTION);
}
use of org.jline.reader.LineReader in project flink by apache.
the class CliClient method createLineReader.
private LineReader createLineReader(Terminal terminal) {
// initialize line lineReader
LineReader lineReader = LineReaderBuilder.builder().terminal(terminal).appName(CliStrings.CLI_NAME).parser(parser).completer(new SqlCompleter(sessionId, executor)).build();
// this option is disabled for now for correct backslash escaping
// a "SELECT '\'" query should return a string with a backslash
lineReader.option(LineReader.Option.DISABLE_EVENT_EXPANSION, true);
// set strict "typo" distance between words when doing code completion
lineReader.setVariable(LineReader.ERRORS, 1);
// perform code completion case insensitive
lineReader.option(LineReader.Option.CASE_INSENSITIVE, true);
// set history file path
if (Files.exists(historyFilePath) || CliUtils.createFile(historyFilePath)) {
String msg = "Command history file path: " + historyFilePath;
// print it in the command line as well as log file
terminal.writer().println(msg);
LOG.info(msg);
lineReader.setVariable(LineReader.HISTORY_FILE, historyFilePath);
} else {
String msg = "Unable to create history file: " + historyFilePath;
terminal.writer().println(msg);
LOG.warn(msg);
}
return lineReader;
}
Aggregations