Search in sources :

Example 1 with Completer

use of jline.console.completer.Completer in project fmv by f-agu.

the class DefaultCommandBuilder method add.

/**
 * @see org.fagu.fmv.cli.CommandBuilder#add(java.lang.Class)
 */
@Override
public void add(final Class<? extends Command> cmdClass) {
    final org.fagu.fmv.cli.annotation.Command cmd = cmdClass.getAnnotation(org.fagu.fmv.cli.annotation.Command.class);
    if (cmd == null) {
        throw new RuntimeException("Annotation @Command not found: " + cmdClass.getName());
    }
    add(new CommandFactory() {

        /**
         * @see org.fagu.fmv.cli.CommandFactory#getCommandName()
         */
        @Override
        public String getCommandName() {
            return cmd.value();
        }

        /**
         * @see org.fagu.fmv.cli.CommandFactory#create(jline.console.ConsoleReader, org.fagu.fmv.cli.CommandBuilder,
         *      org.fagu.fmv.core.project.Project, java.lang.String, String[])
         */
        @Override
        public Command create(ConsoleReader consoleReader, CommandBuilder commandBuilder, Project project, String executable, String[] args) throws LineParseException {
            return build(cmdClass);
        }
    });
    // alias
    List<Alias> aliases = new ArrayList<>();
    Aliases aliasesAnno = cmdClass.getAnnotation(Aliases.class);
    if (aliasesAnno != null) {
        aliases.addAll(Arrays.asList(aliasesAnno.value()));
    }
    Alias aliasAnno = cmdClass.getAnnotation(Alias.class);
    if (aliasAnno != null) {
        aliases.add(aliasAnno);
    }
    if (!aliases.isEmpty()) {
        for (Alias alias : aliases) {
            String aliasCommand = alias.command();
            if (StringUtils.isBlank(aliasCommand)) {
                aliasCommand = cmd.value();
            }
            for (String aliasName : alias.value()) {
                addAlias(aliasName, aliasCommand);
            }
        }
    }
    // completion
    Completion completion = cmdClass.getAnnotation(Completion.class);
    if (completion != null) {
        try {
            @SuppressWarnings("unchecked") Class<CompleterFactory> completerFactoryClass = (Class<CompleterFactory>) Class.forName(completion.value());
            CompleterFactory completerFactory = completerFactoryClass.newInstance();
            Completer completer = completerFactory.create(cmd.value());
            completerMap.add(cmd.value(), completer);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) LineParseException(org.fagu.fmv.cli.exception.LineParseException) ConsoleReader(jline.console.ConsoleReader) Completer(jline.console.completer.Completer) Aliases(org.fagu.fmv.cli.annotation.Aliases) AliasCustomCommandFactory(org.fagu.fmv.cli.command.AliasCustomCommandFactory) Project(org.fagu.fmv.core.project.Project) Completion(org.fagu.fmv.cli.annotation.Completion) Alias(org.fagu.fmv.cli.annotation.Alias) CompleterFactory(org.fagu.fmv.cli.completion.CompleterFactory)

Example 2 with Completer

use of jline.console.completer.Completer in project fmv by f-agu.

the class ArgumentCompleter method complete.

@Override
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
    // System.out.println();
    // System.out.println("complete(" + buffer + ", " + cursor + ", " + candidates + ")");
    // buffer can be null
    checkNotNull(candidates);
    ArgumentDelimiter delim = getDelimiter();
    ArgumentList list = delim.delimit(buffer, cursor);
    int argpos = list.getArgumentPosition();
    int argIndex = list.getCursorArgumentIndex();
    if (argIndex < 0) {
        return -1;
    }
    List<Completer> completers = getCompleters();
    Completer completer;
    // System.out.println("completers: " + completers);
    if (argIndex >= completers.size()) {
        completer = completers.get(completers.size() - 1);
    } else {
        completer = completers.get(argIndex);
    }
    // strict).
    for (int i = 0; isStrict() && (i < argIndex); i++) {
        Completer sub = completers.get(i >= completers.size() ? (completers.size() - 1) : i);
        // System.out.println("sub: " + sub);
        String[] args = list.getArguments();
        String arg = (args == null || i >= args.length) ? "" : args[i];
        // System.out.println("args: " + StringUtils.toString(args, ", "));
        List<CharSequence> subCandidates = new LinkedList<CharSequence>();
        if (sub.complete(arg, arg.length(), subCandidates) == -1) {
            // System.out.println("out 1");
            return -1;
        }
        if (subCandidates.size() == 0) {
            // System.out.println("out 2");
            return -1;
        }
    }
    int ret = completer.complete(list.getCursorArgument(), argpos, candidates);
    if (ret == -1) {
        return -1;
    }
    int pos = ret + list.getBufferPosition() - argpos;
    if ((cursor != buffer.length()) && delim.isDelimiter(buffer, cursor)) {
        for (int i = 0; i < candidates.size(); i++) {
            CharSequence val = candidates.get(i);
            while (val.length() > 0 && delim.isDelimiter(val, val.length() - 1)) {
                val = val.subSequence(0, val.length() - 1);
            }
            candidates.set(i, val);
        }
    }
    Log.trace("Completing ", buffer, " (pos=", cursor, ") with: ", candidates, ": offset=", pos);
    return pos;
}
Also used : Completer(jline.console.completer.Completer) LinkedList(java.util.LinkedList)

Example 3 with Completer

use of jline.console.completer.Completer in project jdepth by Crab2died.

the class JLineSample method main.

public static void main(String... args) throws IOException {
    // IDEA、Eclipse调试时使用
    // System.setProperty("jline.WindowsTerminal.directConsole", "false");
    // System.setProperty("jline.internal.Log.debug", "true");
    ConsoleReader reader = new ConsoleReader();
    reader.addCompleter(new Completer() {

        @Override
        public int complete(String buffer, int cursor, List<CharSequence> candidates) {
            if (buffer.length() > 0 && cursor > 0) {
                String prefix = buffer.substring(cursor);
                if (prefix.endsWith("m")) {
                    candidates.add("mdir");
                    candidates.add("mfile");
                    candidates.add("make");
                }
            }
            return cursor - 2;
        }
    });
    String cmd;
    do {
        cmd = reader.readLine("JLine >");
        System.out.println(cmd);
    } while (cmd != null && !"exit".equals(cmd));
    reader.close();
}
Also used : ConsoleReader(jline.console.ConsoleReader) Completer(jline.console.completer.Completer)

Example 4 with Completer

use of jline.console.completer.Completer in project apex-core by apache.

the class ApexCli method updateCompleter.

private void updateCompleter(ConsoleReader reader) {
    List<Completer> completers = new ArrayList<>(reader.getCompleters());
    for (Completer c : completers) {
        reader.removeCompleter(c);
    }
    setupCompleter(reader);
}
Also used : ArrayList(java.util.ArrayList) FileNameCompleter(jline.console.completer.FileNameCompleter) AggregateCompleter(jline.console.completer.AggregateCompleter) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter)

Example 5 with Completer

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

the class CliDriver method setupConsoleReader.

protected void setupConsoleReader() throws IOException {
    reader = new ConsoleReader();
    reader.setExpandEvents(false);
    reader.setBellEnabled(false);
    for (Completer completer : getCommandCompleter()) {
        reader.addCompleter(completer);
    }
    setupCmdHistory();
}
Also used : ConsoleReader(jline.console.ConsoleReader) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter)

Aggregations

Completer (jline.console.completer.Completer)14 StringsCompleter (jline.console.completer.StringsCompleter)8 ArgumentCompleter (jline.console.completer.ArgumentCompleter)7 AggregateCompleter (jline.console.completer.AggregateCompleter)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 ConsoleReader (jline.console.ConsoleReader)4 FileNameCompleter (jline.console.completer.FileNameCompleter)4 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 AliasCustomCommandFactory (org.fagu.fmv.cli.command.AliasCustomCommandFactory)2 JCommander (com.beust.jcommander.JCommander)1 ParameterDescription (com.beust.jcommander.ParameterDescription)1 AppInfo (com.datatorrent.stram.client.AppPackage.AppInfo)1 DTConfiguration (com.datatorrent.stram.client.DTConfiguration)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1