-
Notifications
You must be signed in to change notification settings - Fork 222
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b765e9
Table and list helper commands
NotTheDr01ds c5a708e
Fix ranges for col-indices and select-column-range
NotTheDr01ds fe38160
Work around failing test due to core issue
NotTheDr01ds e4206bd
Added tests
NotTheDr01ds 0f76c38
Fixed example
NotTheDr01ds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Convert a Nushell value to a list | ||
# | ||
# Primary useful for range-to-list, | ||
# but other types are accepted as well. | ||
# | ||
# Example: | ||
# | ||
# 1..10 | into list | ||
export def "into list" []: any -> list { | ||
let input = $in | ||
let type = ($input | describe --detailed | get type) | ||
match $type { | ||
range => {$input | each {||}} | ||
list => $input | ||
table => $input | ||
_ => [ $input ] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use ../conversions/into.nu * | ||
use ./select-ranges.nu * | ||
|
||
export def main [ ...ranges ] { | ||
columns | ||
| select ranges $ranges | ||
| get item | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export use ./select-ranges.nu * | ||
export use ./reject-ranges.nu * | ||
export use ./select-column-ranges.nu * | ||
export use ./reject-column-ranges.nu * | ||
export use ./row-indices.nu * | ||
export use ./col-indices.nu * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use ./col-indices.nu * | ||
|
||
# Relect a range of columns by their indices | ||
# | ||
# Example: | ||
# | ||
# ls | reject column-ranges 0 4 5 | first 3 | ||
export def "reject column-ranges" [ | ||
...ranges | ||
] { | ||
let column_selector = ($in | col-indices ...$ranges) | ||
$in | reject ...$column_selector | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use ./row-indices.nu * | ||
|
||
# Rejects one or more rows while keeping | ||
# the original indices. | ||
# | ||
# Example - Rejects the first, fifth, and | ||
# sixth rows from the table: | ||
# | ||
# ls / | reject ranges 0 4..5 | ||
export def "reject ranges" [ ...ranges ] { | ||
enumerate | ||
| flatten | ||
| reject ...(row-indices ...$ranges) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use ../conversions/into.nu * | ||
|
||
# Return a list of indices | ||
# for the provided ranges or indices. | ||
# Primarily used as a helper for | ||
# "select ranges" et. al. | ||
# | ||
# Example: | ||
# | ||
# row-indices 0 2..5 7..8 | ||
# # => ╭───┬───╮ | ||
# # => │ 0 │ 0 │ | ||
# # => │ 1 │ 2 │ | ||
# # => │ 2 │ 3 │ | ||
# # => │ 3 │ 4 │ | ||
# # => │ 4 │ 5 │ | ||
# # => │ 5 │ 7 │ | ||
# # => │ 6 │ 8 │ | ||
# # => ╰───┴───╯ | ||
export def main [ ...ranges ] { | ||
$ranges | ||
| reduce -f [] {|range,indices| | ||
$indices ++ ($range | into list) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use ./col-indices.nu * | ||
|
||
# Select a range of columns by their indices | ||
# | ||
# Example: | ||
# | ||
# ls | select column-ranges 0 4 5 | first 3 | ||
# # => ╭───┬────────────────────╮ | ||
# # => │ # │ name │ | ||
# # => ├───┼────────────────────┤ | ||
# # => │ 0 │ CITATION.cff │ | ||
# # => │ 1 │ CODE_OF_CONDUCT.md │ | ||
# # => │ 2 │ CONTRIBUTING.md │ | ||
# # => ╰───┴────────────────────╯ | ||
export def "select column-ranges" [ | ||
...ranges | ||
] { | ||
let column_selector = ($in | col-indices ...$ranges) | ||
$in | select ...$column_selector | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use ./row-indices.nu * | ||
|
||
# Selects one or more rows while keeping | ||
# the original indices. | ||
# | ||
# Example - Selects the first, fifth, and | ||
# sixth rows from the table: | ||
# | ||
# ls / | select ranges 0 4..5 | ||
# | ||
# Example - Select the 5th row: | ||
# | ||
# ls / | select 5 | ||
# | ||
# Example - Select the 4th row. | ||
# Note that the difference beteen this | ||
# and `select 3` is that the index (#) | ||
# column shows the *original* (pre-select) | ||
# position in the table. | ||
# | ||
# ls | select ranges 3 | ||
export def "select ranges" [ ...ranges ] { | ||
enumerate | ||
| flatten | ||
| select ...(row-indices ...$ranges) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?