Search in sources :

Example 1 with Completer

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

Aggregations

PrintStream (java.io.PrintStream)1 Command (org.apache.nifi.toolkit.cli.api.Command)1 CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)1 Context (org.apache.nifi.toolkit.cli.api.Context)1 CommandProcessor (org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)1 StandardContext (org.apache.nifi.toolkit.cli.impl.context.StandardContext)1 Completer (org.jline.reader.Completer)1 EndOfFileException (org.jline.reader.EndOfFileException)1 LineReader (org.jline.reader.LineReader)1 ParsedLine (org.jline.reader.ParsedLine)1 UserInterruptException (org.jline.reader.UserInterruptException)1 Terminal (org.jline.terminal.Terminal)1