Skip to content

Instantly share code, notes, and snippets.

@mjnbrn
Forked from hapylestat/text_menu.ps1
Last active August 22, 2021 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjnbrn/cc115b8fd09e2fdf006e1686ab4445e4 to your computer and use it in GitHub Desktop.
Save mjnbrn/cc115b8fd09e2fdf006e1686ab4445e4 to your computer and use it in GitHub Desktop.
Powershell text based menu that makes you punch your selection in
# Resize Function
# https://blogs.msmvps.com/russel/2017/04/28/resizing-the-powershell-console/
# Thank Charlie Russel!!!
function ResizeMeSenpai {
[CmdletBinding()] Param(
[Parameter(Mandatory = $False, Position = 0)]
[int] $Height = 40,
[Parameter(Mandatory = $False, Position = 1)]
[int] $Width = 120 )
$Console = $host.ui.rawui
$Buffer = $Console.BufferSize
$ConSize = $Console.WindowSize
If ($Buffer.Width -gt $Width ) {
# If the Buffer is wider than the new console setting, first reduce the buffer, then do the resize
$ConSize.Width = $Width
$Console.WindowSize = $ConSize
}
$Buffer.Width = $Width
$ConSize.Width = $Width
$Buffer.Height = 3000
$Console.BufferSize = $Buffer
$ConSize = $Console.WindowSize
$ConSize.Width = $Width
$ConSize.Height = $Height
$Console.WindowSize = $ConSize
}
# Below is basically https://gist.github.com/hapylestat/b940d13b7d272fb6105a1146ddcd4e2a
# Edits to DrawMenu,
# Give me multiple columns
#
function DrawMenu { param ([array]$menuItems, $menuPosition, $menuTitle, $Columns=1, $BiggestBoi)
$menuwidth = $menuTitle.length + 4
Write-Host "`t" -NoNewLine; Write-Host ("=" * $menuwidth)
Write-Host "`t" -NoNewLine; Write-Host " $menuTitle "
Write-Host "`t" -NoNewLine; Write-Host ("=" * $menuwidth)
Write-Host ""
$TheBeholder = ""
$CurCol = 0
$TheBorder = $BiggestBoi
for ($i = 0; $i -le $menuItems.length;$i++) {
if($CurCol -le $Columns){
if ($menuItems[$i]) {
$TheBeholder += "($i) $($menuItems[$i])".PadRight($TheBorder)
}
}
if ($CurCol -eq $Columns){
Write-Output "`t$($TheBeholder)"
$TheBeholder = ""
$CurCol = 0
}
else {$CurCol++}
}
# leading new line
Write-Host ""
}
# This one is basically gutted, resizes window based on inputs
function Menu { param ([array]$menuItems, $menuTitle = "MENU")
# We need to parse our list and find the longest entry
$TheLongest = ($menuItems | % {$_.length } | Measure-Object -Maximum).Maximum
ResizeMeSenpai -Height ($host.ui.RawUI.MaxPhysicalWindowSize.Height / 2) -Width ($TheLongest*4)
DrawMenu $menuItems $pos $menuTitle 2 $TheLongest
Write-Output $menuitems[(read-host "gimme gimme")]
}
# Some lorum For your Ipsum bud
$bad = ", consectetur adipisc", "finibus dui non, faucibu", "ceratsdf sdf", "retium", "risus pulvinar d", "etsdfsdf", "diam dapibu", "cus", "us ornare vel pulvin", "e magna faucibus posuere sed", "alesuada pell", "tis scelerisque eu ", "egestas faucibus at ", "gula commodo semper eget ve", "ellus ullamcorpe", "met elit vehicula, sed elementum metus"
Menu $bad "Select menu item"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment