Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public enum Command {
PIPE,
UNALIAS,
DOC,
SLURP
SLURP,
WHICH
}

private static final String VAR_CONSOLE_OPTIONS = "CONSOLE_OPTIONS";
Expand Down Expand Up @@ -117,6 +118,7 @@ public ConsoleEngineImpl(
commandExecute.put(Command.UNALIAS, new CommandMethods(this::unalias, this::unaliasCompleter));
commandExecute.put(Command.DOC, new CommandMethods(this::doc, this::docCompleter));
commandExecute.put(Command.PIPE, new CommandMethods(this::pipe, this::defaultCompleter));
commandExecute.put(Command.WHICH, new CommandMethods(this::which, this::whichCompleter));
aliasFile = configPath.getUserConfig("aliases.json");
if (aliasFile == null) {
aliasFile = configPath.getUserConfig("aliases.json", true);
Expand Down Expand Up @@ -1176,6 +1178,39 @@ private Object doc(CommandInput input) {
return null;
}

private Object which(CommandInput input) {
final String[] usage = {
"which - Return the full path, command registry or alias of COMMAND(s).",
"Usage: which COMMAND [...]",
" -? --help Displays command help"
};
List<String> out = new ArrayList<>();
try {
parseOptions(usage, input.xargs());
for (String arg : input.args()) {
if (hasAlias(arg)) {
out.add(arg + " is an alias: " + getAlias(arg));
} else if (systemRegistry.hasCommand(arg)) {
out.add(arg + " is registered in " + systemRegistry.name());
} else if (scripts().keySet().contains(arg)) {
Path script = findScript(arg);
out.add(script != null ? script.toString() : arg + ": not found");
} else {
out.add(arg + ": not found");
}
}
out.add("");
} catch (Exception e) {
exception = e;
}
return out;
}

private Path findScript(String command) {
ScriptFile sf = new ScriptFile(command, "", new String[0]);
return sf.isScript() ? sf.script : null;
}

private boolean urlExists(String weburl) {
try {
URL url = URI.create(weburl).toURL();
Expand Down Expand Up @@ -1338,6 +1373,23 @@ private List<Completer> unaliasCompleter(String command) {
return completers;
}

private List<Completer> whichCompleter(String command) {
List<Completer> completers = new ArrayList<>();
List<Completer> parameters = new ArrayList<>();
parameters.add(new StringsCompleter(this::allCommandNames));
completers.add(new ArgumentCompleter(
NullCompleter.INSTANCE, new OptionCompleter(parameters, this::commandOptions, 1)));
return completers;
}

private Set<String> allCommandNames() {
Set<String> out = new HashSet<>();
out.addAll(scripts().keySet());
out.addAll(aliases.keySet());
out.addAll(systemRegistry.commandNames());
return out;
}

private List<String> docs() {
List<String> out = new ArrayList<>();
Map<String, String> docs = consoleOption("docs", null);
Expand Down
Loading