mirror of
https://github.com/nushell/nushell.git
synced 2025-04-30 16:14:27 +02: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
@ -20,7 +20,7 @@ impl WholeStreamCommand for Which {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
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> {
|
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)
|
UntaggedValue::row(map).into_value(tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! entry_builtin {
|
macro_rules! create_entry {
|
||||||
($arg:expr, $tag:expr) => {
|
($arg:expr, $path:expr, $tag:expr, $is_builtin:expr) => {
|
||||||
entry(
|
entry(
|
||||||
$arg.clone(),
|
$arg.clone(),
|
||||||
UntaggedValue::Primitive(Primitive::String("nushell built-in command".to_string()))
|
UntaggedValue::Primitive(Primitive::String($path.to_string())).into_value($tag.clone()),
|
||||||
.into_value($tag.clone()),
|
$is_builtin,
|
||||||
true,
|
|
||||||
$tag,
|
$tag,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
@ -86,12 +85,8 @@ async fn which(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
application.item.clone()
|
application.item.clone()
|
||||||
};
|
};
|
||||||
if !external {
|
if !external {
|
||||||
let builtin = scope.has_command(&item);
|
if let Some(entry) = entry_for(&scope, &item, application.tag.clone()) {
|
||||||
if builtin {
|
output.push(ReturnSuccess::value(entry));
|
||||||
output.push(ReturnSuccess::value(entry_builtin!(
|
|
||||||
item,
|
|
||||||
application.tag.clone()
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::ShellError;
|
use super::ShellError;
|
||||||
|
@ -52,14 +52,20 @@ impl Scope {
|
|||||||
names
|
names
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_command(&self, name: &str) -> bool {
|
fn has_cmd_helper(&self, name: &str, f: fn(&ScopeFrame, &str) -> bool) -> bool {
|
||||||
for frame in self.frames.lock().iter() {
|
self.frames.lock().iter().any(|frame| f(frame, name))
|
||||||
if frame.has_command(name) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
pub fn expect_command(&self, name: &str) -> Result<Command, ShellError> {
|
||||||
@ -203,6 +209,14 @@ impl ScopeFrame {
|
|||||||
self.commands.contains_key(name)
|
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> {
|
pub fn get_command_names(&self) -> Vec<String> {
|
||||||
self.commands.keys().map(|x| x.to_string()).collect()
|
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
|
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 New Issue
Block a user