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();
}
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
}
}
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
}
}
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()));
}
}
Aggregations