Skip to content

Instantly share code, notes, and snippets.

@lenicyl
Forked from hapylestat/text_menu.ps1
Last active December 19, 2021 08:53
Show Gist options
  • Save lenicyl/f64bd9959a175329e7c69392c41773ee to your computer and use it in GitHub Desktop.
Save lenicyl/f64bd9959a175329e7c69392c41773ee to your computer and use it in GitHub Desktop.
Clean powershell based menu with arrow navigation
<#
This is based of a fork of Jakob Bindslet's powershell menu, made by @hapylestat on github the links to both the fork, and the original you will find below.
I have forked it to add 1 line which just hides the cursor to make the menu look cleaner
Links :
Original Menu script : https://mspowershell.blogspot.com/2009/02/cli-menu-in-powershell.html?m=1
Fork : https://gist.github.com/hapylestat/b940d13b7d272fb6105a1146ddcd4e2a
#>
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 "> $oldMenuPos. $($menuItems[$oldMenuPos])" # -fore $fcolor -back $bcolor -NoNewLine
moveCursor $menuNewPos
Write-Host "> $menuPosition. $($menuItems[$menuPosition])"-ForegroundColor Green # -fore $bcolor -back $fcolor -NoNewLine
moveCursor $currPos
}
function DrawMenu { param ([array]$menuItems, $menuPosition, $menuTitel)
$fcolor = $host.UI.RawUI.ForegroundColor
$bcolor = $host.UI.RawUI.BackgroundColor
# Hides Cursor
[System.Console]::CursorVisible = $false
Write-Host " $menuTitel " -fore $fcolor -back $bcolor
Write-Host ""
for ($i = 0; $i -le $menuItems.length;$i++) {
if ($i -eq $menuPosition) {
Write-Host "> $i. $($menuItems[$i])" -NoNewline -ForegroundColor Green # -fore $bcolor -back $fcolor
Write-Host ""
} 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
# Unhides cursor
[System.Console]::CursorVisible = $true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment