Skip to content

Commit 766009a

Browse files
authored
Add winget upgrades command (#1179)
`winget upgrade` has two modes: * No parameters (at all) lists available upgrades * Any parameter attempts to match and upgrade a package Much like `winget list`, `winget upgrade` prints a spinner, then a table, and then additionally a footer. The `winget list` completions handle output through `detect columns --guess` and return structured data. Presumably because `detect columns --guess` does not work on `winget upgrade`, no output handling was done until now. 1. Split off trimLoadingSymbol as a common function between two, now three, commands 2. Implement a separate command `winget upgrades` instead of handling `winget upgrade` because of overloaded behavior complicating implementation Now, `winget upgrades` can be used to get structured data of available upgrades, while `winget upgrade` behaves like before, without parameters has text output, and with parameters upgrades packages. While introducing a separate command is not strictly completions [for external commands], this one is very close to the completions, logic, and use cases. Keeping it close will help discovery and future development which may integrate returning structured data into the "normal" completions. --- I was not able to test for “no updates” output. --- ```nushell winget upgrades # => ╭───┬──────────────────────────────────┬─────────────────────┬───────────────────────────────┬───────────────────────────────┬────────╮ # => │ # │ name │ id │ version │ available │ source │ # => ├───┼──────────────────────────────────┼─────────────────────┼───────────────────────────────┼───────────────────────────────┼────────┤ # => │ 0 │ Go Programming Language amd64 g… │ GoLang.Go │ 1.25.1 │ 1.25.2 │ winget │ # => ╰───┴──────────────────────────────────┴─────────────────────┴───────────────────────────────┴───────────────────────────────┴────────╯ ```
1 parent cd81d16 commit 766009a

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

custom-completions/winget/winget-completions.nu

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def "nu-complete winget flagify" [name: string, value: any, --short(-s)] {
3636
}
3737
}
3838

39+
def "nu-complete winget trimLoadingSymbol" [] {
40+
str replace -r r#'^[^\w]*'# ""
41+
}
42+
3943
def "nu-complete winget uninstall package id" [] {
4044
^winget export -s winget -o __winget-temp__.json | ignore
4145
let results = (open __winget-temp__.json | get Sources.Packages | first | get PackageIdentifier)
@@ -259,8 +263,7 @@ export def "winget search" [
259263
)
260264

261265
let output = ^winget ...$params
262-
# remove loading symbols at start of output
263-
| str replace -r r#'^[^\w]*'# ""
266+
| nu-complete winget trimLoadingSymbol
264267
if $raw or $help {
265268
$output
266269
} else {
@@ -319,8 +322,7 @@ export def "winget list" [
319322
)
320323

321324
let output = ^winget ...$params
322-
# remove loading symbols at start of output
323-
| str replace -r r#'^[^\w]*'# ""
325+
| nu-complete winget trimLoadingSymbol
324326
if $help or $raw {
325327
$output
326328
} else {
@@ -338,6 +340,35 @@ export def "winget list" [
338340
}
339341
export alias "winget ls" = winget list
340342

343+
def "winget upgrades" [] {
344+
let output = ^winget upgrade | nu-complete winget trimLoadingSymbol
345+
346+
# Do nothing when no upgrades available
347+
if ( ($output | str starts-with 'No') or ($output | str starts-with '0') ) { return }
348+
349+
let lines = $output | lines
350+
let head = $lines | first
351+
let rest = $lines | skip 2
352+
353+
let colnames = [ Name Id Version Available Source ]
354+
# We must be unicode aware in determining and using index; winget uses `…` elippses to hide overflow
355+
let cols = $colnames | each {|col| $head | {name: $col i: ($head | str index-of $col --grapheme-clusters) } }
356+
357+
let dirty = $rest | each {|line|
358+
let chars = $line | split chars
359+
{
360+
name: ( $chars | slice ($cols.0.i)..($cols.1.i - 1) | str join | str trim )
361+
id: ( $chars | slice ($cols.1.i)..($cols.2.i - 1) | str join | str trim )
362+
version: ( $chars | slice ($cols.2.i)..($cols.3.i - 1) | str join | str trim )
363+
available: ( $chars | slice ($cols.3.i)..($cols.4.i - 1) | str join | str trim )
364+
source: ( $chars | slice ($cols.4.i).. | str join | str trim )
365+
}
366+
}
367+
# Reject footer lines, in a best effort approach, because there is no clear separator or definitely identifiable form change.
368+
# We expect `x upgrades available.` to follow the table. Then maybe `x package(s) have version numbers that cannot be determined. Use --include-unknown to see all results.`
369+
return ($dirty | take until {|x| $x.source == '' })
370+
}
371+
341372
# Upgrades the given package
342373
export extern "winget upgrade" [
343374
query?: string,

0 commit comments

Comments
 (0)