jline.console.completer.Completer Java Examples

The following examples show how to use jline.console.completer.Completer. 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: LineReader.java    From sylph with Apache License 2.0 5 votes vote down vote up
LineReader(History history, Completer... completers)
        throws IOException
{
    setExpandEvents(false);
    setBellEnabled(true);
    setHandleUserInterrupt(true);
    setHistory(history);
    setHistoryEnabled(false);
    for (Completer completer : completers) {
        addCompleter(completer);
    }
}
 
Example #3
Source File: ApexCli.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void updateCompleter(ConsoleReader reader)
{
  List<Completer> completers = new ArrayList<>(reader.getCompleters());
  for (Completer c : completers) {
    reader.removeCompleter(c);
  }
  setupCompleter(reader);
}
 
Example #4
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 #5
Source File: Terminal.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<Completer> selectCompleters() {
    final List<Completer> selection = new ArrayList<Completer>();
    for (Command eachCommand: configuration.getCommands()) {
        selection.add(new StringsCompleter(eachCommand.getSyntax()));
    }
    selection.add(new FileNameCompleter());
    return selection;
}
 
Example #6
Source File: InputValueBuilder.java    From consoleui with Apache License 2.0 5 votes vote down vote up
public InputValueBuilder addCompleter(Completer completer) {
  if (completers == null) {
    completers = new ArrayList<Completer>();
  }
  this.completers.add(completer);
  return this;
}
 
Example #7
Source File: ApexCli.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
private void updateCompleter(ConsoleReader reader)
{
  List<Completer> completers = new ArrayList<>(reader.getCompleters());
  for (Completer c : completers) {
    reader.removeCompleter(c);
  }
  setupCompleter(reader);
}
 
Example #8
Source File: CmdUtilTest.java    From clamshell-cli with Apache License 2.0 5 votes vote down vote up
public void testGetHintsAsCompleters() {
    List<Command> cmds = new ArrayList<Command>();
    cmds.add(new MockCommand());
    Map<String, List<String>> hints = CmdUtil.extractCommandHints(cmds);
    List<Completer> completers = CmdUtil.getHintsAsCompleters(hints);
    
    Assert.assertNotNull(completers);
    Assert.assertTrue(completers.size() == 1);
    
    StringsCompleter strComp = (StringsCompleter) completers.get(0);
    Assert.assertTrue(strComp.getStrings().size() == 8); // cmd + args
}
 
Example #9
Source File: CmdUtil.java    From clamshell-cli with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a list of jLine completers using the hints map from {@link extractCommandHints}
 * @param hints
 * @return 
 */
public static List<Completer> getHintsAsCompleters(Map<String,List<String>> hints) {
    List<Completer> completors = new ArrayList<Completer>(hints.size());
    for (Map.Entry<String, List<String>> hint : hints.entrySet()){
        List<String> argList = hint.getValue();
        argList.add(0, hint.getKey());
        completors.add(new StringsCompleter(argList));
    }
    return completors;
}
 
Example #10
Source File: TajoCli.java    From tajo with Apache License 2.0 5 votes vote down vote up
private void initCommands() {
  List<Completer> compList = new ArrayList<>();
  for (Class clazz : registeredCommands) {
    TajoShellCommand cmd = null;

    try {
       Constructor cons = clazz.getConstructor(new Class[] {TajoCliContext.class});
       cmd = (TajoShellCommand) cons.newInstance(context);
    } catch (Exception e) {
      System.err.println(e.getMessage());
      throw new RuntimeException(e.getMessage());
    }

    // make completers for console auto-completion
    compList.add(cmd.getArgumentCompleter());

    commands.put(cmd.getCommand(), cmd);
    for (String alias : cmd.getAliases()) {
      commands.put(alias, cmd);
    }
  }

  cliCompleter = new AggregateCompleter(compList);

  sqlCompleter = new ArgumentCompleter(
      new ArgumentCompleter.AbstractArgumentDelimiter() {
        @Override
        public boolean isDelimiterChar(CharSequence buf, int pos) {
          char c = buf.charAt(pos);
          return Character.isWhitespace(c) || !(Character.isLetterOrDigit(c)) && c != '_';
        }
      },
      new StringsCompleter(getKeywords())
  );
}
 
Example #11
Source File: JLineModule.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
    bind(new TypeLiteral<Console<PrintStream>>(){}).to(JLineConsole.class).asEagerSingleton();
    bind(Completer.class).to(JLineRhinoCompleter.class).asEagerSingleton();
    bind(CompletionHandler.class).to(JLineCompletionHandler.class).asEagerSingleton();
}
 
Example #12
Source File: CommandCompleter.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {

    if (buffer.contains(CliConstants.RESOURCE_PATH_LONG_OPTION)) {
        return fileNameCompleter.complete(buffer, cursor, candidates);
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Buffer: {}, cursor: {}", buffer, cursor);
        logger.trace("Candidates {}", candidates);
    }
    if (StringUtils.isNotBlank(buffer)) {
        // User is typing a command
        StrTokenizer strTokenizer = new StrTokenizer(buffer);
        String action = strTokenizer.next();
        Collection<String> arguments = argumentMap.get(action);
        if (arguments != null) {
            if (logger.isTraceEnabled()) {
                logger.trace("Arguments found for {}, Tokens: {}", action, strTokenizer.getTokenList());
                logger.trace("Arguments for {}: {}", action, arguments);
            }
            List<String> args = new ArrayList<String>(arguments);
            List<Completer> completers = new ArrayList<Completer>();
            for (String token : strTokenizer.getTokenList()) {
                boolean argContains = arguments.contains(token);
                if (token.startsWith("-") && !argContains) {
                    continue;
                }
                if (argContains) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Removing argument {}", token);
                    }
                    args.remove(token);
                }
                completers.add(new StringsCompleter(token));
            }
            completers.add(new StringsCompleter(args));
            Completer completer = new ArgumentCompleter(completers);
            return completer.complete(buffer, cursor, candidates);
        } else if (CliConstants.HELP_ACTION.equals(action)) {
            // For help action, we need to display available commands as arguments
            return helpCommandCompleter.complete(buffer, cursor, candidates);
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Using Default Completer...");
    }
    return defaultCommandCompleter.complete(buffer, cursor, candidates);
}
 
Example #13
Source File: CachedCompleter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public Completer getCompleter() {
  return completer;
}
 
Example #14
Source File: CachedCompleter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public CachedCompleter(Completer completer, int ttlInSeconds) {
  this.completer = completer;
  this.ttlInSeconds = ttlInSeconds;
  this.createdAt = System.currentTimeMillis();
}
 
Example #15
Source File: CheckboxPromptTest.java    From consoleui with Apache License 2.0 4 votes vote down vote up
@Test
public void renderSimpleList() throws IOException {
  CheckboxPrompt checkboxPrompt = new CheckboxPrompt();
  List<CheckboxItemIF> list=new ArrayList<CheckboxItemIF>();

  list.add(new CheckboxItem("One"));
  list.add(new CheckboxItem(true,"Two"));
  CheckboxItem three = new CheckboxItem("Three");
  three.setDisabled("not available");
  list.add(three);
  list.add(new Separator("some extra items"));
  list.add(new CheckboxItem("Four"));
  list.add(new CheckboxItem(true,"Five"));

  checkboxPrompt.setReader(new ReaderIF() {
    public void setAllowedSpecialKeys(Set<SpecialKey> allowedSpecialKeys) {

    }

    public void setAllowedPrintableKeys(Set<Character> allowedPrintableKeys) {

    }

    public void addAllowedPrintableKey(Character character) {

    }

    public void addAllowedSpecialKey(SpecialKey specialKey) {

    }

    public ReaderInput read() {
      return new ReaderInput(SpecialKey.ENTER);
    }

    public ReaderInput readLine(List<Completer> completer, String promt, String value, Character mask) throws IOException {
      return null;
    }
  });

  checkboxPrompt.prompt(new Checkbox("no message", null, list));
}
 
Example #16
Source File: InputValue.java    From consoleui with Apache License 2.0 4 votes vote down vote up
public void addCompleter(Completer completer) {
  if (this.completer==null) {
    this.completer=new ArrayList<Completer>();
  }
  this.completer.add(completer);
}
 
Example #17
Source File: InputValue.java    From consoleui with Apache License 2.0 4 votes vote down vote up
public void setCompleter(List<Completer> completer) {
  this.completer = completer;
}
 
Example #18
Source File: InputValue.java    From consoleui with Apache License 2.0 4 votes vote down vote up
public List<Completer> getCompleter() {
  return completer;
}
 
Example #19
Source File: ReaderIF.java    From consoleui with Apache License 2.0 votes vote down vote up
ReaderInput readLine(List<Completer> completer, String promt, String value, Character mask) throws IOException;