Search in sources :

Example 1 with Candidate

use of org.jline.reader.Candidate in project felix by apache.

the class Builtin method __files.

public List<Candidate> __files(CommandSession session) {
    ParsedLine line = Shell.getParsedLine(session);
    LineReader reader = Shell.getReader(session);
    List<Candidate> candidates = new ArrayList<>();
    new FilesCompleter(session.currentDir()) {

        @Override
        protected String getDisplay(Terminal terminal, Path p) {
            return getFileDisplay(session, p);
        }
    }.complete(reader, line, candidates);
    return candidates;
}
Also used : Candidate(org.jline.reader.Candidate) Path(java.nio.file.Path) ParsedLine(org.jline.reader.ParsedLine) LineReader(org.jline.reader.LineReader) ArrayList(java.util.ArrayList) FilesCompleter(org.jline.builtins.Completers.FilesCompleter) Terminal(org.jline.terminal.Terminal)

Example 2 with Candidate

use of org.jline.reader.Candidate in project felix by apache.

the class Builtin method __directories.

public List<Candidate> __directories(CommandSession session) {
    ParsedLine line = Shell.getParsedLine(session);
    LineReader reader = Shell.getReader(session);
    List<Candidate> candidates = new ArrayList<>();
    new DirectoriesCompleter(session.currentDir()) {

        @Override
        protected String getDisplay(Terminal terminal, Path p) {
            return getFileDisplay(session, p);
        }
    }.complete(reader, line, candidates);
    return candidates;
}
Also used : Candidate(org.jline.reader.Candidate) Path(java.nio.file.Path) DirectoriesCompleter(org.jline.builtins.Completers.DirectoriesCompleter) ParsedLine(org.jline.reader.ParsedLine) LineReader(org.jline.reader.LineReader) ArrayList(java.util.ArrayList) Terminal(org.jline.terminal.Terminal)

Example 3 with Candidate

use of org.jline.reader.Candidate in project nifi by apache.

the class TestCLICompleter method testCompletionForSessionVariableWithFiles.

@Test
public void testCompletionForSessionVariableWithFiles() {
    final String topCommand = "session";
    final String subCommand = "set";
    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList("", Arrays.asList(topCommand, subCommand, SessionVariable.NIFI_CLIENT_PROPS.getVariableName(), "src/test/resources/"), 3, -1, -1);
    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);
    boolean found = false;
    for (Candidate candidate : candidates) {
        if (candidate.value().equals("src/test/resources/test.properties")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : Candidate(org.jline.reader.Candidate) ArrayList(java.util.ArrayList) DefaultParser(org.jline.reader.impl.DefaultParser) Test(org.junit.Test)

Example 4 with Candidate

use of org.jline.reader.Candidate in project LanternServer by LanternPowered.

the class ConsoleCommandCompleter method complete.

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
    String buffer = line.line();
    // The content with normalized spaces, the spaces are trimmed
    // from the ends and there are never two spaces directly after each other
    String command = StringUtils.normalizeSpace(buffer);
    boolean hasPrefix = command.startsWith("/");
    // Don't include the '/'
    if (hasPrefix) {
        command = command.substring(1);
    }
    // Keep the last space, it must be there!
    if (buffer.endsWith(" ")) {
        command = command + " ";
    }
    final String command0 = command;
    final Future<List<String>> tabComplete = ((LanternScheduler) Sponge.getScheduler()).callSync(() -> Sponge.getCommandManager().getSuggestions(LanternConsoleSource.INSTANCE, command0, null));
    try {
        // Get the suggestions
        final List<String> suggestions = tabComplete.get();
        // If the suggestions are for the command and there was a prefix, then append the prefix
        if (hasPrefix && command.split(" ").length == 1 && !command.endsWith(" ")) {
            for (String completion : suggestions) {
                if (!completion.isEmpty()) {
                    candidates.add(new Candidate('/' + completion));
                }
            }
        } else {
            for (String completion : suggestions) {
                if (!completion.isEmpty()) {
                    candidates.add(new Candidate(completion));
                }
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Lantern.getLogger().error("Failed to tab complete", e);
    }
}
Also used : Candidate(org.jline.reader.Candidate) LanternScheduler(org.lanternpowered.server.scheduler.LanternScheduler) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with Candidate

use of org.jline.reader.Candidate in project nifi by apache.

the class TestCLICompleter method testCompletionWithFileArguments.

@Test
public void testCompletionWithFileArguments() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;
    final String subCommand = "list-buckets";
    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList("", Arrays.asList(topCommand, subCommand, "-p", "src/test/resources/"), 3, -1, -1);
    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);
    boolean found = false;
    for (Candidate candidate : candidates) {
        if (candidate.value().equals("src/test/resources/test.properties")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : Candidate(org.jline.reader.Candidate) ArrayList(java.util.ArrayList) DefaultParser(org.jline.reader.impl.DefaultParser) Test(org.junit.Test)

Aggregations

Candidate (org.jline.reader.Candidate)8 ArrayList (java.util.ArrayList)4 Path (java.nio.file.Path)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 LineReader (org.jline.reader.LineReader)2 ParsedLine (org.jline.reader.ParsedLine)2 DefaultParser (org.jline.reader.impl.DefaultParser)2 Terminal (org.jline.terminal.Terminal)2 Test (org.junit.Test)2 DirectoriesCompleter (org.jline.builtins.Completers.DirectoriesCompleter)1 FilesCompleter (org.jline.builtins.Completers.FilesCompleter)1 LanternScheduler (org.lanternpowered.server.scheduler.LanternScheduler)1