Search in sources :

Example 6 with Completer

use of org.apache.karaf.shell.console.Completer in project karaf by apache.

the class ArgumentCompleter method getDefaultCompleter.

private Completer getDefaultCompleter(Field field) {
    Completer completer = null;
    Class<?> type = field.getType();
    if (type.isAssignableFrom(File.class)) {
        completer = new FileCompleter(null);
    } else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
        completer = new StringsCompleter(new String[] { "false", "true" }, false);
    } else if (type.isAssignableFrom(Enum.class)) {
        Set<String> values = new HashSet<>();
        for (Object o : EnumSet.allOf((Class<Enum>) type)) {
            values.add(o.toString());
        }
        completer = new StringsCompleter(values, false);
    } else {
    // TODO any other completers we can add?
    }
    return completer;
}
Also used : FileCompleter(org.apache.karaf.shell.console.completer.FileCompleter) StringsCompleter(org.apache.karaf.shell.console.completer.StringsCompleter) FileCompleter(org.apache.karaf.shell.console.completer.FileCompleter) Completer(org.apache.karaf.shell.console.Completer) StringsCompleter(org.apache.karaf.shell.console.completer.StringsCompleter) NullCompleter(org.apache.karaf.shell.console.completer.NullCompleter) HashSet(java.util.HashSet)

Example 7 with Completer

use of org.apache.karaf.shell.console.Completer in project karaf by apache.

the class CommandsCompleter method checkData.

@SuppressWarnings({ "unchecked", "deprecation" })
protected Map<String, Completer>[] checkData() {
    // Copy the set to avoid concurrent modification exceptions
    // TODO: fix that in gogo instead
    Set<String> names;
    boolean update;
    synchronized (this) {
        names = new HashSet<>((Set<String>) session.get(COMMANDS));
        update = !names.equals(commands);
    }
    if (update) {
        // get command aliases
        Set<String> commands = new HashSet<>();
        Map<String, Completer> global = new HashMap<>();
        Map<String, Completer> local = new HashMap<>();
        // add argument completers for each command
        for (String command : names) {
            String rawCommand = stripScope(command);
            Function function = (Function) session.get(command);
            function = unProxy(function);
            if (function instanceof CommandWithAction) {
                try {
                    global.put(command, new ArgumentCompleter(session, (CommandWithAction) function, command));
                    local.put(command, new ArgumentCompleter(session, (CommandWithAction) function, rawCommand));
                } catch (Throwable t) {
                    LOGGER.debug("Unable to create completers for command '" + command + "'", t);
                }
            } else if (function instanceof org.apache.felix.gogo.commands.CommandWithAction) {
                try {
                    global.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, command));
                    local.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, rawCommand));
                } catch (Throwable t) {
                    LOGGER.debug("Unable to create completers for command '" + command + "'", t);
                }
            }
            commands.add(command);
        }
        synchronized (this) {
            this.commands.clear();
            this.globalCompleters.clear();
            this.localCompleters.clear();
            this.commands.addAll(commands);
            this.globalCompleters.putAll(global);
            this.localCompleters.putAll(local);
        }
    }
    synchronized (this) {
        return new Map[] { new HashMap<>(this.globalCompleters), new HashMap<>(this.localCompleters) };
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Completer(org.apache.karaf.shell.console.Completer) CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Function(org.apache.felix.service.command.Function) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with Completer

use of org.apache.karaf.shell.console.Completer in project karaf by apache.

the class AggregateCompleter method complete.

@SuppressWarnings({ "unchecked", "rawtypes" })
public int complete(final String buffer, final int cursor, final List candidates) {
    // buffer could be null
    assert candidates != null;
    List<Completion> completions = new ArrayList<>(completers.size());
    // Run each completer, saving its completion results
    int max = -1;
    for (Completer completer : completers) {
        Completion completion = new Completion(candidates);
        completion.complete(completer, buffer, cursor);
        // Compute the max cursor position
        max = Math.max(max, completion.cursor);
        completions.add(completion);
    }
    // Append candiates from completions which have the same cursor position as max
    for (Completion completion : completions) {
        if (completion.cursor == max) {
            // noinspection unchecked
            candidates.addAll(completion.candidates);
        }
    }
    return max;
}
Also used : ArrayList(java.util.ArrayList) Completer(org.apache.karaf.shell.console.Completer)

Aggregations

Completer (org.apache.karaf.shell.console.Completer)8 Field (java.lang.reflect.Field)4 CommandSession (org.apache.felix.service.command.CommandSession)4 HashSet (java.util.HashSet)3 FileCompleter (org.apache.karaf.shell.console.completer.FileCompleter)3 NullCompleter (org.apache.karaf.shell.console.completer.NullCompleter)3 StringsCompleter (org.apache.karaf.shell.console.completer.StringsCompleter)3 Option (org.apache.felix.gogo.commands.Option)2 Function (org.apache.felix.service.command.Function)2 HelpOption (org.apache.karaf.shell.commands.HelpOption)2 Option (org.apache.karaf.shell.commands.Option)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 CommandWithAction (org.apache.karaf.shell.commands.CommandWithAction)1 CompletableFunction (org.apache.karaf.shell.console.CompletableFunction)1