nushell / nu_scripts

A place to share Nushell scripts with each other
MIT License
772 stars 230 forks source link

Table and list helper commands #967

Closed NotTheDr01ds closed 1 month ago

NotTheDr01ds commented 1 month ago

From https://github.com/nushell/nushell/issues/13887, this adds:

into list
select ranges
select column-ranges
reject ranges
reject column-ranges
row-indices
col-indices

Note that these replace the former get-row and get-col commands that were in the library. They have the same functionality and can accept any combination of:

NotTheDr01ds commented 1 month ago

It works with ranges - At least select ranges did. I had a slight bug in select column-ranges that I just pushed the fix for. (Which reminds me - Before promoting anything to std we need test cases in place).

The ... ranges commands all work with a mixture of integer-indices and ranges, so ... naming suggestions?

I went with select/reject ...ranges because I posit that a single int can be considered a shorthand for a range of <int>..<int> .

The only thing it can't do is an infinity-range, of course (and I might be able to eke that out), but all of the following work:

# Same as | first 3
ls | select ranges ..2
# => ╭───┬────────────────────┬──────┬──────────┬──────────────╮
# => │ # │        name        │ type │   size   │   modified   │
# => ├───┼────────────────────┼──────┼──────────┼──────────────┤
# => │ 0 │ CITATION.cff       │ file │    812 B │ 3 months ago │
# => │ 1 │ CODE_OF_CONDUCT.md │ file │  3.4 KiB │ 7 months ago │
# => │ 2 │ CONTRIBUTING.md    │ file │ 11.0 KiB │ 3 months ago │
# => ╰───┴────────────────────┴──────┴──────────┴──────────────╯

# Every other row between 0 and 8 inclusive
ls | select ranges 0..2..8
# => ╭───┬─────────────────┬──────┬──────────┬──────────────╮
# => │ # │      name       │ type │   size   │   modified   │
# => ├───┼─────────────────┼──────┼──────────┼──────────────┤
# => │ 0 │ CITATION.cff    │ file │    812 B │ 3 months ago │
# => │ 2 │ CONTRIBUTING.md │ file │ 11.0 KiB │ 3 months ago │
# => │ 4 │ Cargo.toml      │ file │  9.0 KiB │ 8 hours ago  │
# => │ 6 │ LICENSE         │ file │  1.1 KiB │ 7 months ago │
# => │ 8 │ SECURITY.md     │ file │  2.6 KiB │ 2 months ago │
# => ╰───┴─────────────────┴──────┴──────────┴──────────────╯

# Every other column from the first three trows
ls -l | select column-ranges 0..2..100 | select ranges ..2
# => ╭───┬────────────────────┬────────┬───────────┬────────┬───────┬──────────────┬──────────────╮
# => │ # │        name        │ target │   mode    │ inode  │ group │   created    │   modified   │
# => ├───┼────────────────────┼────────┼───────────┼────────┼───────┼──────────────┼──────────────┤
# => │ 0 │ CITATION.cff       │        │ rw-r--r-- │ 393244 │ ntd   │ 3 months ago │ 3 months ago │
# => │ 1 │ CODE_OF_CONDUCT.md │        │ rw-r--r-- │ 430741 │ ntd   │ 7 months ago │ 7 months ago │
# => │ 2 │ CONTRIBUTING.md    │        │ rw-r--r-- │ 371737 │ ntd   │ 3 months ago │ 3 months ago │
# => ╰───┴────────────────────┴────────┴───────────┴────────┴───────┴──────────────┴──────────────╯

# Every third column from rows 4, 7, 9, and 10
ls -l | select column-ranges 0..3..100 | select ranges 4 7 9..10
# => ╭────┬────────────┬──────────┬────────┬──────────┬──────────────╮
# => │  # │    name    │ readonly │ inode  │   size   │   modified   │
# => ├────┼────────────┼──────────┼────────┼──────────┼──────────────┤
# => │  4 │ Cargo.toml │ false    │  77221 │  9.0 KiB │ 8 hours ago  │
# => │  7 │ README.md  │ false    │ 371008 │ 12.0 KiB │ 3 months ago │
# => │  9 │ assets     │ false    │ 430748 │  4.0 KiB │ 7 months ago │
# => │ 10 │ benches    │ false    │ 430753 │  4.0 KiB │ 19 hours ago │
# => ╰────┴────────────┴──────────┴────────┴──────────┴──────────────╯
fdncred commented 1 month ago

gotcha, as long as it works, it's probably fine. do you want to wait for tests or land this?

NotTheDr01ds commented 1 month ago

Ah, I'm going to hate myself for saying this, but hold off. It will push me to get the tests written sooner ;-)

NotTheDr01ds commented 1 month ago

Tests added.

Also fixed the bulk_rename test that was failing due to the ls <dir> no longer being sorted.

fdncred commented 1 month ago

Good work! Thanks!!