实现颜色批量切换开关

问题描述 投票:0回答:2

所以我有一个带有选择开关的简单菜单,只需在键盘上选择“1”或“2”即可在“是”和“否”之间切换,效果很好,但我正在尝试实现一个颜色功能来进行切换输出显示为红色(否)或绿色(是),这是我的菜单代码。

@echo off
setlocal enabledelayedexpansion

set "option1=no"
set "option2=no"
set "startScript=0"
:menu
cls
echo]
echo]
echo Select an option:
echo 1 - Toggle Option 1 [!option1!]
echo 2 - Toggle Option 2 [!option2!]
echo 3 - Start Script
echo 4 - Exit

choice /c 1234 /n /t 1000 /d 4
set "choice=%errorlevel%"

if %choice%==1 (
    if "!option1!"=="yes" (
        set "option1=no"
    ) else (
        set "option1=yes"
    )
    goto menu
) else if %choice%==2 (
    if "!option2!"=="yes" (
        set "option2=no"
    ) else (
        set "option2=yes"
    )
    goto menu
) else if %choice%==3 (
    set "startScript=1"
    cls
    echo Starting the script with the selected options...
    rem Add your script code here

    if "!option1!"=="yes" (
        call :function1
    )

    if "!option2!"=="yes" (
        call :function2
    )

    echo Done
    pause
    set "startScript=0"  REM Reset the startScript variable
    goto menu
) else if %choice%==4 (
    exit /b
)

goto menu

:function1
echo Function 1 is running...
rem Add your code for function 1 here
goto :eof

:function2
echo Function 2 is running...
rem Add your code for function 2 here
goto :eof
batch-file colors switch-statement
2个回答
0
投票

对于将来遇到此问题的任何人,这是我的解决方案:

@echo off
setlocal enabledelayedexpansion

set "option1=no"
set "option2=no"
set "option3=no"
set "startScript=0"

:menu
cls
echo]
echo]
if "!option1!"=="yes" (
call :c 07 "    Option1" & call :c 02 " [!option1!] " /n
) else ( 
if "!option1!"=="no" (
call :c 07 "    Option1" & call :c 04 " [!option1!] " /n
)
 )
echo     test [!option2!]
echo     test [!option3!]
echo     Start Script
echo     Exit

choice /c 1234 /n /t 1000 /d 4
set "choice=%errorlevel%"

if %choice%==1 (
    if "!option1!"=="yes" (
    set "option1=no" 
    if "!option1!"=="yes" (
    call :c 02 "!option1!" (
        
)
    ) else (
        set "option1=yes"
    )
    goto menu
) else if %choice%==2 (
    if "!option2!"=="yes" (
        set "option2=no"
    ) else (
        set "option2=yes"
    )
    goto menu
) else if %choice%==3 (
    if "!option3!"=="yes" (
        set "option3=no"
    ) else (
        set "option3=yes"
    )
    goto menu
) else if %choice%==4 (
    set "startScript=1"
    cls
    echo]
    echo Starting the script with the selected options...
    ping localhost -n 3 >nul

    if "!option1!"=="yes" (
        call :function1
    )

    if "!option2!"=="yes" (
        call :function2
    )

    if "!option3!"=="yes" (
        call :function3
    )

    echo Done
    pause
    set "startScript=0"  REM Reset the startScript variable
    goto menu
) else if %choice%==5 (
    exit /b
)

goto menu

:function1
cls
Tree
ping localhost -n 2 >nul
goto :eof

:function2
cls
dir/s
ping localhost -n 2 >nul
goto :eof

:function3
cls
echo Hello
pause >nul
goto :eof





:c
setlocal enableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:colorPrint Color  Str  [/n]
setlocal
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^

^") do (
  set "s=!s:\=%%~n\%%~n!"
  set "s=!s:/=%%~n/%%~n!"
  set "s=!s::=%%~n:%%~n!"
)
for /f delims^=^ eol^= %%s in ("!s!") do (
  if "!" equ "" setlocal disableDelayedExpansion
  if %%s==\ (
    findstr /a:%~1 "." "\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%"
  ) else if %%s==/ (
    findstr /a:%~1 "." "/.\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
  ) else (
    >colorPrint.txt (echo %%s\..\')
    findstr /a:%~1 /f:colorPrint.txt "."
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
  )
)
if /i "%~3"=="/n" echo(
popd
exit /b


:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b


:cleanupColorPrint
2>nul del "%temp%\'"
2>nul del "%temp%\colorPrint.txt"
>nul subst ': /d
exit /b

0
投票

这就是我会做的方式:

@echo off
setlocal enabledelayedexpansion

set "option[1]=no"
set "option[2]=no"
set "option[3]=no"
set /A "lastOpt=3, lastMenu=lastOpt+2"
set "startScript=0"

set "options="
for /L %%i in (1,1,%lastMenu%) do set "options=!options!%%i"

set "toggle[no]=yes"
set "toggle[yes]=no"

set "color[no]=04"
set "color[yes]=02"

:menu
cls
echo]
echo]
echo Select an option:
for /L %%i in (1,1,%lastOpt%) do (
   for %%a in (!option[%%i]!) do (
      call :c 07 "    Option%%i" & call :c !color[%%a]! " [%%a] " /n
   )
)
echo     Start Script
echo     Exit

choice /c %options% /n /t 1000 /d %lastMenu%
set "choice=%errorlevel%"

if %choice% leq %lastOpt% (
    for %%a in (!option[%choice%]!) do set "option[%choice%]=!toggle[%%a]!"
    goto menu
) else if %choice% lss %lastMenu% (
    set "startScript=1"
    cls
    echo Starting the script with the selected options...
    rem Add your script code below each :function# label

    for /L %%i in (1,1,%lastOpt%) do (
        if "!option[%%i]!" == "yes" (
            call :function%%i
        )
    )

    echo Done
    pause
    set "startScript=0"  & REM Reset the startScript variable
    goto menu
)
exit /B

更多详细信息,请访问:arrays-linked-lists-and-other-data-structs-in-cmd-exe-batch-script

© www.soinside.com 2019 - 2024. All rights reserved.