Created
February 26, 2024 11:41
-
-
Save byBretema/c0a74c76afbd86d0a512af342670abfd to your computer and use it in GitHub Desktop.
Interactive WinGet search, just type the index of the package to install
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
function winget_is { | |
param ($inName) | |
$lines = winget search $inName | |
# Gather indices | |
$nameIdx = 0 | |
$nameLen = -1 | |
$idIdx = -1 | |
$idLen = -1 | |
$verIdx = -1 | |
$verLen = -1 | |
$firstContentRow = -1 | |
for ($lIdx = 0; $lIdx -lt $lines.Length; $lIdx++) { | |
$l = $lines[$lIdx] | |
if ($l.StartsWith("Name")) { | |
$firstContentRow = $lIdx + 2; | |
for ($c = 0; $c -lt $l.Length - 1; $c++) { | |
$s = "$($l[$c])$($l[$($c+1)])" | |
if ($s -eq "Id") { | |
$idIdx = $c | |
$nameLen = $idIdx - $nameIdx | |
} | |
elseif ($s -eq "Ve") { | |
$verIdx = $c | |
$idLen = $verIdx - $idIdx | |
} | |
elseif ($s -eq "Ma") { | |
$verLen = $c - $verIdx | |
} | |
} | |
} | |
} | |
# Validate indices | |
$idxs = @($nameLen, $idIdx, $idLen, $verIdx, $verLen) | |
$idxsOk = $true; | |
foreach ($idx in $idxs) { | |
$idxsOk = $idxsOk -and $($idx -gt -1) | |
} | |
# Extract info | |
$it0 = $firstContentRow | |
$it = $firstContentRow | |
$packages = New-Object System.Collections.Generic.List[System.Object] | |
if (($it -gt -1) -and ($it -lt $lines.Length - 1) -and $idxsOk) { | |
for (; $it -lt $lines.Length - 1; $it++) { | |
$n = $($it - $it0) | |
$nStr = "$n" | |
if ($n -lt 10) { $nStr = " " + $nStr } | |
if (($n -gt 9) -and $n -lt 100) { $nStr = " " + $nStr } | |
$pkgName = $lines[$it].Substring($nameIdx, $nameLen) | |
$pkgId = $lines[$it].Substring($idIdx, $idLen) | |
$pkgVer = $lines[$it].Substring($verIdx, $verLen) | |
Write-Output "$nStr) ID: $pkgId | Ver: $pkgVer | Name: $pkgName" | |
$packages.Add($pkgId.Trim()); | |
} | |
$toInstall = Read-Host ">> Enter index of package to install" | |
try { | |
$toInstallIdx = [int]$toInstall | |
} | |
catch { | |
Write-Output "Bad index '$toInstall'." | |
return; | |
} | |
$p = $packages[$toInstallIdx]; | |
Write-Output "!! Trying to install: '$p'" | |
winget install --disable-interactivity -h -e --id $p | |
} | |
else { | |
Write-Output "No package found matching input criteria: '$inName'" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment