Search in sources :

Example 6 with Candidate

use of org.apache.karaf.shell.api.console.Candidate in project karaf by apache.

the class ArgumentCompleter method verifyCompleter.

protected boolean verifyCompleter(Session session, Completer completer, String argument) {
    List<Candidate> candidates = new ArrayList<>();
    completer.completeCandidates(session, new ArgumentCommandLine(argument, argument.length()), candidates);
    return !candidates.isEmpty();
}
Also used : Candidate(org.apache.karaf.shell.api.console.Candidate) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) ArrayList(java.util.ArrayList)

Example 7 with Candidate

use of org.apache.karaf.shell.api.console.Candidate in project karaf by apache.

the class FileCompleter method completeCandidates.

public void completeCandidates(final Session session, CommandLine commandLine, final List<Candidate> candidates) {
    // buffer can be null
    if (candidates == null) {
        return;
    }
    String buffer = commandLine.getCursorArgument().substring(0, commandLine.getArgumentPosition());
    if (OS_IS_WINDOWS) {
        buffer = buffer.replaceAll("/", File.separator);
    }
    Terminal terminal = (Terminal) session.get(".jline.terminal");
    Path current;
    String curBuf;
    int lastSep = buffer.lastIndexOf(separator());
    if (lastSep >= 0) {
        curBuf = buffer.substring(0, lastSep + 1);
        if (curBuf.startsWith("~")) {
            if (curBuf.startsWith("~" + separator())) {
                current = getUserHome().resolve(curBuf.substring(2));
            } else {
                current = getUserHome().getParent().resolve(curBuf.substring(1));
            }
        } else {
            current = getUserDir().resolve(curBuf);
        }
    } else {
        curBuf = "";
        current = getUserDir();
    }
    try {
        Files.newDirectoryStream(current, this::accept).forEach(p -> {
            String value = curBuf + p.getFileName().toString();
            if (Files.isDirectory(p)) {
                String s = OS_IS_WINDOWS ? "\\\\" : "/";
                candidates.add(new Candidate(value + s, getDisplay(terminal, p), null, null, s, null, false));
            } else {
                candidates.add(new Candidate(value, getDisplay(terminal, p), null, null, null, null, true));
            }
        });
    } catch (IOException e) {
    // Ignore
    }
}
Also used : Path(java.nio.file.Path) Candidate(org.apache.karaf.shell.api.console.Candidate) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal)

Example 8 with Candidate

use of org.apache.karaf.shell.api.console.Candidate in project karaf by apache.

the class UriCompleter method file.

private void file(Session session, CommandLine commandLine, List<Candidate> candidates) {
    String buffer = commandLine.getCursorArgument();
    String path = buffer.substring("file:".length(), commandLine.getArgumentPosition());
    String rem = "";
    try {
        Path dir;
        if (path.length() == 0) {
            for (Path root : FileSystems.getDefault().getRootDirectories()) {
                candidates.add(new Candidate(root.toString(), false));
            }
            dir = Paths.get(".");
        } else {
            dir = Paths.get(decode(path));
            if (!path.endsWith("/")) {
                rem = dir.getFileName().toString();
                dir = dir.getParent();
                if (dir == null) {
                    dir = Paths.get(".");
                }
            }
        }
        if (Files.isDirectory(dir)) {
            try (DirectoryStream<Path> paths = Files.newDirectoryStream(dir, rem + "*")) {
                for (Path child : paths) {
                    String name = encode(child.getFileName().toString());
                    boolean isDir = Files.isDirectory(child);
                    if (isDir) {
                        name += "/";
                    }
                    String dirstr = dir.endsWith("/") ? dir.toString() : dir.toString() + "/";
                    candidates.add(new Candidate("file:" + dirstr + name, !isDir));
                }
            }
        }
    } catch (Exception e) {
    // Ignore
    }
}
Also used : Path(java.nio.file.Path) Candidate(org.apache.karaf.shell.api.console.Candidate)

Example 9 with Candidate

use of org.apache.karaf.shell.api.console.Candidate in project karaf by apache.

the class CommandsCompleter method complete.

@Override
public void complete(LineReader reader, ParsedLine line, List<org.jline.reader.Candidate> candidates) {
    CommandLine commandLine = new CommandLineImpl(line);
    List<Candidate> cands = new ArrayList<>();
    completeCandidates(session, commandLine, cands);
    for (Candidate cand : cands) {
        candidates.add(new org.jline.reader.Candidate(cand.value(), cand.displ(), cand.group(), cand.descr(), cand.suffix(), cand.key(), cand.complete()));
    }
}
Also used : Candidate(org.apache.karaf.shell.api.console.Candidate) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ArrayList(java.util.ArrayList)

Aggregations

Candidate (org.apache.karaf.shell.api.console.Candidate)9 ArrayList (java.util.ArrayList)6 Path (java.nio.file.Path)3 ArgumentCommandLine (org.apache.karaf.shell.support.completers.ArgumentCommandLine)2 IOException (java.io.IOException)1 DirectoryStream (java.nio.file.DirectoryStream)1 CommandLine (org.apache.karaf.shell.api.console.CommandLine)1 Terminal (org.jline.terminal.Terminal)1