Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table and list helper commands #967

Merged
merged 5 commits into from
Oct 3, 2024
Merged

Conversation

NotTheDr01ds
Copy link
Contributor

@NotTheDr01ds NotTheDr01ds commented Oct 2, 2024

From nushell/nushell#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:

  • One or more row or column indices
  • One or more row or column ranges

# Example:
#
# ls | reject column-ranges 0 4 5 | first 3
export def "reject column-ranges" [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is named wrong since 0 4 5 isn't a nushell range?

Copy link
Collaborator

@fdncred fdncred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are useful and cool but I'm just not sure "ranges" works because nushell ranges are 0..2 and 0..2..10. Maybe if someone uses a range it'll still work?

@NotTheDr01ds
Copy link
Contributor Author

NotTheDr01ds commented Oct 2, 2024

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
Copy link
Collaborator

fdncred commented Oct 2, 2024

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

@NotTheDr01ds
Copy link
Contributor Author

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

@fdncred fdncred marked this pull request as draft October 2, 2024 21:20
@NotTheDr01ds
Copy link
Contributor Author

Tests added.

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

@NotTheDr01ds NotTheDr01ds marked this pull request as ready for review October 3, 2024 10:57
@NotTheDr01ds NotTheDr01ds marked this pull request as draft October 3, 2024 10:59
@NotTheDr01ds NotTheDr01ds marked this pull request as ready for review October 3, 2024 11:04
@fdncred fdncred merged commit 743ccc0 into nushell:main Oct 3, 2024
1 check passed
@fdncred
Copy link
Collaborator

fdncred commented Oct 3, 2024

Good work! Thanks!!

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.

2 participants