functions/ui/Show-UpDownMenu.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
function Show-UpDownMenu { [Cmdletbinding()] param ( [Parameter(Mandatory = $true)] [object[]]$Options, [Parameter(Mandatory = $false)] [object[]]$ExpandProperty, [Parameter(Mandatory = $false)] [object[]]$Title ) begin { $titlePos = Get-CursorPosition Write-HostColored "$Title (Pres arrow keys and {{Enter}} to choose or {{Escape}} to exit):" -ForegroundColor Yellow -HighlightColor Magenta $i = 0 $key = $null $startPos = Get-CursorPosition } process { while ($null -eq $key -or ($key.Key -ne 'Enter' -and $key.Key -ne 'Esc')) { Clear-Line Set-CursorPosition $startPos if ($null -ne $ExpandProperty) { Write-Host "> $($Options[$i].$ExpandProperty)" -NoNewline -BackgroundColor Black } else { Write-Host "> $($Options[$i])" -NoNewline -BackgroundColor Black } $key = [System.Console]::ReadKey() if ($key.Key -eq 'DownArrow') { if ($i -eq $Options.Length - 1) { $i = 0 } else { $i++ } } elseif ($key.Key -eq 'UpArrow') { if ($i -eq 0) { $i = $Options.Length - 1 } else { $i-- } } } if ($key.Key -eq 'Esc') { exit } Set-CursorPosition $titlePos Clear-Line Write-Host $Options[$i] } } |