mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:06:03 +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:
@ -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;
|
||||
|
Reference in New Issue
Block a user