Skip to content

Instantly share code, notes, and snippets.

@hapylestat
Created July 21, 2021 18:38
Show Gist options
  • Save hapylestat/b940d13b7d272fb6105a1146ddcd4e2a to your computer and use it in GitHub Desktop.
Save hapylestat/b940d13b7d272fb6105a1146ddcd4e2a to your computer and use it in GitHub Desktop.
Powershell text based menu with arrows navigation
function moveCursor{ param($position)
$host.UI.RawUI.CursorPosition = $position
}
function RedrawMenuItems{
param ([array]$menuItems, $oldMenuPos=0, $menuPosition=0, $currPos)
# +1 comes from leading new line in the menu
$menuLen = $menuItems.Count + 1
$fcolor = $host.UI.RawUI.ForegroundColor
$bcolor = $host.UI.RawUI.BackgroundColor
$menuOldPos = New-Object System.Management.Automation.Host.Coordinates(0, ($currPos.Y - ($menuLen - $oldMenuPos)))
$menuNewPos = New-Object System.Management.Automation.Host.Coordinates(0, ($currPos.Y - ($menuLen - $menuPosition)))
moveCursor $menuOldPos
Write-Host "`t" -NoNewLine
Write-Host "$oldMenuPos. $($menuItems[$oldMenuPos])" -fore $fcolor -back $bcolor -NoNewLine
moveCursor $menuNewPos
Write-Host "`t" -NoNewLine
Write-Host "$menuPosition. $($menuItems[$menuPosition])" -fore $bcolor -back $fcolor -NoNewLine
moveCursor $currPos
}
function DrawMenu { param ([array]$menuItems, $menuPosition, $menuTitel)
$fcolor = $host.UI.RawUI.ForegroundColor
$bcolor = $host.UI.RawUI.BackgroundColor
$menuwidth = $menuTitel.length + 4
Write-Host "`t" -NoNewLine; Write-Host ("=" * $menuwidth) -fore $fcolor -back $bcolor
Write-Host "`t" -NoNewLine; Write-Host " $menuTitel " -fore $fcolor -back $bcolor
Write-Host "`t" -NoNewLine; Write-Host ("=" * $menuwidth) -fore $fcolor -back $bcolor
Write-Host ""
for ($i = 0; $i -le $menuItems.length;$i++) {
Write-Host "`t" -NoNewLine
if ($i -eq $menuPosition) {
Write-Host "$i. $($menuItems[$i])" -fore $bcolor -back $fcolor -NoNewline
Write-Host "" -fore $fcolor -back $bcolor
} else {
if ($($menuItems[$i])) {
Write-Host "$i. $($menuItems[$i])" -fore $fcolor -back $bcolor
}
}
}
# leading new line
Write-Host ""
}
function Menu { param ([array]$menuItems, $menuTitel = "MENU")
$vkeycode = 0
$pos = 0
$oldPos = 0
DrawMenu $menuItems $pos $menuTitel
$currPos=$host.UI.RawUI.CursorPosition
While ($vkeycode -ne 13) {
$press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
$vkeycode = $press.virtualkeycode
Write-host "$($press.character)" -NoNewLine
$oldPos=$pos;
If ($vkeycode -eq 38) {$pos--}
If ($vkeycode -eq 40) {$pos++}
if ($pos -lt 0) {$pos = 0}
if ($pos -ge $menuItems.length) {$pos = $menuItems.length -1}
RedrawMenuItems $menuItems $oldPos $pos $currPos
}
Write-Output $pos
}
$bad = "Item1","Item2"
$selection = Menu $bad "Select menu item"
Switch ($selection){
0 {
Write-Host "Menu item 0"
}
1 {
Write-Host "Menu item 1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment