使用Box字符创建控制台菜单

时间:2014-03-31 22:48:38

标签: powershell powershell-ise

我已经构建了一个菜单,但我希望将其格式化为:

使用框中的菜单标题和框中的选择选项。

我找到了http://en.wikipedia.org/wiki/Box-drawing_character

中的方框字符

╔═╗ ║║ ╚═╝

我想使用字符串的length属性来绘制水平框线。

4 个答案:

答案 0 :(得分:1)

要制作50个字符(例如)水平线,您可以这样做:

PS> "═" * 50
══════════════════════════════════════════════════

答案 1 :(得分:0)

这不会回答有关长度属性的任何内容。但是,看起来示例截图可能只是使用单个字符和Write-Host。 Write-Host "╔══════════╗" Write-Host "║ Box Menu ║" Write-Host "╚══════════╝"

答案 2 :(得分:0)

我意识到这很老了,但似乎有很多志同道合的人想要比System.Management.Automation.Host.ChoiceDescription更好的菜单选项。我一直在寻找类似的东西,发现this closed question启发了我写一个菜单系统,该系统用适合于最长选择长度或菜单标题的框包围选择:

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

该脚本仅适用于Powershell控制台,不适用于ISE。使用箭头键将突出显示反向视频中的选择。击中enter将返回它。

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')

还有一个multi-select version on GitHub,允许使用空格键选择多个选项。

答案 3 :(得分:0)

这可以解决问题

# ----------------------------------------------------------------------------------
#
# 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
}

并给出输出

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