Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 17 additions & 70 deletions custom-completions/winget/winget-completions.nu
Original file line number Diff line number Diff line change
Expand Up @@ -80,63 +80,6 @@ def "nu-complete winget install id" [] {
}
}

def "nu-complete winget parse table" [lines: any] {
let header = (
$lines
| first
| parse -r '(?P<name>Name\s+)(?P<id>Id\s+)(?P<version>Version\s+)?(?P<match>Match\s+)?(?P<available>Available\s+)?(?P<source>Source\s*)?'
| first
)
let lengths = {
name: ($header.name | str length),
id: ($header.id | str length),
version: ($header.version | default "" | str length),
match: ($header.match | default "" | str length),
available: ($header.available | default "" | str length),
source: ($header.source | default "" | str length)
}
$lines | skip 2 | each { |it|
let it = ($it | split chars)

let version = if $lengths.version > 0 {
(
$it | skip ($lengths.name + $lengths.id)
| first $lengths.version | str join | str trim
)
} else { "" }

let match = if $lengths.match > 0 {
(
$it | skip ($lengths.name + $lengths.id + $lengths.version)
| first $lengths.match | str join | str trim
)
} else { "" }

let available = if $lengths.available > 0 {
(
$it | skip ($lengths.name + $lengths.id + $lengths.version + $lengths.match)
| first $lengths.available | str join | str trim
)
} else { "" }

let source = if $lengths.source > 0 {
(
$it | skip ($lengths.name + $lengths.id + $lengths.version + $lengths.match + $lengths.available)
| str join | str trim
)
} else { "" }

{
name: ($it | first $lengths.name | str join | str trim),
id: ($it | skip $lengths.name | first $lengths.id | str join | str trim),
version: $version,
match: $match,
available: $available,
source: $source
}
}
}

export extern winget [
--version(-v), # Display the version of the tool
--info, # Display general info of the tool
Expand Down Expand Up @@ -251,7 +194,7 @@ export extern "winget source update" [
--name(-n): string, # Name of the source
--help(-?) # Display the help for this command
]
export alias "winget source refresh" = winet source update
export alias "winget source refresh" = winget source update

# Remove current sources
export extern "winget source remove" [
Expand Down Expand Up @@ -315,15 +258,18 @@ export def "winget search" [
| where { not ($in | is-empty) }
)

let output = (^winget ...$params)
let output = ^winget ...$params
# remove loading symbols at start of output
| str replace -r r#'^[^\w]*'# ""
if $raw or $help {
$output
} else {
let lines = ($output | lines)
let lines = ($output | detect columns --guess) | rename name id version match source
if ($lines | length) == 1 {
$"(ansi light_red)($lines | first)(ansi reset)"
print -e $"(ansi light_red)($output)(ansi reset)"
null
} else {
nu-complete winget parse table $lines | select name id version source
$lines
}
}
}
Expand Down Expand Up @@ -373,19 +319,20 @@ export def "winget list" [
)

let output = ^winget ...$params
# remove loading symbols at start of output
| str replace -r r#'^[^\w]*'# ""
if $help or $raw {
$output
} else {
let lines = ($output | lines)
let lines = ($output | detect columns --guess) | rename name id version available source
if ($lines | length) == 1 {
$"(ansi light_red)($lines | first)(ansi reset)"
print -e $"(ansi light_red)($output)(ansi reset)"
null
} else {
let truncated = $lines | last | $in == "<additional entries truncated due to result limit>"
nu-complete winget parse table (if $truncated { $lines | drop } else { $lines })
| reject match
# Because of a bug: https://github.com/microsoft/winget-cli/issues/4236
# we need to filter it with the "where" command
| if ($source | is-not-empty) { $in | where source == $source } else { $in }
$lines
# Because of a bug: https://github.com/microsoft/winget-cli/issues/4236
# we need to filter it with the "where" command
| if ($source | is-not-empty) { $in | where source == $source } else { $in }
}
}
}
Expand Down