Java Code Examples for jline.console.ConsoleReader#addCompleter()

The following examples show how to use jline.console.ConsoleReader#addCompleter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CmdlineHelper.java    From clue with Apache License 2.0 7 votes vote down vote up
public CmdlineHelper(Supplier<Collection<String>> commandNameSupplier,
                     Supplier<Collection<String>> fieldNameSupplier) throws IOException {
    consoleReader = new ConsoleReader();
    consoleReader.setBellEnabled(false);

    Collection<String> commands = commandNameSupplier != null
            ? commandNameSupplier.get() : Collections.emptyList();

    Collection<String> fields = fieldNameSupplier != null
            ? fieldNameSupplier.get() : Collections.emptyList();

    LinkedList<Completer> completors = new LinkedList<Completer>();
    completors.add(new StringsCompleter(commands));
    completors.add(new StringsCompleter(fields));
    completors.add(new FileNameCompleter());
    consoleReader.addCompleter(new ArgumentCompleter(completors));
}
 
Example 2
Source File: Example.java    From picocli with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    try {
        ConsoleReader reader = new ConsoleReader();
        IFactory factory = new CustomFactory(new InteractiveParameterConsumer(reader));
        
        // set up the completion
        CliCommands commands = new CliCommands(reader);
        CommandLine cmd = new CommandLine(commands, factory);
        reader.addCompleter(new PicocliJLineCompleter(cmd.getCommandSpec()));

        // start the shell and process input until the user quits with Ctl-D
        String line;
        while ((line = reader.readLine("prompt> ")) != null) {
            ArgumentList list = new WhitespaceArgumentDelimiter()
                .delimit(line, line.length());
            new CommandLine(commands, factory)
                .execute(list.getArguments());
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 3
Source File: HeroicInteractiveShell.java    From heroic with Apache License 2.0 6 votes vote down vote up
public static HeroicInteractiveShell buildInstance(
    final List<CommandDefinition> commands, FileInputStream input
) throws Exception {
    final ConsoleReader reader = new ConsoleReader("heroicsh", input, System.out, null);

    final FileHistory history = setupHistory(reader);

    if (history != null) {
        reader.setHistory(history);
    }

    reader.setPrompt(String.format("heroic> "));
    reader.addCompleter(new StringsCompleter(
        ImmutableList.copyOf(commands.stream().map((d) -> d.getName()).iterator())));
    reader.setHandleUserInterrupt(true);

    return new HeroicInteractiveShell(reader, commands, history);
}
 
Example 4
Source File: ConsoleShellFactory.java    From Bukkit-SSHD with Apache License 2.0 6 votes vote down vote up
public void start(Environment env) throws IOException {
    try {
        consoleReader = new ConsoleReader(in, new FlushyOutputStream(out), new SshTerminal());
        consoleReader.setExpandEvents(true);
        consoleReader.addCompleter(new ConsoleCommandCompleter());

        StreamHandler streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), consoleReader);
        streamHandlerAppender = new StreamHandlerAppender(streamHandler);

        ((Logger) LogManager.getRootLogger()).addAppender(streamHandlerAppender);

        environment = env;
        thread = new Thread(this, "SSHD ConsoleShell " + env.getEnv().get(Environment.ENV_USER));
        thread.start();
    } catch (Exception e) {
        throw new IOException("Error starting shell", e);
    }
}
 
Example 5
Source File: MacroBaseSQLRepl.java    From macrobase with Apache License 2.0 5 votes vote down vote up
/**
 * Main entry point to the SQL CLI interface in MacroBase
 *
 * @param userWantsPaging try to enable paging of results in SQL shell
 * @throws IOException if unable to instantiate ConsoleReader
 */
private MacroBaseSQLRepl(final boolean userWantsPaging) throws IOException {
    // First try to turn paging on
    this.paging = enablePaging(userWantsPaging);
    // Initialize console reader and writer
    reader = new ConsoleReader();
    final CandidateListCompletionHandler handler = new CandidateListCompletionHandler();
    handler.setStripAnsi(true);
    reader.setCompletionHandler(handler);
    reader.addCompleter(new FileNameCompleter());

    parser = new SqlParser();
    queryEngine = new QueryEngine();
}
 
Example 6
Source File: Terminal.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Terminal(Configuration configuration) {
    try {
        this.configuration = configuration;

        jline.TerminalFactory.configure("auto");

        reader = new ConsoleReader();
        reader.setHistory(new MemoryHistory());
        reader.addCompleter(new ArgumentCompleter(selectCompleters()));
    
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 7
Source File: ClueCommandClient.java    From clue with Apache License 2.0 5 votes vote down vote up
public void run() throws Exception {
    ConsoleReader consoleReader = new ConsoleReader();
    consoleReader.setBellEnabled(false);

    Collection<String> commands = getCommands();

    LinkedList<Completer> completors = new LinkedList<Completer>();
    completors.add(new StringsCompleter(commands));

    completors.add(new FileNameCompleter());

    consoleReader.addCompleter(new ArgumentCompleter(completors));



    while(true){
        String line = readCommand();
        if (line == null || line.isEmpty()) continue;
        line = line.trim();
        if ("exit".equals(line)) {
            System.exit(0);
        }
        String[] parts = line.split("\\s");
        if (parts.length > 0){
            String cmd = parts[0];
            String[] cmdArgs = new String[parts.length - 1];
            System.arraycopy(parts, 1, cmdArgs, 0, cmdArgs.length);
            handleCommand(cmd, cmdArgs, System.out);
        }
    }
}
 
Example 8
Source File: ApexCli.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void setupCompleter(ConsoleReader reader)
{
  reader.addCompleter(new AggregateCompleter(defaultCompleters()));
}
 
Example 9
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 4 votes vote down vote up
public Cqlsh() {
    try {
        Config applicationConfig = ConfigFactory.defaultApplication();
        config = ConfigFactory.parseFile(PREFERENCES_FILE).withFallback(applicationConfig);
        paging = config.getBoolean(PROP_PAGING_ENABLED);
        pageSize = config.getInt(PROP_PAGING_SIZE);
        sstables = config.getStringList(PROP_SSTABLES).stream().map(File::new).filter(f -> {
            if (!f.exists()) {
                System.err.printf(CANNOT_FIND_FILE, f.getAbsolutePath());
            }
            return f.exists();
        }).collect(Collectors.toSet());

        boolean persistInfo = false;
        if (sstables.size() > 0) {
            System.out.println(infoMsg("Using previously defined sstables: " + sstables));
            persistInfo = true;
        }

        preferences = config.getBoolean(PROP_PREFERENCES_ENABLED);
        String schema = config.getString(PROP_SCHEMA);
        if (!Strings.isNullOrEmpty(schema)) {
            System.out.printf(infoMsg("Using previously defined schema:%n%s%n"), schema);
            persistInfo = true;
            CassandraUtils.cqlOverride = schema;
        }

        if (persistInfo) {
            System.out.println(PERSISTENCE_NOTE);
        }

        history = new FileHistory(HISTORY_FILE);
        console = new ConsoleReader();
        console.setPrompt(prompt);
        console.setHistory(history);
        console.setHistoryEnabled(true);
        List<Completer> completers = Lists.newArrayList();

        ArgumentCompleter argCompleter = new ArgumentCompleter(
                caselessCompleter("use"),
                new FileNameCompleter()
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("describe"),
                caselessCompleter("sstables", "schema")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("dump"),
                caselessCompleter("where")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("create"),
                caselessCompleter("table")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("paging"),
                caselessCompleter("on", "off")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("persist"),
                caselessCompleter("on", "off")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("schema"),
                new AggregateCompleter(new FileNameCompleter(), caselessCompleter("on", "off"))
        );
        completers.add(argCompleter);
        completers.add(caselessCompleter("exit", "help", "select"));
        for (Completer c : completers) {
            console.addCompleter(c);
        }
        console.setHandleUserInterrupt(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: ApexCli.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
private void setupCompleter(ConsoleReader reader)
{
  reader.addCompleter(new AggregateCompleter(defaultCompleters()));
}
 
Example 11
Source File: ShellCommand.java    From citeproc-java with Apache License 2.0 4 votes vote down vote up
@Override
public int doRun(String[] remainingArgs, InputReader in, PrintWriter out)
        throws OptionParserException, IOException {
    // prepare console
    final ConsoleReader reader = new ConsoleReader();
    reader.setPrompt("> ");
    reader.addCompleter(new ShellCommandCompleter(EXCLUDED_COMMANDS));
    FileHistory history = new FileHistory(new File(
            CSLToolContext.current().getConfigDir(), "shell_history.txt"));
    reader.setHistory(history);

    // enable colored error stream for ANSI terminals
    if (reader.getTerminal().isAnsiSupported()) {
        OutputStream errout = new ErrorOutputStream(reader.getTerminal()
                .wrapOutIfNeeded(System.out));
        System.setErr(new PrintStream(errout, false,
                ((OutputStreamWriter)reader.getOutput()).getEncoding()));
    }

    PrintWriter cout = new PrintWriter(reader.getOutput(), true);

    // print welcome message
    cout.println("Welcome to " + CSLToolContext.current().getToolName() +
            " " + CSLTool.getVersion());
    cout.println();
    cout.println("Type `help' for a list of commands and `help "
            + "<command>' for information");
    cout.println("on a specific command. Type `quit' to exit " +
            CSLToolContext.current().getToolName() + ".");
    cout.println();

    String line;
    ShellContext.enter();
    try {
        line = mainLoop(reader, cout);
    } finally {
        ShellContext.exit();

        // make sure we save the history before we exit
        history.flush();
    }

    // print Goodbye message
    if (line == null) {
        // user pressed Ctrl+D
        cout.println();
    }
    cout.println("Bye!");

    return 0;
}