2

I've built a menu, but I want to format it to look like this:

With the menu title within a box and the selection choices in a box.

I found the box characters from http://en.wikipedia.org/wiki/Box-drawing_character

╔═╗ ║ ║ ╚═╝

I want to use the length property of strings to draw the horizontal box lines.

Musa
  • 553
  • 1
  • 7
  • 19
  • wow...is there some reason not to just create a GUI? – EBGreen Mar 31 '14 at 22:59
  • 1
    Sarcastic though it may be, TessellatingHeckler has an excellent point. You haven't actually asked a question. What is it you need help with, what have you tried, and where are you having trouble with your attempts? – TheMadTechnician Mar 31 '14 at 23:35

4 Answers4

2

I realize this is old, but it seems to have a lot of traffic from like-minded individuals who want a better menu option than System.Management.Automation.Host.ChoiceDescription. I was looking for something similar and found this closed question that inspired me to write a menu system that surrounds the choices with a box adapted to the longest choice length or menu title:

┌Choose an option┐
│first           │
│second option   │
│third           │
└────────────────┘

The script only works in Powershell console, not ISE. Using the arrow keys will highlight a selection in reverse video. Hitting enter will return it.

function Show-SimpleMenu ([array]$MenuOptions, [string]$Title ='Choose an option'){
    $maxLength = ($MenuOptions | Measure-Object -Maximum -Property Length).Maximum #get longest string length
    If($maxLength -lt $Title.Length){$maxLength = $Title.Length}
    $highlighted = 0 
    $MenuTop = [Console]::CursorTop
    Do{
        [Console]::CursorTop = $MenuTop
        Write-Host "┌$($Title.PadRight($maxLength,'─'))┐" 
        for ($i = 0; $i -lt $MenuOptions.Length;$i++) {
            Write-Host "│" -NoNewLine
            if ($i -eq $highlighted) {
                Write-Host "$(([string]$MenuOptions[$i]).PadRight($maxLength,' '))" -fore $host.UI.RawUI.BackgroundColor -back $host.UI.RawUI.ForegroundColor -NoNewline
            } else {
                Write-Host "$(([string]$MenuOptions[$i]).PadRight($maxLength,' '))" -fore $host.UI.RawUI.ForegroundColor -back $host.UI.RawUI.BackgroundColor -NoNewline
            }
            Write-Host "│"
        }
        Write-Host "└$('─' * ($maxLength))┘"
        $keycode = [Console]::ReadKey($true)
        If ($keyCode.Key -eq [ConsoleKey]::UpArrow -and $highlighted -gt 0 ) {$highlighted--}
        If ($keycode.Key -eq [ConsoleKey]::DownArrow -and $highlighted -lt $MenuOptions.Length - 1) {$highlighted++}
    }While($keyCode.Key -ne [ConsoleKey]::Enter -and $keycode.Key -ne [ConsoleKey]::Escape )
    If($keyCode.Key -eq [ConsoleKey]::Enter){ $MenuOptions[$highlighted] }
}

Show-SimpleMenu @('first','second option','third')

There is also a multi-select version on GitHub that allows choosing multiple options with the space bar.

Rich Moss
  • 2,195
  • 1
  • 13
  • 18
1

To make a 50-character (for example) horizontal line, you could do it like this:

PS> "═" * 50
══════════════════════════════════════════════════
dan-gph
  • 16,301
  • 12
  • 61
  • 79
1

This doesn't answer anything regarding length property. But, it looks like the example screenshot might just be using single characters and Write-Host as such. Write-Host "╔══════════╗" Write-Host "║ Box Menu ║" Write-Host "╚══════════╝"

harvey263
  • 76
  • 1
  • 4
1

This somewhat do the trick

# ----------------------------------------------------------------------------------
#
# Script functions
#
# ----------------------------------------------------------------------------------
function MakeTop
{
    $string = "╔═"
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + "═"
    }
    $string = $string + "═╗"

    return $string
}
function MakeMiddel
{
    $string = "╠═"
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + "═"
    }
    $string = $string + "═╣"

    return $string
}
function MakeButtom
{
    $string = "╚═"
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + "═"
    }
    $string = $string + "═╝"

    return $string
}

function MakeSpaces
{
    $string = "║ "
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + " "
    }
    $string = $string + " ║"
    return $string
}

function CenterText
{
    param($Message)

    $string = "║ "

    for($i = 0; $i -lt (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 4; $i++)
    {
        $string = $string + " "
    }

    $string = $string + $Message

    for($i = 0; $i -lt ($Host.UI.RawUI.BufferSize.Width - ((([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 2 + $Message.Length)) - 2; $i++)
    {
        $string = $string + " "
    }

    $string = $string + " ║"
    return $string

}


$MakeTop = MakeTop
$MakeMiddel = MakeMiddel
$MakeButtom = MakeButtom
$MakeSpaces = MakeSpaces



# ----------------------------------------------------------------------------------
#
# Run
#
# ----------------------------------------------------------------------------------
$MakeTop
CenterText "Changing TCP/IP Settings"

$MakeMiddel

$MakeSpaces
CenterText "1. Change Settings to DHCP"
CenterText "2. Change Settings to STATIC"
CenterText "3. Exit Program"
$MakeSpaces
$MakeButtom

$UserInput = Read-Host "Please make a selection [1-3]"


# ----------------------------------------------------------------------------------
#
# Handle input
#
# ----------------------------------------------------------------------------------
if($UserInput -eq "1")
{
    Write-Host "Setting TCP/IP Settings to DHCP"
    Read-Host
}
elseif($UserInput -eq "2")
{
    Write-Host "Setting TCP/IP Settings to STATIC"
    Read-Host
}

And give the output

╔══════════════════════════════════════════════════════════════════════════════════════════╗
║                               Changing TCP/IP Settings                                   ║
╠══════════════════════════════════════════════════════════════════════════════════════════╣
║                                                                                          ║
║                              1. Change Settings to DHCP                                  ║
║                             2. Change Settings to STATIC                                 ║
║                                   3. Exit Program                                        ║
║                                                                                          ║
╚══════════════════════════════════════════════════════════════════════════════════════════╝
Please make a selection [1-3]: 
Mikki Sørensen
  • 101
  • 1
  • 9