Я пытаюсь обновить файл простого пакетного меню до гибридного пакетного / PowerShell. Я сталкивался с этим вопросом - наброски пакетного меню и дизайн - который имеет приятный интерфейс 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) }
}
}