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