Search in sources :

Example 6 with EndOfFileException

use of org.jline.reader.EndOfFileException 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)

Aggregations

EndOfFileException (org.jline.reader.EndOfFileException)6 UserInterruptException (org.jline.reader.UserInterruptException)6 LineReader (org.jline.reader.LineReader)5 IOException (java.io.IOException)4 Terminal (org.jline.terminal.Terminal)4 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 PrintStream (java.io.PrintStream)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 JsonSchemaGenerator (com.kjetland.jackson.jsonSchema.JsonSchemaGenerator)1 ReporterConfiguration (com.uber.jaeger.Configuration.ReporterConfiguration)1 SamplerConfiguration (com.uber.jaeger.Configuration.SamplerConfiguration)1 ConstSampler (com.uber.jaeger.samplers.ConstSampler)1 ActiveSpan (io.opentracing.ActiveSpan)1