gpt4 book ai didi

powershell - 选项去哪儿了?

转载 作者:行者123 更新时间:2023-12-03 00:32:03 25 4
gpt4 key购买 nike

我正在尝试将简单的批处理菜单文件升级为混合批处理/ powershell混合。我遇到了这个问题-batch menu outlines and design-它具有一个不错的Powershell界面。但是,我不知道如何正确设置菜单选项!你能说明我吗?

代码如下:

<# : Batch portion # Original Code by rojo@StackOverflow 
REM https://stackoverflow.com/users/1683264/rojo)
@echo off & setlocal enabledelayedexpansion

set "menu[0]=Open GenNBO Helper"
set "menu[1]=Open Jmol@NBO Viewer Helper"
set "menu[2]=Open Jmol *"
set "menu[3]=Start NBO"
set "menu[4]=Start Multiwfn"
set "menu[5]=EXIT"

set "default=5"

powershell -noprofile "iex (gc \"%~f0\" | out-string)"
echo Option Selected: !menu[%ERRORLEVEL%]!.

goto :EOF

:GENNBO
CLS
ECHO Starting GenNbo Helper v1.33
cd L:\GennboHelper\
start GennboHelper_1.33.jar
GOTO MENU
#JMOLNBO
:JMOLNBO
CLS
ECHO Starting JmolNbo Viewer Helper v2.1
cd L:\JmolNboVHelper2.1\
start JmolNbo21W.jar
GOTO MENU
#JMOL
:JMOL
CLS
ECHO Starting Jmol v14.30.1
cd L:\Jmol-14.30.1-binary\jmol-14.30.1
start Jmol.jar
GOTO MENU
#NBO
:NBO
CLS
ECHO Starting NBO 6.0 Environment CMD
cd L:\nbo6\
start C:\Windows\System32\cmd.exe /c L:\Portland\PGI\win64\19.10\pgi_dos.bat
GOTO MENU
#MWFN
:MWFN
CLS
ECHO Starting Multiwfn
L:\Multiwfn
start L:\Multiwfn\Multiwfn.exe
GOTO MENU

: end batch / begin PowerShell hybrid chimera #>

$menutitle = "=== MENU ==="
$menuprompt = "Use the arrow keys. Hit Enter to select."

$maxlen = $menuprompt.length + 6
$menu = gci env: | ?{ $_.Name -match "^menu\[\d+\]$" } | %{
$_.Value.trim()
$len = $_.Value.trim().Length + 6
if ($len -gt $maxlen) { $maxlen = $len }
}
[int]$selection = $env:default
$h = $Host.UI.RawUI.WindowSize.Height
$w = $Host.UI.RawUI.WindowSize.Width
$xpos = [math]::floor(($w - ($maxlen + 5)) / 2)
$ypos = [math]::floor(($h - ($menu.Length + 4)) / 3)

$offY = [console]::WindowTop;
$rect = New-Object Management.Automation.Host.Rectangle `
0,$offY,($w - 1),($offY+$ypos+$menu.length+4)
$buffer = $Host.UI.RawUI.GetBufferContents($rect)

function destroy {
$coords = New-Object Management.Automation.Host.Coordinates 0,$offY
$Host.UI.RawUI.SetBufferContents($coords,$buffer)
}

function getKey {
while (-not ((37..40 + 13 + 48..(47 + $menu.length)) -contains $x)) {
$x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode
}
$x
}

# goo.gl/IAmdR6
function WriteTo-Pos ([string]$str, [int]$x = 0, [int]$y = 0,
[string]$bgc = [console]::BackgroundColor, [string]$fgc = [Console]::ForegroundColor) {
if($x -ge 0 -and $y -ge 0 -and $x -le [Console]::WindowWidth -and
$y -le [Console]::WindowHeight) {
$saveY = [console]::CursorTop
$offY = [console]::WindowTop
[console]::setcursorposition($x,$offY+$y)
Write-Host $str -b $bgc -f $fgc -nonewline
[console]::setcursorposition(0,$saveY)
}
}

function center([string]$what) {
$what = " $what "
$lpad = " " * [math]::max([math]::floor(($maxlen - $what.length) / 2), 0)
$rpad = " " * [math]::max(($maxlen - $what.length - $lpad.length), 0)
WriteTo-Pos "$lpad $what $rpad" $xpos $line blue yellow
}

function menu {
$line = $ypos
center $menutitle
$line++
center " "
$line++

for ($i=0; $item = $menu[$i]; $i++) {
# write-host $xpad -nonewline
$rtpad = " " * ($maxlen - $item.length)
if ($i -eq $selection) {
WriteTo-Pos " > $item <$rtpad" $xpos ($line++) yellow blue
} else {
WriteTo-Pos " $i`: $item $rtpad" $xpos ($line++) blue yellow
}
}
center " "
$line++
center $menuprompt
1
}

while (menu) {

[int]$key = getKey

switch ($key) {

37 {} # left or up
38 { if ($selection) { $selection-- }; break }

39 {} # right or down
40 { if ($selection -lt ($menu.length - 1)) { $selection++ }; break }

# number or enter
default { if ($key -gt 13) {$selection = $key - 48}; destroy; exit($selection) }
}
}

最佳答案

powershell ...行之后,您只需回显选定的菜单点。您需要将其转换为gotoif "!menu[%errorlevel%]!"=="Open GenNBO Helper" goto :GENNBO等(每个菜单项一行)。

我建议改为更改标签,并仅使用一个goto:

powershell ...
echo Option Selected: !menu[%ERRORLEVEL%]!.
goto :menu%errorlevel%
goto :eof

:menu0
REM formerly :GENNBO
CLS
...
goto :menu

:menu1
REM formerly :JMOLNBO
...
etc.

关于powershell - 选项去哪儿了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59862699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com