Public/Write-CliMenu.ps1
function Write-CliMenu { param ( [object]$MenuHeader, [string] $MenuTitle, [array] $MenuItems, [switch] $Multiselect, [switch] $ReturnIndex = $False ) $Key = 0 $POS = 0 $CurrentSelection = @() if ($MenuItems.Count -gt 0) { try { [console]::CursorVisible = $False Write-CliMenuNewLineTop if ($MenuHeader -gt $null) { Write-CliMenuHeader -InputObject $MenuHeader } if ($MenuTitle -gt $null) { Write-CliMenuTitle -MenuTitle $MenuTitle } Invoke-DrawCliMenu -MenuItems $MenuItems -POS $POS -CurrentSelection $CurrentSelection -Multiselect $Multiselect while ($Key -ne 13 -and $Key -ne 27) { $Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode # UP ARROW GOES TO NEXT OPTION UP if ($Key -eq 38) { $POS-- } # DOWN ARROW GOES TO NEXT OPTION DOWN if ($Key -eq 40) { $POS++ } # DOWN ARROW ON LAST ITEM GOES BACK TO FIRST ITEM if ($POS -eq $MenuItems.Count) { $POS = 0 } # UP ARROW ON FIRST ITEM GOES DOWN TO LAST ITEM if ($POS -lt 0) { $POS = $MenuItems.Count - 1 } # SPACEBAR SELECTS AN OPTION IN MULTISELECT MENU if ($Key -eq 32) { $CurrentSelection = Set-MultiSelect -POS $POS -CurrentSelection $CurrentSelection } # ESC QUITS MENU if ($Key -eq 27) { $POS = $null } if ($Key -ne 27) { try { $NewPOS = [System.Console]::CursorTop - $MenuItems.Count [System.Console]::SetCursorPosition(0, $NewPOS) } catch { Clear-Host } Invoke-DrawCliMenu -MenuItems $MenuItems -POS $POS -Multiselect $Multiselect -CurrentSelection $CurrentSelection } } } finally { try { [System.Console]::SetCursorPosition(0, $NewPOS + $MenuItems.Count) } catch { Clear-Host } [console]::CursorVisible = $True } } else { $POS = $null } # Return selected item. if ($ReturnIndex -eq $False -and $null -ne $POS) { if ($Multiselect) { Return $MenuItems[$CurrentSelection] } else { Return $MenuItems[$POS] } } else { if ($Multiselect) { Return $CurrentSelection } else { Return $POS } } } |