Skip to content

Proof of concept for command/utility separation in ls - #10651

Closed
skyvine wants to merge 1 commit into
nushell:mainfrom
skyvine:ls-command-utility-separation
Closed

skyvine wants to merge 1 commit into
nushell:mainfrom
skyvine:ls-command-utility-separation

Conversation

@skyvine

@skyvine skyvine commented Oct 8, 2023

Copy link
Copy Markdown

Description

This PR demonstrates a proof-of-concept for the feature request in #10650. Note that this is a crude proof-of-concept, and you will see several TODO notes (which are not comprehensive).

With this proof-of-concept, ls is redefined to accept a list of strings as input instead of a command-line argument. This means that it must be called differently, and in a final version this would be renamed to fetch-file-metadata instead of ls to avoid confusion (note: the copy/paste did something funky with the table borders, they are still rendered as smooth lines on my terminal):

> ls # if there are no inputs, behavior is unchanged
╭────┬──────────────────────────────────────────────────────────┬──────┬──────────┬─────────────╮
│  # │                           name                           │ type │   size   │  modified   │
├────┼──────────────────────────────────────────────────────────┼──────┼──────────┼─────────────┤
│  0 │ /home/user/Projects/nushell/checkout/src                 │ dir  │   4.1 KB │ a week ago  │
│  1 │ /home/user/Projects/nushell/checkout/.gitattributes      │ file │    111 B │ a week ago  │
│  2 │ /home/user/Projects/nushell/checkout/rust-toolchain.toml │ file │   1.1 KB │ 3 hours ago │
│  3 │ /home/user/Projects/nushell/checkout/CODE_OF_CONDUCT.md  │ file │   3.4 KB │ 2 weeks ago │
│  4 │ /home/user/Projects/nushell/checkout/target              │ dir  │   4.1 KB │ an hour ago │
│  5 │ /home/user/Projects/nushell/checkout/docker              │ dir  │   4.1 KB │ a week ago  │
│  6 │ /home/user/Projects/nushell/checkout/README.md           │ file │  12.0 KB │ a week ago  │
│  7 │ /home/user/Projects/nushell/checkout/Cargo.lock          │ file │ 148.8 KB │ 3 hours ago │
│  8 │ /home/user/Projects/nushell/checkout/scripts             │ dir  │   4.1 KB │ a week ago  │
│  9 │ /home/user/Projects/nushell/checkout/.cargo              │ dir  │   4.1 KB │ a week ago  │
│ 10 │ /home/user/Projects/nushell/checkout/.github             │ dir  │   4.1 KB │ a week ago  │
│ 11 │ /home/user/Projects/nushell/checkout/tests               │ dir  │   4.1 KB │ 2 weeks ago │
│ 12 │ /home/user/Projects/nushell/checkout/.git                │ dir  │   4.1 KB │ an hour ago │
│ 13 │ /home/user/Projects/nushell/checkout/Cargo.toml          │ file │   5.8 KB │ a week ago  │
│ 14 │ /home/user/Projects/nushell/checkout/benches             │ dir  │   4.1 KB │ a week ago  │
│ 15 │ /home/user/Projects/nushell/checkout/LICENSE             │ file │   1.1 KB │ 2 weeks ago │
│ 16 │ /home/user/Projects/nushell/checkout/.gitignore          │ file │    660 B │ a week ago  │
│ 17 │ /home/user/Projects/nushell/checkout/assets              │ dir  │   4.1 KB │ a week ago  │
│ 18 │ /home/user/Projects/nushell/checkout/wix                 │ dir  │   4.1 KB │ a week ago  │
│ 19 │ /home/user/Projects/nushell/checkout/.githooks           │ dir  │   4.1 KB │ 2 weeks ago │
│ 20 │ /home/user/Projects/nushell/checkout/toolkit.nu          │ file │  14.5 KB │ 3 hours ago │
│ 21 │ /home/user/Projects/nushell/checkout/crates              │ dir  │   4.1 KB │ a week ago  │
│ 22 │ /home/user/Projects/nushell/checkout/Cross.toml          │ file │    666 B │ a week ago  │
│ 23 │ /home/user/Projects/nushell/checkout/CONTRIBUTING.md     │ file │  18.8 KB │ a week ago  │
╰────┴──────────────────────────────────────────────────────────┴──────┴──────────┴─────────────╯
> [/home] | ls # if given a single directory, the contents are still listed 
╭───┬────────────┬──────┬────────┬─────────────╮
│ # │    name    │ type │  size  │  modified   │
├───┼────────────┼──────┼────────┼─────────────┤
│ 0 │ /home/user │ dir  │ 4.1 KB │ an hour ago │
╰───┴────────────┴──────┴────────┴─────────────╯
> [/home /opt] | ls # if given multiple directories, still list the directories themselves                                                                                                    10/08/23 15:12:05 PM
╭───┬───────┬──────┬────────┬──────────────╮
│ # │ name  │ type │  size  │   modified   │
├───┼───────┼──────┼────────┼──────────────┤
│ 0 │ /home │ dir  │ 4.1 KB │ 2 weeks ago  │
│ 1 │ /opt  │ dir  │ 4.1 KB │ 2 months ago │
╰───┴───────┴──────┴────────┴──────────────╯

The default config file has been updated to provide a function named new-ls, which would be renamed to just ls in a final version:

> new-ls /home                                                                                                                                                                                10/08/23 15:13:40 PM
╭───┬────────────┬──────┬────────┬─────────────╮
│ # │    name    │ type │  size  │  modified   │
├───┼────────────┼──────┼────────┼─────────────┤
│ 0 │ /home/user │ dir  │ 4.1 KB │ an hour ago │
╰───┴────────────┴──────┴────────┴─────────────╯
> new-ls "/{home,opt}"                                                                                                                                                                      1 10/08/23 15:14:29 PM
╭───┬───────┬──────┬────────┬──────────────╮
│ # │ name  │ type │  size  │   modified   │
├───┼───────┼──────┼────────┼──────────────┤
│ 0 │ /home │ dir  │ 4.1 KB │ 2 weeks ago  │
│ 1 │ /opt  │ dir  │ 4.1 KB │ 2 months ago │
╰───┴───────┴──────┴────────┴──────────────╯

Note that I am sometimes experiencing notable delays when using new-ls. I assume that this is due to performance differences between nu_glob and wax. However, it is theoretically possible that it has to do with the nu-level function. I would expect that a final version of this PR would also provide a user-facing command which performs globbing as in nu_glob, and this would be used in the nu-level function be default.

One unresolved issue is that new-ls has no way to pass flags like --directory or --short-names to the underlying ls command. It is not clear whether these things are best handled within the Rust-level command, as separate commands in the pipeline, or in a more complicated implementation of new-ls (I would prefer to avoid any code beyond a simple pipeline in at least the default implementations of the nu-level functions).

User-Facing Changes

A final version of this PR would not create any user-facing changes when invoking ls. However, it would give users the additional option of invoking fetch-file-metadata directly.

Tests + Formatting

TODO

After Submitting

TODO

@amtoine amtoine linked an issue Oct 9, 2023 that may be closed by this pull request
@fdncred

fdncred commented Apr 12, 2024

Copy link
Copy Markdown
Contributor

I think this is an interesting idea but hasn't caught on. Since it's so old, let's close for now and maybe revisit this discussion on discord in #design-discussion.

@fdncred fdncred closed this Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Separate commands from utilities

2 participants