Search in sources :

Example 1 with StringsCompleter

use of jline.console.completer.StringsCompleter in project zeppelin by apache.

the class SqlCompleter method init.

/**
   * Initializes all local completers
   *
   * @param schemas set of schema names
   * @param tables for every schema name there is a set of table names within the schema
   * @param columns for every table name there is a set of columns within the table;
   *        table name is in format schema_name.table_name
   * @param keywords set with sql keywords
   */
public void init(Set<String> schemas, Map<String, Set<String>> tables, Map<String, Set<String>> columns, Set<String> keywords) {
    initSchemas(schemas);
    initTables(tables);
    initColumns(columns);
    keywordCompleter = new StringsCompleter(keywords);
}
Also used : StringsCompleter(jline.console.completer.StringsCompleter)

Example 2 with StringsCompleter

use of jline.console.completer.StringsCompleter in project ANNIS by korpling.

the class AnnisBaseRunner method runInteractive.

protected void runInteractive() throws IOException {
    System.out.println(helloMessage + " " + VersionInfo.getReleaseName());
    System.out.println();
    System.out.println("Use \"help\" for a list of all commands.");
    System.out.println();
    ConsoleReader console = new ConsoleReader();
    File annisDir = new File(System.getProperty("user.home") + "/.annis/");
    String annisDirPath = annisDir.getAbsolutePath();
    if (!annisDir.exists()) {
        log.info("Creating directory: " + annisDirPath);
        if (!annisDir.mkdirs()) {
            log.warn("Could not create directory: " + annisDirPath);
        }
    } else if (!annisDir.isDirectory()) {
        log.warn("Could not create directory because a file with the same name already exists: " + annisDirPath);
    }
    history = new FileHistory(new File(System.getProperty("user.home") + "/.annis/shellhistory.txt"));
    console.setHistory(history);
    console.setHistoryEnabled(true);
    console.setBellEnabled(true);
    console.setExpandEvents(false);
    List<String> commands = detectAvailableCommands();
    Collections.sort(commands);
    console.addCompleter(new StringsCompleter(commands));
    Splitter argSplitter = Splitter.on(' ').limit(2);
    String line;
    StringBuilder input = new StringBuilder();
    prompt = "no corpus>";
    console.setPrompt(prompt + " ");
    while ((line = console.readLine()) != null) {
        if (line.endsWith("\\")) {
            // multi-line input
            input.append(line.substring(0, line.length() - 1)).append("\n");
            // notifiy user by changing the prompt
            console.setPrompt("> ");
        } else {
            // input finished, run command
            input.append(line);
            ArrayList<String> splitted = Lists.newArrayList(argSplitter.split(input.toString()));
            String command = splitted.get(0);
            String args = "";
            if ("help".equalsIgnoreCase(command)) {
                System.out.println("Available commands:");
                System.out.println(StringUtils.join(commands, "\n"));
            } else {
                if (splitted.size() > 1) {
                    args = splitted.get(1);
                }
            }
            try {
                if (!command.isEmpty()) {
                    runCommand(command, args);
                }
            } catch (UsageException e) {
                error(e);
            }
            // reset the current prompt
            console.setPrompt(prompt + " ");
            // empty input
            input = new StringBuilder();
        }
    }
// end while
}
Also used : Splitter(com.google.common.base.Splitter) ConsoleReader(jline.console.ConsoleReader) StringsCompleter(jline.console.completer.StringsCompleter) File(java.io.File) FileHistory(jline.console.history.FileHistory)

Example 3 with StringsCompleter

use of jline.console.completer.StringsCompleter in project hive by apache.

the class CliDriver method getCommandCompleter.

public static Completer[] getCommandCompleter() {
    // StringsCompleter matches against a pre-defined wordlist
    // We start with an empty wordlist and build it up
    List<String> candidateStrings = new ArrayList<String>();
    // parenthesis at the end.
    for (String s : FunctionRegistry.getFunctionNames()) {
        if (s.matches("[a-z_]+")) {
            candidateStrings.add(s + "(");
        } else {
            candidateStrings.add(s);
        }
    }
    // We add Hive keywords, including lower-cased versions
    for (String s : HiveParser.getKeywords()) {
        candidateStrings.add(s);
        candidateStrings.add(s.toLowerCase());
    }
    StringsCompleter strCompleter = new StringsCompleter(candidateStrings);
    // Because we use parentheses in addition to whitespace
    // as a keyword delimiter, we need to define a new ArgumentDelimiter
    // that recognizes parenthesis as a delimiter.
    ArgumentDelimiter delim = new AbstractArgumentDelimiter() {

        @Override
        public boolean isDelimiterChar(CharSequence buffer, int pos) {
            char c = buffer.charAt(pos);
            return (Character.isWhitespace(c) || c == '(' || c == ')' || c == '[' || c == ']');
        }
    };
    // The ArgumentCompletor allows us to match multiple tokens
    // in the same line.
    final ArgumentCompleter argCompleter = new ArgumentCompleter(delim, strCompleter);
    // By default ArgumentCompletor is in "strict" mode meaning
    // a token is only auto-completed if all prior tokens
    // match. We don't want that since there are valid tokens
    // that are not in our wordlist (eg. table and column names)
    argCompleter.setStrict(false);
    // ArgumentCompletor always adds a space after a matched token.
    // This is undesirable for function names because a space after
    // the opening parenthesis is unnecessary (and uncommon) in Hive.
    // We stack a custom Completor on top of our ArgumentCompletor
    // to reverse this.
    Completer customCompletor = new Completer() {

        @Override
        public int complete(String buffer, int offset, List completions) {
            List<String> comp = completions;
            int ret = argCompleter.complete(buffer, offset, completions);
            // is exactly one valid completion, so we ignore other cases.
            if (completions.size() == 1) {
                if (comp.get(0).endsWith("( ")) {
                    comp.set(0, comp.get(0).trim());
                }
            }
            return ret;
        }
    };
    List<String> vars = new ArrayList<String>();
    for (HiveConf.ConfVars conf : HiveConf.ConfVars.values()) {
        vars.add(conf.varname);
    }
    StringsCompleter confCompleter = new StringsCompleter(vars) {

        @Override
        public int complete(final String buffer, final int cursor, final List<CharSequence> clist) {
            int result = super.complete(buffer, cursor, clist);
            if (clist.isEmpty() && cursor > 1 && buffer.charAt(cursor - 1) == '=') {
                HiveConf.ConfVars var = HiveConf.getConfVars(buffer.substring(0, cursor - 1));
                if (var == null) {
                    return result;
                }
                if (var.getValidator() instanceof Validator.StringSet) {
                    Validator.StringSet validator = (Validator.StringSet) var.getValidator();
                    clist.addAll(validator.getExpected());
                } else if (var.getValidator() != null) {
                    clist.addAll(Arrays.asList(var.getValidator().toDescription(), ""));
                } else {
                    clist.addAll(Arrays.asList("Expects " + var.typeString() + " type value", ""));
                }
                return cursor;
            }
            if (clist.size() > DELIMITED_CANDIDATE_THRESHOLD) {
                Set<CharSequence> delimited = new LinkedHashSet<CharSequence>();
                for (CharSequence candidate : clist) {
                    Iterator<String> it = Splitter.on(".").split(candidate.subSequence(cursor, candidate.length())).iterator();
                    if (it.hasNext()) {
                        String next = it.next();
                        if (next.isEmpty()) {
                            next = ".";
                        }
                        candidate = buffer != null ? buffer.substring(0, cursor) + next : next;
                    }
                    delimited.add(candidate);
                }
                clist.clear();
                clist.addAll(delimited);
            }
            return result;
        }
    };
    StringsCompleter setCompleter = new StringsCompleter("set") {

        @Override
        public int complete(String buffer, int cursor, List<CharSequence> clist) {
            return buffer != null && buffer.equals("set") ? super.complete(buffer, cursor, clist) : -1;
        }
    };
    ArgumentCompleter propCompleter = new ArgumentCompleter(setCompleter, confCompleter) {

        @Override
        public int complete(String buffer, int offset, List<CharSequence> completions) {
            int ret = super.complete(buffer, offset, completions);
            if (completions.size() == 1) {
                completions.set(0, ((String) completions.get(0)).trim());
            }
            return ret;
        }
    };
    return new Completer[] { propCompleter, customCompletor };
}
Also used : AbstractArgumentDelimiter(jline.console.completer.ArgumentCompleter.AbstractArgumentDelimiter) ArgumentDelimiter(jline.console.completer.ArgumentCompleter.ArgumentDelimiter) LinkedHashSet(java.util.LinkedHashSet) ArgumentCompleter(jline.console.completer.ArgumentCompleter) ArrayList(java.util.ArrayList) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter) ConfVars(org.apache.hadoop.hive.conf.HiveConf.ConfVars) StringsCompleter(jline.console.completer.StringsCompleter) List(java.util.List) ArrayList(java.util.ArrayList) HiveConf(org.apache.hadoop.hive.conf.HiveConf) AbstractArgumentDelimiter(jline.console.completer.ArgumentCompleter.AbstractArgumentDelimiter) Validator(org.apache.hadoop.hive.conf.Validator)

Example 4 with StringsCompleter

use of jline.console.completer.StringsCompleter in project hive by apache.

the class BeeLineCommandCompleter method getCompleters.

public static List<Completer> getCompleters(Iterable<CommandHandler> handlers) {
    List<Completer> completers = new LinkedList<Completer>();
    for (CommandHandler handler : handlers) {
        String[] commandNames = handler.getNames();
        if (commandNames != null) {
            for (String commandName : commandNames) {
                List<Completer> compl = new LinkedList<Completer>();
                compl.add(new StringsCompleter(BeeLine.COMMAND_PREFIX + commandName));
                compl.addAll(Arrays.asList(handler.getParameterCompleters()));
                // last param no complete
                compl.add(new NullCompleter());
                completers.add(new AggregateCompleter(compl.toArray(new Completer[compl.size()])));
            }
        }
    }
    return completers;
}
Also used : NullCompleter(jline.console.completer.NullCompleter) StringsCompleter(jline.console.completer.StringsCompleter) AggregateCompleter(jline.console.completer.AggregateCompleter) Completer(jline.console.completer.Completer) AggregateCompleter(jline.console.completer.AggregateCompleter) StringsCompleter(jline.console.completer.StringsCompleter) NullCompleter(jline.console.completer.NullCompleter) LinkedList(java.util.LinkedList)

Example 5 with StringsCompleter

use of jline.console.completer.StringsCompleter in project neo4j by neo4j.

the class ShellTabCompleter method getAppNameCompleter.

private Completer getAppNameCompleter() throws RemoteException {
    if (timeWhenCached != client.timeForMostRecentConnection()) {
        timeWhenCached = client.timeForMostRecentConnection();
        appNameCompleter = new StringsCompleter(client.getServer().getAllAvailableCommands());
    }
    return this.appNameCompleter;
}
Also used : StringsCompleter(jline.console.completer.StringsCompleter)

Aggregations

StringsCompleter (jline.console.completer.StringsCompleter)10 ConsoleReader (jline.console.ConsoleReader)4 Completer (jline.console.completer.Completer)4 ArrayList (java.util.ArrayList)3 AggregateCompleter (jline.console.completer.AggregateCompleter)3 ArgumentCompleter (jline.console.completer.ArgumentCompleter)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 FileNameCompleter (jline.console.completer.FileNameCompleter)2 JCommander (com.beust.jcommander.JCommander)1 ParameterDescription (com.beust.jcommander.ParameterDescription)1 Splitter (com.google.common.base.Splitter)1 CommandHelp (commandHelp.CommandHelp)1 CommandManager (de.jackwhite20.japs.server.command.CommandManager)1 SubCommand (de.jackwhite20.japs.server.command.impl.sub.SubCommand)1 JaPSLogger (de.jackwhite20.japs.server.logging.JaPSLogger)1 LoggingOutputStream (de.jackwhite20.japs.server.logging.out.LoggingOutputStream)1 InvalidCommandLineException (exceptions.InvalidCommandLineException)1