Skip to content

Commit ae02260

Browse files
authored
Improve winget completion (#1171)
- Fix winget source refresh - Improve some implementations, now they do not depend on user locale (function `nu-complete winget parse table` is now uselless) My implementation of `winget list` may not provide the same result as before, feel free to delete the corresponding commit if that's a problem.
1 parent 92fa4b4 commit ae02260

File tree

1 file changed

+17
-70
lines changed

1 file changed

+17
-70
lines changed

custom-completions/winget/winget-completions.nu

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -80,63 +80,6 @@ def "nu-complete winget install id" [] {
8080
}
8181
}
8282

83-
def "nu-complete winget parse table" [lines: any] {
84-
let header = (
85-
$lines
86-
| first
87-
| 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*)?'
88-
| first
89-
)
90-
let lengths = {
91-
name: ($header.name | str length),
92-
id: ($header.id | str length),
93-
version: ($header.version | default "" | str length),
94-
match: ($header.match | default "" | str length),
95-
available: ($header.available | default "" | str length),
96-
source: ($header.source | default "" | str length)
97-
}
98-
$lines | skip 2 | each { |it|
99-
let it = ($it | split chars)
100-
101-
let version = if $lengths.version > 0 {
102-
(
103-
$it | skip ($lengths.name + $lengths.id)
104-
| first $lengths.version | str join | str trim
105-
)
106-
} else { "" }
107-
108-
let match = if $lengths.match > 0 {
109-
(
110-
$it | skip ($lengths.name + $lengths.id + $lengths.version)
111-
| first $lengths.match | str join | str trim
112-
)
113-
} else { "" }
114-
115-
let available = if $lengths.available > 0 {
116-
(
117-
$it | skip ($lengths.name + $lengths.id + $lengths.version + $lengths.match)
118-
| first $lengths.available | str join | str trim
119-
)
120-
} else { "" }
121-
122-
let source = if $lengths.source > 0 {
123-
(
124-
$it | skip ($lengths.name + $lengths.id + $lengths.version + $lengths.match + $lengths.available)
125-
| str join | str trim
126-
)
127-
} else { "" }
128-
129-
{
130-
name: ($it | first $lengths.name | str join | str trim),
131-
id: ($it | skip $lengths.name | first $lengths.id | str join | str trim),
132-
version: $version,
133-
match: $match,
134-
available: $available,
135-
source: $source
136-
}
137-
}
138-
}
139-
14083
export extern winget [
14184
--version(-v), # Display the version of the tool
14285
--info, # Display general info of the tool
@@ -251,7 +194,7 @@ export extern "winget source update" [
251194
--name(-n): string, # Name of the source
252195
--help(-?) # Display the help for this command
253196
]
254-
export alias "winget source refresh" = winet source update
197+
export alias "winget source refresh" = winget source update
255198

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

318-
let output = (^winget ...$params)
261+
let output = ^winget ...$params
262+
# remove loading symbols at start of output
263+
| str replace -r r#'^[^\w]*'# ""
319264
if $raw or $help {
320265
$output
321266
} else {
322-
let lines = ($output | lines)
267+
let lines = ($output | detect columns --guess) | rename name id version match source
323268
if ($lines | length) == 1 {
324-
$"(ansi light_red)($lines | first)(ansi reset)"
269+
print -e $"(ansi light_red)($output)(ansi reset)"
270+
null
325271
} else {
326-
nu-complete winget parse table $lines | select name id version source
272+
$lines
327273
}
328274
}
329275
}
@@ -373,19 +319,20 @@ export def "winget list" [
373319
)
374320

375321
let output = ^winget ...$params
322+
# remove loading symbols at start of output
323+
| str replace -r r#'^[^\w]*'# ""
376324
if $help or $raw {
377325
$output
378326
} else {
379-
let lines = ($output | lines)
327+
let lines = ($output | detect columns --guess) | rename name id version available source
380328
if ($lines | length) == 1 {
381-
$"(ansi light_red)($lines | first)(ansi reset)"
329+
print -e $"(ansi light_red)($output)(ansi reset)"
330+
null
382331
} else {
383-
let truncated = $lines | last | $in == "<additional entries truncated due to result limit>"
384-
nu-complete winget parse table (if $truncated { $lines | drop } else { $lines })
385-
| reject match
386-
# Because of a bug: https://github.com/microsoft/winget-cli/issues/4236
387-
# we need to filter it with the "where" command
388-
| if ($source | is-not-empty) { $in | where source == $source } else { $in }
332+
$lines
333+
# Because of a bug: https://github.com/microsoft/winget-cli/issues/4236
334+
# we need to filter it with the "where" command
335+
| if ($source | is-not-empty) { $in | where source == $source } else { $in }
389336
}
390337
}
391338
}

0 commit comments

Comments
 (0)