mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
* Display aliases and custom commands in which; Fix #2810 Example output of nu after the commit is applied: ```shell /home/leo/repos/nushell(feature/which_inspect_alias)> def docker-ps [] { docker ps --format '{{json .}}' | from json -o } /home/leo/repos/nushell(feature/which_inspect_alias)> which docker-ps ───┬───────────┬────────────────────────┬───────── # │ arg │ path │ builtin ───┼───────────┼────────────────────────┼───────── 0 │ docker-ps │ nushell custom command │ No ───┴───────────┴────────────────────────┴───────── /home/leo/repos/nushell(feature/which_inspect_alias)> alias d = gid pd /home/leo/repos/nushell(feature/which_inspect_alias)> which d ───┬─────┬───────────────┬───────── # │ arg │ path │ builtin ───┼─────┼───────────────┼───────── 0 │ d │ nushell alias │ No ───┴─────┴───────────────┴───────── ``` * Update documentation
This commit is contained in:
parent
43c10b0625
commit
48f535f02e
3 changed files with 64 additions and 19 deletions
|
@ -20,7 +20,7 @@ impl WholeStreamCommand for Which {
|
|||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Finds a program file."
|
||||
"Finds a program file, alias or custom command."
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
|
@ -44,13 +44,12 @@ fn entry(arg: impl Into<String>, path: Value, builtin: bool, tag: Tag) -> Value
|
|||
UntaggedValue::row(map).into_value(tag)
|
||||
}
|
||||
|
||||
macro_rules! entry_builtin {
|
||||
($arg:expr, $tag:expr) => {
|
||||
macro_rules! create_entry {
|
||||
($arg:expr, $path:expr, $tag:expr, $is_builtin:expr) => {
|
||||
entry(
|
||||
$arg.clone(),
|
||||
UntaggedValue::Primitive(Primitive::String("nushell built-in command".to_string()))
|
||||
.into_value($tag.clone()),
|
||||
true,
|
||||
UntaggedValue::Primitive(Primitive::String($path.to_string())).into_value($tag.clone()),
|
||||
$is_builtin,
|
||||
$tag,
|
||||
)
|
||||
};
|
||||
|
@ -86,12 +85,8 @@ async fn which(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||
application.item.clone()
|
||||
};
|
||||
if !external {
|
||||
let builtin = scope.has_command(&item);
|
||||
if builtin {
|
||||
output.push(ReturnSuccess::value(entry_builtin!(
|
||||
item,
|
||||
application.tag.clone()
|
||||
)));
|
||||
if let Some(entry) = entry_for(&scope, &item, application.tag.clone()) {
|
||||
output.push(ReturnSuccess::value(entry));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,6 +110,18 @@ async fn which(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||
}
|
||||
}
|
||||
|
||||
fn entry_for(scope: &Scope, name: &str, tag: Tag) -> Option<Value> {
|
||||
if scope.has_custom_command(name) {
|
||||
Some(create_entry!(name, "Nushell custom command", tag, false))
|
||||
} else if scope.has_command(name) {
|
||||
Some(create_entry!(name, "Nushell built-in command", tag, true))
|
||||
} else if scope.has_alias(name) {
|
||||
Some(create_entry!(name, "Nushell alias", tag, false))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
|
|
|
@ -52,14 +52,20 @@ impl Scope {
|
|||
names
|
||||
}
|
||||
|
||||
pub fn has_command(&self, name: &str) -> bool {
|
||||
for frame in self.frames.lock().iter() {
|
||||
if frame.has_command(name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fn has_cmd_helper(&self, name: &str, f: fn(&ScopeFrame, &str) -> bool) -> bool {
|
||||
self.frames.lock().iter().any(|frame| f(frame, name))
|
||||
}
|
||||
|
||||
false
|
||||
pub fn has_command(&self, name: &str) -> bool {
|
||||
self.has_cmd_helper(name, ScopeFrame::has_command)
|
||||
}
|
||||
|
||||
pub fn has_custom_command(&self, name: &str) -> bool {
|
||||
self.has_cmd_helper(name, ScopeFrame::has_custom_command)
|
||||
}
|
||||
|
||||
pub fn has_alias(&self, name: &str) -> bool {
|
||||
self.has_cmd_helper(name, ScopeFrame::has_alias)
|
||||
}
|
||||
|
||||
pub fn expect_command(&self, name: &str) -> Result<Command, ShellError> {
|
||||
|
@ -203,6 +209,14 @@ impl ScopeFrame {
|
|||
self.commands.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn has_custom_command(&self, name: &str) -> bool {
|
||||
self.custom_commands.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn has_alias(&self, name: &str) -> bool {
|
||||
self.aliases.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn get_command_names(&self) -> Vec<String> {
|
||||
self.commands.keys().map(|x| x.to_string()).collect()
|
||||
}
|
||||
|
|
|
@ -79,3 +79,27 @@ Passing the `all` flag identifies all instances of a command or binary
|
|||
builtin │ No
|
||||
─────────┴────────────────────────────────
|
||||
```
|
||||
|
||||
`which` also identifies aliases
|
||||
|
||||
```shell
|
||||
> alias e = echo
|
||||
> which e
|
||||
───┬─────┬───────────────┬─────────
|
||||
# │ arg │ path │ builtin
|
||||
───┼─────┼───────────────┼─────────
|
||||
0 │ e │ Nushell alias │ No
|
||||
───┴─────┴───────────────┴─────────
|
||||
```
|
||||
|
||||
and custom commands
|
||||
|
||||
```shell
|
||||
> def my_cool_echo [arg] { echo $arg }
|
||||
> which my_cool_echo
|
||||
───┬──────────────┬────────────────────────┬─────────
|
||||
# │ arg │ path │ builtin
|
||||
───┼──────────────┼────────────────────────┼─────────
|
||||
0 │ my_cool_echo │ Nushell custom command │ No
|
||||
───┴──────────────┴────────────────────────┴─────────
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue