Search in sources :

Example 21 with LineReader

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;
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) LineReader(org.jline.reader.LineReader) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal)

Example 22 with LineReader

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);
}
Also used : LineReader(org.jline.reader.LineReader) Terminal(org.jline.terminal.Terminal)

Example 23 with LineReader

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());
    }
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) InputStreamReader(java.io.InputStreamReader) LineReader(org.jline.reader.LineReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) SpongeExecutorService(org.spongepowered.api.scheduler.SpongeExecutorService) UserInterruptException(org.jline.reader.UserInterruptException)

Example 24 with LineReader

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);
}
Also used : LineReader(org.jline.reader.LineReader)

Example 25 with LineReader

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;
}
Also used : LineReader(org.jline.reader.LineReader)

Aggregations

LineReader (org.jline.reader.LineReader)27 Terminal (org.jline.terminal.Terminal)15 EndOfFileException (org.jline.reader.EndOfFileException)10 UserInterruptException (org.jline.reader.UserInterruptException)10 IOException (java.io.IOException)7 AccumuloClient (org.apache.accumulo.core.client.AccumuloClient)5 Shell (org.apache.accumulo.shell.Shell)5 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 SecurityOperations (org.apache.accumulo.core.client.admin.SecurityOperations)4 CommandLine (org.apache.commons.cli.CommandLine)4 ParsedLine (org.jline.reader.ParsedLine)4 Test (org.junit.Test)4 PrintStream (java.io.PrintStream)3 Authorizations (org.apache.accumulo.core.security.Authorizations)3 Candidate (org.jline.reader.Candidate)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2