change the output of which to be more explicit (#9646)

related to
- https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548

# 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
This commit is contained in:
Antoine Stevan
2023-07-21 02:10:53 +02:00
committed by GitHub
parent c01f2ee0e9
commit a1f989caf9
2 changed files with 29 additions and 27 deletions

View File

@ -57,35 +57,40 @@ impl Command for Which {
}
// Shortcut for creating an entry to the output table
fn entry(arg: impl Into<String>, path: impl Into<String>, builtin: bool, span: Span) -> Value {
fn entry(
arg: impl Into<String>,
path: impl Into<String>,
cmd_type: impl Into<String>,
span: Span,
) -> Value {
let mut cols = vec![];
let mut vals = vec![];
cols.push("arg".to_string());
cols.push("command".to_string());
vals.push(Value::string(arg.into(), span));
cols.push("path".to_string());
vals.push(Value::string(path.into(), span));
cols.push("built-in".to_string());
vals.push(Value::Bool { val: builtin, span });
cols.push("type".to_string());
vals.push(Value::string(cmd_type.into(), span));
Value::Record { cols, vals, span }
}
fn get_entry_in_commands(engine_state: &EngineState, name: &str, span: Span) -> Option<Value> {
if let Some(decl_id) = engine_state.find_decl(name.as_bytes(), &[]) {
let (msg, is_builtin) = if engine_state.get_decl(decl_id).is_custom_command() {
("Nushell custom command", false)
let cmd_type = if engine_state.get_decl(decl_id).is_custom_command() {
"custom"
} else if engine_state.get_decl(decl_id).is_alias() {
("Nushell alias", false)
"alias"
} else {
("Nushell built-in command", true)
"built-in"
};
trace!("Found command: {}", name);
Some(entry(name, msg, is_builtin, span))
Some(entry(name, "", cmd_type, span))
} else {
None
}
@ -118,7 +123,7 @@ fn get_first_entry_in_path(
paths: impl AsRef<OsStr>,
) -> Option<Value> {
which::which_in(item, Some(paths), cwd)
.map(|path| entry(item, path.to_string_lossy().to_string(), false, span))
.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
.ok()
}
@ -141,7 +146,7 @@ fn get_all_entries_in_path(
) -> Vec<Value> {
which::which_in_all(&item, Some(paths), cwd)
.map(|iter| {
iter.map(|path| entry(item, path.to_string_lossy().to_string(), false, span))
iter.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
.collect()
})
.unwrap_or_default()