【发布时间】:2020-10-30 02:01:43
【问题描述】:
所以我拼凑了一个包含标题和可供选择的选项的菜单。选择一个项目是通过使用箭头键完成的。我希望突出显示选定的项目,以便您知道当前正在选择哪个项目。我是 PowerShell 的新手,并且熟悉如何使用 write-host 更改颜色,但在此示例中我一无所知。我认为我需要将颜色选项注入的有问题的行是以 $Width 开头的行。我真的很感激一些见解!这是我开始添加实际代码之前的最后一个小问题!
Function Create-Menu (){
Param(
[Parameter(Mandatory=$True)][String]$MenuTitle,
[Parameter(Mandatory=$True)][array]$MenuOptions
)
$MaxValue = $MenuOptions.count-1
$Selection = 0
$EnterPressed = $False
Clear-Host
While($EnterPressed -eq $False){
For ($i=0; $i -le $MaxValue; $i++){
$Width = if($Title){$Length = $Title.Length;$Length2 = $MenuOptions|%{$_.length}|Sort -Descending|Select -First 1;$Length2,$Length|Sort -Descending|Select -First 1}else{$MenuOptions|%{$_.length}|Sort -Descending|Select -First 1}
$Buffer = if(($Width*1.5) -gt 78){(78-$width)/2}else{$width/4}
if($Buffer -gt 6){$Buffer = 6}
$MaxWidth = $Buffer*2+$Width+$($MenuOptions.count).length
$Menu = @()
$Menu += "╔"+"═"*$maxwidth+"╗"
if($MenuTitle){
$Menu += "║"+" "*[Math]::Floor(($maxwidth-$MenuTitle.Length)/2)+$MenuTitle+" "*[Math]::Ceiling(($maxwidth-$MenuTitle.Length)/2)+"║"
$Menu += "╟"+"─"*$maxwidth+"╢"
}
For($i=1;$i -le $MenuOptions.count;$i++){
$Item = "$i`. "
$Menu += "║"+" "*$Buffer+$Item+$MenuOptions[$i-1]+" "*($MaxWidth-$Buffer-$Item.Length-$MenuOptions[$i-1].Length)+"║"
}
$Menu += "╚"+"═"*$maxwidth+"╝"
$menu
}
$KeyInput = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown").virtualkeycode
Switch($KeyInput){
13{
$EnterPressed = $True
$script:Selection = "$Selection"
Clear-Host
break
}
38{
If ($Selection -eq 0){
$Selection = $MaxValue
} Else {
$Selection -= 1
}
Clear-Host
break
}
40{
If ($Selection -eq $MaxValue){
$Selection = 0
} Else {
$Selection +=1
}
Clear-Host
break
}
Default{
Clear-Host
}
}
}
}
#MainMenu
Function MainMenu (){
Create-Menu -MenuTitle "Tool" -MenuOptions "Lookup","Prep","Tools","Settings","Cleanup and Exit"
If($script:Selection -eq 0) {Lookup}
If($script:Selection -eq 1) {PrepMenu}
If($script:Selection -eq 2) {ToolsMenu}
If($script:Selection -eq 3) {SettingsMenu}
If($script:Selection -eq 4) {CleanupAndExit}
}
MainMenu
pause
代码输出如下:
╔═════════════════════════╗
║ Tool ║
╟─────────────────────────╢
║ 1. Lookup ║
║ 2. Prep ║
║ 3. Tools ║
║ 4. Settings ║
║ 5. Cleanup and Exit ║
╚═════════════════════════╝
【问题讨论】:
-
你应该为powershell添加一个标签,否则没人会发现这个。
-
欣赏建议。第一次发帖..
-
请看我编辑的代码。虽然第一个答案有效,但第二个答案会产生更流畅的工作菜单而不会闪烁。如果您发现我的回答解决了您的问题,请点击左侧的 ✓ 图标,考虑accepting。这将帮助其他有类似问题的人更轻松地找到它。
标签: powershell colors menu items