functions/ui/Show-ChooseMenu.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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
function Show-ChooseMenu { [Cmdletbinding()] param ( [Parameter(Mandatory = $true)] [object[]]$Options, [Parameter(Mandatory = $false)] [object[]]$ExpandProperty, [Parameter(Mandatory = $false)] [object[]]$Title ) begin { if ($host.Name -ne 'ConsoleHost') { Write-Error "Cannot open ChooseMenu in current host($($host.Name))!" return } Write-HostColored "$Title (Pres {{arrow keys}} and hit {{Enter}} to choose; {{Escape}} to exit):" -ForegroundColor Yellow -HighlightColor Magenta $i = 0 $key = $null $startPos = Get-CursorPosition $currPos = $startPos } process { foreach ($option in $Options) { if ($null -ne $ExpandProperty) { Write-Host "[ ] $($option.$ExpandProperty)" -BackgroundColor Black } else { Write-Host "[ ] $option" -BackgroundColor Black } } $host.UI.RawUI.BufferSize $endPos = Get-CursorPosition $currPos.X++ $prevPos = $currPos Set-CursorPosition $currPos Write-Host "x" -NoNewline -BackgroundColor Black Set-CursorPosition $currPos while ($null -eq $key -or ($key.Key -ne 'Enter' -and $key.Key -ne 'Esc')) { $key = [System.Console]::ReadKey($false) if ($key.Key -eq 'DownArrow') { if ($i -eq $Options.Length - 1) { $i = 0 $currPos.Y = $startPos.Y } else { $currPos.Y = $currPos.Y + 1 $i++ } } elseif ($key.Key -eq 'UpArrow') { if ($i -eq 0) { $i = $Options.Length - 1 $currPos.Y = $endPos.Y - 1 } else { $currPos.Y = $currPos.Y - 1 $i-- } } Set-CursorPosition $prevPos Write-Host " " -NoNewline -BackgroundColor Black Set-CursorPosition $currPos Write-Host "x" -NoNewline -BackgroundColor Black Set-CursorPosition $currPos $prevPos = $currPos } if ($key.Key -eq 'Esc') { exit } Set-CursorPosition $endPos Write-Host $Options[$i] } } |