Skip to content

Commit a1f989c

Browse files
authored
change the output of which to be more explicit (#9646)
related to - #9637 (comment) # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - ⚫ `toolkit test` - ⚫ `toolkit test stdlib` # After Submitting mention that in the release note
1 parent c01f2ee commit a1f989c

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

crates/nu-command/src/system/which_.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,40 @@ impl Command for Which {
5757
}
5858

5959
// Shortcut for creating an entry to the output table
60-
fn entry(arg: impl Into<String>, path: impl Into<String>, builtin: bool, span: Span) -> Value {
60+
fn entry(
61+
arg: impl Into<String>,
62+
path: impl Into<String>,
63+
cmd_type: impl Into<String>,
64+
span: Span,
65+
) -> Value {
6166
let mut cols = vec![];
6267
let mut vals = vec![];
6368

64-
cols.push("arg".to_string());
69+
cols.push("command".to_string());
6570
vals.push(Value::string(arg.into(), span));
6671

6772
cols.push("path".to_string());
6873
vals.push(Value::string(path.into(), span));
6974

70-
cols.push("built-in".to_string());
71-
vals.push(Value::Bool { val: builtin, span });
75+
cols.push("type".to_string());
76+
vals.push(Value::string(cmd_type.into(), span));
7277

7378
Value::Record { cols, vals, span }
7479
}
7580

7681
fn get_entry_in_commands(engine_state: &EngineState, name: &str, span: Span) -> Option<Value> {
7782
if let Some(decl_id) = engine_state.find_decl(name.as_bytes(), &[]) {
78-
let (msg, is_builtin) = if engine_state.get_decl(decl_id).is_custom_command() {
79-
("Nushell custom command", false)
83+
let cmd_type = if engine_state.get_decl(decl_id).is_custom_command() {
84+
"custom"
8085
} else if engine_state.get_decl(decl_id).is_alias() {
81-
("Nushell alias", false)
86+
"alias"
8287
} else {
83-
("Nushell built-in command", true)
88+
"built-in"
8489
};
8590

8691
trace!("Found command: {}", name);
8792

88-
Some(entry(name, msg, is_builtin, span))
93+
Some(entry(name, "", cmd_type, span))
8994
} else {
9095
None
9196
}
@@ -118,7 +123,7 @@ fn get_first_entry_in_path(
118123
paths: impl AsRef<OsStr>,
119124
) -> Option<Value> {
120125
which::which_in(item, Some(paths), cwd)
121-
.map(|path| entry(item, path.to_string_lossy().to_string(), false, span))
126+
.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
122127
.ok()
123128
}
124129

@@ -141,7 +146,7 @@ fn get_all_entries_in_path(
141146
) -> Vec<Value> {
142147
which::which_in_all(&item, Some(paths), cwd)
143148
.map(|iter| {
144-
iter.map(|path| entry(item, path.to_string_lossy().to_string(), false, span))
149+
iter.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
145150
.collect()
146151
})
147152
.unwrap_or_default()

crates/nu-command/tests/commands/which.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use nu_test_support::nu;
22

33
#[test]
44
fn which_ls() {
5-
let actual = nu!("which ls | get path.0 | str trim");
5+
let actual = nu!("which ls | get type.0");
66

7-
assert_eq!(actual.out, "Nushell built-in command");
7+
assert_eq!(actual.out, "built-in");
88
}
99

1010
#[ignore = "TODO: Can't have alias recursion"]
@@ -19,17 +19,14 @@ fn which_alias_ls() {
1919
fn which_custom_alias() {
2020
let actual = nu!(r#"alias foo = print "foo!"; which foo | to nuon"#);
2121

22-
assert_eq!(
23-
actual.out,
24-
"[[arg, path, built-in]; [foo, \"Nushell alias\", false]]"
25-
);
22+
assert_eq!(actual.out, r#"[[command, path, type]; [foo, "", alias]]"#);
2623
}
2724

2825
#[test]
2926
fn which_def_ls() {
30-
let actual = nu!("def ls [] {echo def}; which ls | get path.0 | str trim");
27+
let actual = nu!("def ls [] {echo def}; which ls | get type.0");
3128

32-
assert_eq!(actual.out, "Nushell custom command");
29+
assert_eq!(actual.out, "custom");
3330
}
3431

3532
#[ignore = "TODO: Can't have alias with the same name as command"]
@@ -58,24 +55,24 @@ fn multiple_reports_for_alias_def_custom() {
5855
#[ignore]
5956
#[test]
6057
fn correctly_report_of_shadowed_alias() {
61-
let actual = nu!(r#"alias xaz = echo alias1
58+
let actual = nu!("alias xaz = echo alias1
6259
def helper [] {
6360
alias xaz = echo alias2
6461
which -a xaz
6562
}
66-
helper | get path | str contains alias2"#);
63+
helper | get path | str contains alias2");
6764

6865
assert_eq!(actual.out, "true");
6966
}
7067

7168
#[test]
7269
fn one_report_of_multiple_defs() {
73-
let actual = nu!(r#"def xaz [] { echo def1 }
70+
let actual = nu!("def xaz [] { echo def1 }
7471
def helper [] {
7572
def xaz [] { echo def2 }
7673
which -a xaz
7774
}
78-
helper | length"#);
75+
helper | length");
7976

8077
let length: i32 = actual.out.parse().unwrap();
8178
assert_eq!(length, 1);
@@ -91,19 +88,19 @@ fn def_only_seen_once() {
9188

9289
#[test]
9390
fn do_not_show_hidden_aliases() {
94-
let actual = nu!(r#"alias foo = echo foo
91+
let actual = nu!("alias foo = echo foo
9592
hide foo
96-
which foo | length"#);
93+
which foo | length");
9794

9895
let length: i32 = actual.out.parse().unwrap();
9996
assert_eq!(length, 0);
10097
}
10198

10299
#[test]
103100
fn do_not_show_hidden_commands() {
104-
let actual = nu!(r#"def foo [] { echo foo }
101+
let actual = nu!("def foo [] { echo foo }
105102
hide foo
106-
which foo | length"#);
103+
which foo | length");
107104

108105
let length: i32 = actual.out.parse().unwrap();
109106
assert_eq!(length, 0);

0 commit comments

Comments
 (0)