1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-07-30 21:57:44 +00:00

replace filter with where (#1135)

This PR tries to clean up the use of `filter` by replacing it with
`where`. I did not test each script.

closes #1134
closes #1133
This commit is contained in:
Darren Schroeder 2025-06-12 06:42:16 -05:00 committed by GitHub
parent 84c25bbc6d
commit 32cdc96414
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 66 additions and 66 deletions

View file

@ -279,7 +279,7 @@ def columns [
] ]
if not ($query | is-empty) { if not ($query | is-empty) {
$columns = $columns | filter { $columns = $columns | where {
$in | str contains --ignore-case $query $in | str contains --ignore-case $query
} }
} }

View file

@ -14,7 +14,7 @@ def "nu-complete flutter commands" [] {
| into string | into string
| str replace --regex --multiline '(Manage[\s\S]*(?=Flutter SDK))' '' | str replace --regex --multiline '(Manage[\s\S]*(?=Flutter SDK))' ''
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| each { str trim } | each { str trim }
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim
@ -33,7 +33,7 @@ def "nu-complete pub commands" [] {
| into string | into string
| str replace --regex --multiline '(Commands[\s\S]*(?=Available subcommands))' '' | str replace --regex --multiline '(Commands[\s\S]*(?=Available subcommands))' ''
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| each { str trim } | each { str trim }
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim

View file

@ -2,7 +2,7 @@
def "nu-complete gh" [] { def "nu-complete gh" [] {
^gh --help ^gh --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -16,7 +16,7 @@ export extern "gh" [
def "nu-complete gh auth" [] { def "nu-complete gh auth" [] {
^gh auth --help ^gh auth --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -43,7 +43,7 @@ export extern "gh browse" [
def "nu-complete gh codespace" [] { def "nu-complete gh codespace" [] {
^gh codespace --help ^gh codespace --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -57,7 +57,7 @@ export extern "gh codespace" [
def "nu-complete gh gist" [] { def "nu-complete gh gist" [] {
^gh gist --help ^gh gist --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -129,7 +129,7 @@ export extern "gh gist view" [
def "nu-complete gh issue" [] { def "nu-complete gh issue" [] {
^gh issue --help ^gh issue --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -144,7 +144,7 @@ export extern "gh issue" [
def "nu-complete gh org" [] { def "nu-complete gh org" [] {
^gh org --help ^gh org --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -156,7 +156,7 @@ export extern "gh org" [
] ]
def "nu-complete gh pr" [] { def "nu-complete gh pr" [] {
^gh pr --help | lines | filter { str starts-with " " } | skip 1 | parse "{value}: {description}" | str trim ^gh pr --help | lines | where { str starts-with " " } | skip 1 | parse "{value}: {description}" | str trim
} }
export extern "gh pr" [ export extern "gh pr" [
@ -182,7 +182,7 @@ export extern "gh pr checkout" [
def "nu-complete gh project" [] { def "nu-complete gh project" [] {
^gh project --help ^gh project --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -196,7 +196,7 @@ export extern "gh project" [
def "nu-complete gh release" [] { def "nu-complete gh release" [] {
^gh release --help ^gh release --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -211,7 +211,7 @@ export extern "gh release" [
def "nu-complete gh repo" [] { def "nu-complete gh repo" [] {
^gh repo --help ^gh repo --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -310,7 +310,7 @@ export extern "gh repo fork" [
def "nu-complete gh cache" [] { def "nu-complete gh cache" [] {
^gh cache --help ^gh cache --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -325,7 +325,7 @@ export extern "gh cache" [
def "nu-complete gh run" [] { def "nu-complete gh run" [] {
^gh run --help ^gh run --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -339,7 +339,7 @@ export extern "gh run" [
def "nu-complete gh workflow" [] { def "nu-complete gh workflow" [] {
^gh workflow --help ^gh workflow --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -354,7 +354,7 @@ export extern "gh workflow" [
def "nu-complete gh alias" [] { def "nu-complete gh alias" [] {
^gh alias --help ^gh alias --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -403,7 +403,7 @@ export extern "gh completion" [
def "nu-complete gh config" [] { def "nu-complete gh config" [] {
^gh config --help ^gh config --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -417,7 +417,7 @@ export extern "gh config" [
def "nu-complete gh extension" [] { def "nu-complete gh extension" [] {
^gh extension --help ^gh extension --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -431,7 +431,7 @@ export extern "gh extension" [
def "nu-complete gh gpg-key" [] { def "nu-complete gh gpg-key" [] {
^gh gpg-key --help ^gh gpg-key --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -445,7 +445,7 @@ export extern "gh gpg-key" [
def "nu-complete gh label" [] { def "nu-complete gh label" [] {
^gh label --help ^gh label --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -460,7 +460,7 @@ export extern "gh label" [
def "nu-complete gh ruleset" [] { def "nu-complete gh ruleset" [] {
^gh ruleset --help ^gh ruleset --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -475,7 +475,7 @@ export extern "gh ruleset" [
def "nu-complete gh search" [] { def "nu-complete gh search" [] {
^gh search --help ^gh search --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -501,7 +501,7 @@ export extern "gh search" [
def "nu-complete gh secret" [] { def "nu-complete gh secret" [] {
^gh secret --help ^gh secret --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -516,7 +516,7 @@ export extern "gh secret" [
def "nu-complete gh ssh-key" [] { def "nu-complete gh ssh-key" [] {
^gh ssh-key --help ^gh ssh-key --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim
@ -536,7 +536,7 @@ export extern "gh status" [
def "nu-complete gh variable" [] { def "nu-complete gh variable" [] {
^gh variable --help ^gh variable --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| skip 1 | skip 1
| parse "{value}: {description}" | parse "{value}: {description}"
| str trim | str trim

View file

@ -37,7 +37,7 @@ def "nu-complete args" [context:string,offset:int] {
def get-just-arg [name:string,position:int] { def get-just-arg [name:string,position:int] {
let recipes = ( let recipes = (
get-recipes get-recipes
| filter {|r| $r.name == $name } | where {|r| $r.name == $name }
| get parameters | get parameters
| flatten) | flatten)
let len = $recipes | length let len = $recipes | length

View file

@ -85,7 +85,7 @@ def "nu-complete kw configs" [] {
def "nu-complete kw remotes" [] { def "nu-complete kw remotes" [] {
^kw remote --list ^kw remote --list
| lines | lines
| filter {|line| not ($line | str contains " ")} | where {|line| not ($line | str contains " ")}
} }
# The inglorious kernel developer workflow tool # The inglorious kernel developer workflow tool

View file

@ -44,7 +44,7 @@ module lftp-completion-utils {
let files = [ let files = [
'/etc/ssh/ssh_config', '/etc/ssh/ssh_config',
'~/.ssh/config' '~/.ssh/config'
] | filter {|file| $file | path exists } ] | where {|file| $file | path exists }
let first_result: record<hosts: list<record<name: string, addr: string>>, includes: list<string>> = $files | par-each {|file| let first_result: record<hosts: list<record<name: string, addr: string>>, includes: list<string>> = $files | par-each {|file|

View file

@ -34,7 +34,7 @@ def "nu-complete mask args" [context: string, offset: int] {
$rt ++= ($c | get named_flags | each {|x| $rt ++= ($c | get named_flags | each {|x|
let v = if not ($x.long | is-empty) { $"`--($x.long)`" } else if not ($x.short | is-empty) { $"`-($x.short)`" } else { $"---($x.name)" } let v = if not ($x.long | is-empty) { $"`--($x.long)`" } else if not ($x.short | is-empty) { $"`-($x.short)`" } else { $"---($x.name)" }
let a = ["required", "multiple", "takes_value", "validate_as_number"] let a = ["required", "multiple", "takes_value", "validate_as_number"]
| filter {|y| ($x | get $y) == true } | where {|y| ($x | get $y) == true }
| str join ',' | str join ','
let d = if ($a | is-empty) { $x.description } else { $"($x.description) \(($a))" } let d = if ($a | is-empty) { $x.description } else { $"($x.description) \(($a))" }
{value: $v , description: $d } {value: $v , description: $d }

View file

@ -21,7 +21,7 @@ export def "nu-complete pass-directories" [] {
let dir = (pass_completions_directory) let dir = (pass_completions_directory)
ls ($dir | path join **) ls ($dir | path join **)
| get name | get name
| filter { |it| not (ls $it | is-empty) } | where { |it| not (ls $it | is-empty) }
| each {|it| ( $it | path relative-to $dir) } | each {|it| ( $it | path relative-to $dir) }
} }

View file

@ -5,7 +5,7 @@ export extern "rye" [
def "nu-complete rye" [] { def "nu-complete rye" [] {
^rye --help ^rye --help
| lines | lines
| filter { str starts-with " " } | where { str starts-with " " }
| each {str trim} | each {str trim}
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim
@ -120,7 +120,7 @@ def "nu-complete rye tools" [] {
^rye tools --help ^rye tools --help
| lines | lines
| drop 1 | drop 1
| filter { str starts-with " " } | where { str starts-with " " }
| each {str trim} | each {str trim}
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim
@ -135,7 +135,7 @@ def "nu-complete rye self" [] {
^rye self --help ^rye self --help
| lines | lines
| drop 1 | drop 1
| filter { str starts-with " " } | where { str starts-with " " }
| each {str trim} | each {str trim}
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim
@ -150,7 +150,7 @@ def "nu-complete rye toolchain" [] {
^rye toolchain --help ^rye toolchain --help
| lines | lines
| drop 1 | drop 1
| filter { str starts-with " " } | where { str starts-with " " }
| each {str trim} | each {str trim}
| parse "{value} {description}" | parse "{value} {description}"
| str trim | str trim

View file

@ -79,7 +79,7 @@ def "nu-complete ssh-host" [] {
let files = [ let files = [
'/etc/ssh/ssh_config', '/etc/ssh/ssh_config',
'~/.ssh/config' '~/.ssh/config'
] | filter {|file| $file | path exists } ] | where {|file| $file | path exists }
use ssh-completion-utils process use ssh-completion-utils process

View file

@ -369,7 +369,7 @@ export def "winget list" [
(do $flagify help $help) (do $flagify help $help)
] ]
| flatten | flatten
| filter { not ($in | is-empty) }) | where { not ($in | is-empty) })
) )
let output = ^winget ...$params let output = ^winget ...$params

View file

@ -28,7 +28,7 @@ export def 'from tree' [
if not ($fst in ['list', 'record']) { if not ($fst in ['list', 'record']) {
$acc $acc
} else { } else {
let r = $acc.schema | filter {|i| ($i | get $selector.value) == $x} let r = $acc.schema | where {|i| ($i | get $selector.value) == $x}
if ($r | is-empty) { if ($r | is-empty) {
$acc $acc
} else { } else {

View file

@ -97,7 +97,7 @@ export def image-select [name] {
let imgs = (image-list) let imgs = (image-list)
let fs = [image tag repo] let fs = [image tag repo]
for i in 2..0 { for i in 2..0 {
let r = $imgs | filter {|x| let r = $imgs | where {|x|
$fs | slice 0..$i | all {|y| ($n | get $y) == ($x | get $y) } $fs | slice 0..$i | all {|y| ($n | get $y) == ($x | get $y) }
} }
if ($r | is-not-empty) { if ($r | is-not-empty) {

View file

@ -60,7 +60,7 @@ def "nu-complete git log" [] {
def "nu-complete git branches" [] { def "nu-complete git branches" [] {
git branch git branch
| lines | lines
| filter {|x| not ($x | str starts-with '*')} | where {|x| not ($x | str starts-with '*')}
| each {|x| $"($x|str trim)"} | each {|x| $"($x|str trim)"}
} }

View file

@ -141,7 +141,7 @@ def list_merged [
run-external "git" "branch" ...$args run-external "git" "branch" ...$args
| lines | lines
| filter {|branch| | where {|branch|
$keep $keep
| all {|pattern| | all {|pattern|
$branch !~ $'\A($pattern)\z' $branch !~ $'\A($pattern)\z'

View file

@ -101,7 +101,7 @@ export def gb [
git branch -D $b git branch -D $b
} }
if ($dels | is-not-empty) and (agree 'delete remote branch?!') { if ($dels | is-not-empty) and (agree 'delete remote branch?!') {
for b in ($dels | filter { $"($remote)/($in)" in $remote_branches }) { for b in ($dels | where { $"($remote)/($in)" in $remote_branches }) {
tips $"delete (ansi yellow)($remote)/($b)" tips $"delete (ansi yellow)($remote)/($b)"
git branch -D -r $'($remote)/($b)' git branch -D -r $'($remote)/($b)'
git push $remote -d $b git push $remote -d $b
@ -657,13 +657,13 @@ def "nu-complete git branch files" [context: string, offset:int] {
let files = $token | skip 2 let files = $token | skip 2
git ls-tree -r --name-only $branch git ls-tree -r --name-only $branch
| lines | lines
| filter {|x| not ($x in $files)} | where {|x| not ($x in $files)}
} }
def "nu-complete git branches" [] { def "nu-complete git branches" [] {
git branch git branch
| lines | lines
| filter {|x| not ($x | str starts-with '*')} | where {|x| not ($x | str starts-with '*')}
| each {|x| $"($x|str trim)"} | each {|x| $"($x|str trim)"}
} }
@ -671,7 +671,7 @@ export def remote_branches [] {
git branch -r git branch -r
| lines | lines
| str trim | str trim
| filter {|x| not ($x | str starts-with 'origin/HEAD') } | where {|x| not ($x | str starts-with 'origin/HEAD') }
} }
def "nu-complete git remotes" [] { def "nu-complete git remotes" [] {

View file

@ -3,7 +3,7 @@ def quote [...t] {
} }
def flatten_fields [args] { def flatten_fields [args] {
let f = $in | default [] | filter {|x| $x | is-not-empty } let f = $in | default [] | where {|x| $x | is-not-empty }
let prefix = $args.0 let prefix = $args.0
let inner = $args.1 let inner = $args.1
let outer = $args.2 let outer = $args.2

View file

@ -16,7 +16,7 @@ export def kube-refine [
let cns = kubectl get namespace let cns = kubectl get namespace
| from ssv -a | from ssv -a
| get NAME | get NAME
| filter $nsf | where $nsf
let resource = kubectl get crd | from ssv | get NAME let resource = kubectl get crd | from ssv | get NAME
let resource = kubectl api-resources | from ssv -a | get NAME | append $resource let resource = kubectl api-resources | from ssv -a | get NAME | append $resource
@ -24,7 +24,7 @@ export def kube-refine [
$resource $resource
} else { } else {
$resource $resource
| filter {|x| $x in $kind } | where {|x| $x in $kind }
} }
mut data = [] mut data = []

View file

@ -77,7 +77,7 @@ export def --wrapped level [
| each (do $env.lg.line_formatter $theme $align $mkl) | each (do $env.lg.line_formatter $theme $align $mkl)
| str join (char newline) | str join (char newline)
let head = [$time $label $txt] let head = [$time $label $txt]
| filter {|x| $x | is-not-empty } | where {|x| $x | is-not-empty }
| str join $theme.delimiter | str join $theme.delimiter
[$head $body] | str join (char newline) [$head $body] | str join (char newline)
} else { } else {
@ -85,7 +85,7 @@ export def --wrapped level [
| each {|y| $"($theme.bg)($y.k)=($theme.fg)($y.v)"} | each {|y| $"($theme.bg)($y.k)=($theme.fg)($y.v)"}
| str join ' ' | str join ' '
[$time $label $tag $txt] [$time $label $tag $txt]
| filter {|x| $x | is-not-empty } | where {|x| $x | is-not-empty }
| str join $theme.delimiter | str join $theme.delimiter
} }
$r + $theme.terminal | do $output $r + $theme.terminal | do $output

View file

@ -3,5 +3,5 @@ export def ls-hidden [
--dir(-d):any # The directory you want to list --dir(-d):any # The directory you want to list
] { ] {
let dir = if ($dir | is-empty) { "." } else { $dir } let dir = if ($dir | is-empty) { "." } else { $dir }
ls -a $dir | filter { ($in.name | into string | str starts-with '.') } ls -a $dir | where { ($in.name | into string | str starts-with '.') }
} }

View file

@ -60,7 +60,7 @@ flag_record:record # A object filled all known flags and their values.
} }
| filter { |value| ( $value | describe ) != nothing } | where { |value| ( $value | describe ) != nothing }
} }

View file

@ -25,7 +25,7 @@ module cdpath {
$env.CDPATH $env.CDPATH
| path expand | path expand
| uniq | uniq
| filter {|| $in | path exists } | where {|| $in | path exists }
} }
# Children of a path # Children of a path
@ -59,7 +59,7 @@ module cdpath {
let first = $path | first let first = $path | first
complete_from_cdpath complete_from_cdpath
| filter {|| $in.value | str contains $first } | where {|| $in.value | str contains $first }
| upsert value {|| $"($in.value)/" } | upsert value {|| $"($in.value)/" }
# Complete a child of a CDPATH entry # Complete a child of a CDPATH entry
} else { } else {
@ -86,14 +86,14 @@ module cdpath {
| each {|| | each {||
$in | path join $prefix $in | path join $prefix
} }
| filter {|| | where {||
$in | path exists $in | path exists
} }
| first | first
} }
children $chosen_path children $chosen_path
| filter {|| | where {||
$in | str contains $last $in | str contains $last
} }
| each {|child| | each {|child|

View file

@ -60,7 +60,7 @@ $set ++ $set_b | uniq
### Intersection ### Intersection
$set | filter { |elem| $elem in $set_b } $set | where { |elem| $elem in $set_b }
# Result: [2 3] # Result: [2 3]
@ -68,7 +68,7 @@ $set | filter { |elem| $elem in $set_b }
# $set - $set_b # $set - $set_b
$set | filter { |elem| $elem not-in $set_b } $set | where { |elem| $elem not-in $set_b }
# or # or

View file

@ -53,7 +53,7 @@ def todo [
get_todo get_todo
| reverse | reverse
# change ($num - 1) to $num to have zero-based indexing # change ($num - 1) to $num to have zero-based indexing
| filter {|it, n| $n != ($num - 1) } | where {|it, n| $n != ($num - 1) }
| reverse | reverse
| save $env.TODO -f | save $env.TODO -f
} }

View file

@ -335,20 +335,20 @@ export def run-tests [
commands: (get-annotated $row.name) commands: (get-annotated $row.name)
} }
} }
| filter {|x| ($x.commands|length) > 0} | where {|x| ($x.commands|length) > 0}
| upsert commands {|module| | upsert commands {|module|
$module.commands $module.commands
| create-test-record | create-test-record
} }
| flatten | flatten
| filter {|x| ($x.test|length) > 0} | where {|x| ($x.test|length) > 0}
| filter {|x| if ($exclude_module|is-empty) {true} else {$x.name !~ $exclude_module}} | where {|x| if ($exclude_module|is-empty) {true} else {$x.name !~ $exclude_module}}
| filter {|x| if ($test|is-empty) {true} else {$x.test|any {|y| $y =~ $test}}} | where {|x| if ($test|is-empty) {true} else {$x.test|any {|y| $y =~ $test}}}
| filter {|x| if ($module|is-empty) {true} else {$module == $x.name}} | where {|x| if ($module|is-empty) {true} else {$module == $x.name}}
| update test {|x| | update test {|x|
$x.test $x.test
| filter {|y| if ($test|is-empty) {true} else {$y =~ $test}} | where {|y| if ($test|is-empty) {true} else {$y =~ $test}}
| filter {|y| if ($exclude|is-empty) {true} else {$y !~ $exclude}} | where {|y| if ($exclude|is-empty) {true} else {$y !~ $exclude}}
} }
) )
if $list { if $list {