use of jline.console.completer.Completer in project GeoGig by boundlessgeo.
the class GeogigConsole method addCommandCompleter.
private void addCommandCompleter(ConsoleReader consoleReader, final GeogigCLI cli) {
final JCommander globalCommandParser = cli.newCommandParser();
final Map<String, JCommander> commands = globalCommandParser.getCommands();
List<Completer> completers = new ArrayList<Completer>(commands.size());
for (Map.Entry<String, JCommander> entry : commands.entrySet()) {
String commandName = entry.getKey();
JCommander commandParser = entry.getValue();
List<ParameterDescription> parameters = commandParser.getParameters();
List<String> options = new ArrayList<String>(parameters.size());
for (ParameterDescription pd : parameters) {
String longestName = pd.getLongestName();
options.add(longestName);
}
Collections.sort(options);
ArgumentCompleter commandCompleter = new ArgumentCompleter(new StringsCompleter(commandName), new StringsCompleter(options));
completers.add(commandCompleter);
}
completers.add(new StringsCompleter("exit", "clear"));
Completer completer = new AggregateCompleter(completers);
consoleReader.addCompleter(completer);
}
use of jline.console.completer.Completer in project fmv by f-agu.
the class DefaultCommandBuilder method remove.
/**
* @see org.fagu.fmv.cli.CommandBuilder#remove(java.lang.String)
*/
@Override
public CommandFactory remove(String name) {
String lcName = name.toLowerCase();
CommandFactory removed = cmdBuilderMap.remove(lcName);
for (Completer completer : completerMap.get(lcName)) {
consoleReader.removeCompleter(completer);
}
return removed;
}
use of jline.console.completer.Completer in project apex-core by apache.
the class ApexCli method getLaunchAppPackageArgs.
String[] getLaunchAppPackageArgs(AppPackage ap, ConfigPackage cp, LaunchCommandLineInfo commandLineInfo, ConsoleReader reader) throws Exception {
String matchAppName = null;
if (commandLineInfo.args.length > 1) {
matchAppName = commandLineInfo.args[1];
}
List<AppInfo> applications = new ArrayList<>(getAppsFromPackageAndConfig(ap, cp, commandLineInfo.useConfigApps));
if (matchAppName != null) {
Iterator<AppInfo> it = applications.iterator();
while (it.hasNext()) {
AppInfo ai = it.next();
if ((commandLineInfo.exactMatch && !ai.name.equals(matchAppName)) || !ai.name.toLowerCase().matches(".*" + matchAppName.toLowerCase() + ".*")) {
it.remove();
}
}
}
AppInfo selectedApp = null;
if (applications.isEmpty()) {
throw new CliException("No applications in Application Package" + (matchAppName != null ? " matching \"" + matchAppName + "\"" : ""));
} else if (applications.size() == 1) {
selectedApp = applications.get(0);
} else {
// Store the appNames sorted in alphabetical order and their position in matchingAppFactories list
TreeMap<String, Integer> appNamesInAlphabeticalOrder = new TreeMap<>();
// Display matching applications
for (int i = 0; i < applications.size(); i++) {
String appName = applications.get(i).name;
appNamesInAlphabeticalOrder.put(appName, i);
}
// Create a mapping between the app display number and original index at matchingAppFactories
int index = 1;
HashMap<Integer, Integer> displayIndexToOriginalUnsortedIndexMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : appNamesInAlphabeticalOrder.entrySet()) {
// Map display number of the app to original unsorted index
displayIndexToOriginalUnsortedIndexMap.put(index, entry.getValue());
// Display the app names
System.out.printf("%3d. %s\n", index++, entry.getKey());
}
// Exit if not in interactive mode
if (!consolePresent) {
throw new CliException("More than one application in Application Package match '" + matchAppName + "'");
} else {
boolean useHistory = reader.isHistoryEnabled();
reader.setHistoryEnabled(false);
History previousHistory = reader.getHistory();
History dummyHistory = new MemoryHistory();
reader.setHistory(dummyHistory);
List<Completer> completers = new ArrayList<>(reader.getCompleters());
for (Completer c : completers) {
reader.removeCompleter(c);
}
reader.setHandleUserInterrupt(true);
String optionLine;
try {
optionLine = reader.readLine("Choose application: ");
} finally {
reader.setHandleUserInterrupt(false);
reader.setHistoryEnabled(useHistory);
reader.setHistory(previousHistory);
for (Completer c : completers) {
reader.addCompleter(c);
}
}
try {
int option = Integer.parseInt(optionLine);
if (0 < option && option <= applications.size()) {
int appIndex = displayIndexToOriginalUnsortedIndexMap.get(option);
selectedApp = applications.get(appIndex);
}
} catch (Exception ex) {
// ignore
}
}
}
if (selectedApp == null) {
throw new CliException("No application selected");
}
DTConfiguration launchProperties = getLaunchAppPackageProperties(ap, cp, commandLineInfo, selectedApp.name);
String appFile = ap.tempDirectory() + "/app/" + selectedApp.file;
List<String> launchArgs = new ArrayList<>();
launchArgs.add("launch");
launchArgs.add("-exactMatch");
List<String> absClassPath = new ArrayList<>(ap.getClassPath());
for (int i = 0; i < absClassPath.size(); i++) {
String path = absClassPath.get(i);
if (!path.startsWith("/")) {
absClassPath.set(i, ap.tempDirectory() + "/" + path);
}
}
if (cp != null) {
StringBuilder files = new StringBuilder();
for (String file : cp.getClassPath()) {
if (files.length() != 0) {
files.append(',');
}
files.append(cp.tempDirectory()).append(File.separatorChar).append(file);
}
if (!StringUtils.isBlank(files.toString())) {
if (commandLineInfo.libjars != null) {
commandLineInfo.libjars = files.toString() + "," + commandLineInfo.libjars;
} else {
commandLineInfo.libjars = files.toString();
}
}
files.setLength(0);
for (String file : cp.getFiles()) {
if (files.length() != 0) {
files.append(',');
}
files.append(cp.tempDirectory()).append(File.separatorChar).append(file);
}
if (!StringUtils.isBlank(files.toString())) {
if (commandLineInfo.files != null) {
commandLineInfo.files = files.toString() + "," + commandLineInfo.files;
} else {
commandLineInfo.files = files.toString();
}
}
}
StringBuilder libjarsVal = new StringBuilder();
if (!absClassPath.isEmpty() || commandLineInfo.libjars != null) {
if (!absClassPath.isEmpty()) {
libjarsVal.append(org.apache.commons.lang3.StringUtils.join(absClassPath, ','));
}
if (commandLineInfo.libjars != null) {
if (libjarsVal.length() > 0) {
libjarsVal.append(",");
}
libjarsVal.append(commandLineInfo.libjars);
}
}
if (appFile.endsWith(".json") || appFile.endsWith(".properties")) {
if (libjarsVal.length() > 0) {
libjarsVal.append(",");
}
libjarsVal.append(ap.tempDirectory()).append("/app/*.jar");
}
if (libjarsVal.length() > 0) {
launchArgs.add("-libjars");
launchArgs.add(libjarsVal.toString());
}
File launchPropertiesFile = new File(ap.tempDirectory(), "launch.xml");
launchProperties.writeToFile(launchPropertiesFile, "");
launchArgs.add("-conf");
launchArgs.add(launchPropertiesFile.getCanonicalPath());
if (commandLineInfo.localMode) {
launchArgs.add("-local");
}
if (commandLineInfo.archives != null) {
launchArgs.add("-archives");
launchArgs.add(commandLineInfo.archives);
}
if (commandLineInfo.files != null) {
launchArgs.add("-files");
launchArgs.add(commandLineInfo.files);
}
if (commandLineInfo.origAppId != null) {
launchArgs.add("-originalAppId");
launchArgs.add(commandLineInfo.origAppId);
}
if (commandLineInfo.queue != null) {
launchArgs.add("-queue");
launchArgs.add(commandLineInfo.queue);
}
if (commandLineInfo.tags != null) {
launchArgs.add("-tags");
launchArgs.add(commandLineInfo.tags);
}
launchArgs.add(appFile);
if (!appFile.endsWith(".json") && !appFile.endsWith(".properties")) {
launchArgs.add(selectedApp.name);
}
LOG.debug("Launch command: {}", StringUtils.join(launchArgs, " "));
return launchArgs.toArray(new String[] {});
}
use of jline.console.completer.Completer in project apex-core by apache.
the class ApexCli method defaultCompleters.
private List<Completer> defaultCompleters() {
Map<String, CommandSpec> commands = new TreeMap<>();
commands.putAll(logicalPlanChangeCommands);
commands.putAll(connectedCommands);
commands.putAll(globalCommands);
List<Completer> completers = new LinkedList<>();
for (Map.Entry<String, CommandSpec> entry : commands.entrySet()) {
String command = entry.getKey();
CommandSpec cs = entry.getValue();
List<Completer> argCompleters = new LinkedList<>();
argCompleters.add(new StringsCompleter(command));
Arg[] args = (Arg[]) ArrayUtils.addAll(cs.requiredArgs, cs.optionalArgs);
if (args != null) {
if (cs instanceof OptionsCommandSpec) {
// ugly hack because jline cannot dynamically change completer while user types
if (args[0] instanceof FileArg || args[0] instanceof VarArg) {
for (int i = 0; i < 10; i++) {
argCompleters.add(new MyFileNameCompleter());
}
}
} else {
for (Arg arg : args) {
if (arg instanceof FileArg || arg instanceof VarArg) {
argCompleters.add(new MyFileNameCompleter());
} else if (arg instanceof CommandArg) {
argCompleters.add(new StringsCompleter(commands.keySet().toArray(new String[] {})));
} else {
argCompleters.add(MyNullCompleter.INSTANCE);
}
}
}
}
completers.add(new ArgumentCompleter(argCompleters));
}
List<Completer> argCompleters = new LinkedList<>();
Set<String> set = new TreeSet<>();
set.addAll(aliases.keySet());
set.addAll(macros.keySet());
argCompleters.add(new StringsCompleter(set.toArray(new String[] {})));
for (int i = 0; i < 10; i++) {
argCompleters.add(new MyFileNameCompleter());
}
completers.add(new ArgumentCompleter(argCompleters));
return completers;
}
use of jline.console.completer.Completer in project grails-core by grails.
the class SortedAggregateCompleter method complete.
/**
* Perform a completion operation across all aggregated completers.
*
* @see Completer#complete(String, int, java.util.List)
* @return the highest completion return value from all completers
*/
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
// buffer could be null
checkNotNull(candidates);
List<Completion> completions = new ArrayList<Completion>(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);
}
SortedSet<CharSequence> allCandidates = new TreeSet<>();
// Append candidates from completions which have the same cursor position as max
for (Completion completion : completions) {
if (completion.cursor == max) {
allCandidates.addAll(completion.candidates);
}
}
candidates.addAll(allCandidates);
return max;
}
Aggregations