Search in sources :

Example 1 with LineReader

use of org.jline.reader.LineReader in project felix by apache.

the class Builtin method __files.

public List<Candidate> __files(CommandSession session) {
    ParsedLine line = Shell.getParsedLine(session);
    LineReader reader = Shell.getReader(session);
    List<Candidate> candidates = new ArrayList<>();
    new FilesCompleter(session.currentDir()) {

        @Override
        protected String getDisplay(Terminal terminal, Path p) {
            return getFileDisplay(session, p);
        }
    }.complete(reader, line, candidates);
    return candidates;
}
Also used : Candidate(org.jline.reader.Candidate) Path(java.nio.file.Path) ParsedLine(org.jline.reader.ParsedLine) LineReader(org.jline.reader.LineReader) ArrayList(java.util.ArrayList) FilesCompleter(org.jline.builtins.Completers.FilesCompleter) Terminal(org.jline.terminal.Terminal)

Example 2 with LineReader

use of org.jline.reader.LineReader in project felix by apache.

the class Builtin method __directories.

public List<Candidate> __directories(CommandSession session) {
    ParsedLine line = Shell.getParsedLine(session);
    LineReader reader = Shell.getReader(session);
    List<Candidate> candidates = new ArrayList<>();
    new DirectoriesCompleter(session.currentDir()) {

        @Override
        protected String getDisplay(Terminal terminal, Path p) {
            return getFileDisplay(session, p);
        }
    }.complete(reader, line, candidates);
    return candidates;
}
Also used : Candidate(org.jline.reader.Candidate) Path(java.nio.file.Path) DirectoriesCompleter(org.jline.builtins.Completers.DirectoriesCompleter) ParsedLine(org.jline.reader.ParsedLine) LineReader(org.jline.reader.LineReader) ArrayList(java.util.ArrayList) Terminal(org.jline.terminal.Terminal)

Example 3 with LineReader

use of org.jline.reader.LineReader in project nifi by apache.

the class CLIMain method runInteractiveCLI.

/**
 * Runs the interactive CLI.
 *
 * @throws IOException if an error occurs
 */
private static void runInteractiveCLI() throws IOException {
    // Logger.getLogger("org.jline").setLevel(Level.FINE);
    try (final Terminal terminal = TerminalBuilder.builder().name(SHELL_NAME).system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
        final PrintStream output = new PrintStream(terminal.output(), true)) {
        printHeader(BANNER_FILE, output);
        final Context context = createContext(output, true);
        final Map<String, Command> topLevelCommands = CommandFactory.createTopLevelCommands(context);
        final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
        final CommandProcessor commandProcessor = new CommandProcessor(topLevelCommands, commandGroups, context);
        final Completer completer = new CLICompleter(topLevelCommands.values(), commandGroups.values());
        final LineReader reader = LineReaderBuilder.builder().appName(SHELL_NAME).terminal(terminal).completer(completer).build();
        reader.setOpt(LineReader.Option.AUTO_FRESH_LINE);
        reader.unsetOpt(LineReader.Option.INSERT_TAB);
        while (true) {
            try {
                final String line = reader.readLine(PROMPT);
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                final ParsedLine parsedLine = reader.getParsedLine();
                final String[] parsedArgs = parsedLine.words().toArray(new String[parsedLine.words().size()]);
                commandProcessor.process(parsedArgs);
            } catch (UserInterruptException e) {
            // Ignore
            } catch (EndOfFileException e) {
                return;
            }
        }
    }
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) PrintStream(java.io.PrintStream) EndOfFileException(org.jline.reader.EndOfFileException) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) Completer(org.jline.reader.Completer) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal) Command(org.apache.nifi.toolkit.cli.api.Command) LineReader(org.jline.reader.LineReader) ParsedLine(org.jline.reader.ParsedLine) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)

Example 4 with LineReader

use of org.jline.reader.LineReader in project herddb by diennea.

the class HerdDBCLI method runSqlConsole.

private static void runSqlConsole(Connection connection, Statement statement, boolean pretty) throws IOException {
    Terminal terminal = TerminalBuilder.builder().system(true).build();
    LineReader reader = LineReaderBuilder.builder().history(new DefaultHistory()).terminal(terminal).build();
    String prompt = "herd: ";
    while (true) {
        String line = null;
        try {
            line = reader.readLine(prompt);
            if (line == null) {
                return;
            }
            executeStatement(true, true, false, false, line, statement, null, false, pretty);
        } catch (UserInterruptException | EndOfFileException e) {
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) LineReader(org.jline.reader.LineReader) DefaultHistory(org.jline.reader.impl.history.DefaultHistory) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal) ScriptException(javax.script.ScriptException) EndOfFileException(org.jline.reader.EndOfFileException) ParseException(org.apache.commons.cli.ParseException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 5 with LineReader

use of org.jline.reader.LineReader in project flink by apache.

the class CliClientTest method verifySqlCompletion.

private void verifySqlCompletion(String statement, int position, List<String> expectedHints) throws IOException {
    final MockExecutor mockExecutor = new MockExecutor();
    String sessionId = mockExecutor.openSession("test-session");
    final SqlCompleter completer = new SqlCompleter(sessionId, mockExecutor);
    final SqlMultiLineParser parser = new SqlMultiLineParser(new SqlCommandParserImpl(mockExecutor, sessionId));
    try (Terminal terminal = TerminalUtils.createDumbTerminal()) {
        final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();
        final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
        final List<Candidate> candidates = new ArrayList<>();
        final List<String> results = new ArrayList<>();
        completer.complete(reader, parsedLine, candidates);
        candidates.forEach(item -> results.add(item.value()));
        assertThat(results.containsAll(expectedHints)).isTrue();
        assertThat(statement).isEqualTo(mockExecutor.receivedStatement);
        assertThat(position).isEqualTo(mockExecutor.receivedPosition);
    }
}
Also used : Candidate(org.jline.reader.Candidate) LineReader(org.jline.reader.LineReader) ParsedLine(org.jline.reader.ParsedLine) ArrayList(java.util.ArrayList) DumbTerminal(org.jline.terminal.impl.DumbTerminal) Terminal(org.jline.terminal.Terminal)

Aggregations

LineReader (org.jline.reader.LineReader)19 Terminal (org.jline.terminal.Terminal)13 EndOfFileException (org.jline.reader.EndOfFileException)9 UserInterruptException (org.jline.reader.UserInterruptException)9 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 ParsedLine (org.jline.reader.ParsedLine)4 Candidate (org.jline.reader.Candidate)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 PrintWriter (java.io.PrintWriter)2 Path (java.nio.file.Path)2 CommandException (org.glassfish.api.admin.CommandException)2 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)2 Completer (org.jline.reader.Completer)2 ExternalTerminal (org.jline.terminal.impl.ExternalTerminal)2 SqlResult (com.hazelcast.sql.SqlResult)1 BufferedReader (java.io.BufferedReader)1