Advertisement
T3RRYT3RR0R

Batch instant expert Colored menu

Oct 19th, 2020 (edited)
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.47 KB | None | 0 0
  1. ::: Multi-Use Menu :: Author - T3RRY ::  Version 4.2 :: October 2020 ::
  2. ::: %Menu% macro - accepts up to 36 options for selection as quoted Args in list form.
  3. ::: parameters to call functions with via a given menu can be supplied using Substring modification as exampled.
  4. ::: - Multiple selection of menu items achieved using loops.
  5. ::: Default is to assume a menu option is a label to be called. If the label doesn't exist error is suppressed and the option is added as a quoted
  6. ::: string to the #Sel Variable for assessment - builds a list of selected options [ that can be deselected ] when used in a loop.
  7. ::: Windows 10 users will experience faster display of menus thanks to VT codes
  8. ::: Users not running Windows 10 are compensated with more dynamic color output from the menu.
  9. ::: "&&" and "||" conditional operators are used heavily to control script flow, and are used with findstr as an alternative to
  10. ::: If Not "!Errorlevel!" == "0" (Command)Else (other command) statements.
  11. @Echo off & Goto :Main
  12. :::::::::::::::::::::::::: [* Balances Environment stack on script completion *]
  13. :End [Endlocal ^& Set "_End=Y" ^& Exit /B 0]
  14.  Color 07 & %CLOSE%
  15. :::::::::::::::::::::::::::::::::::::::::::::: Findstr based Colorprint function
  16. ::: Unsupported Chars: "<" ">" ":" "\" "/" "?" "&"
  17. :C_out [BG:a-f|0-9][FG:a-f|0-9] ["Quoted Strings" "to print"]
  18.  Set "Str_=%*" &  Set "_Str=" & For %%G in (!Str_!)Do Set "_Str=!_Str! %%~G"
  19.  Set "C_Out=%~1"
  20.  Set "_Str=!_Str:%1 =!" & For /F "Delims=" %%G in ("!_Str!")Do Set "_Str=%%~G"
  21.  For %%G in (!_Str!) Do Set ".Str=%%G"
  22.  PUSHD "%TEMP%\colorize" || (Echo/"%TEMP%\colorize" Could not be found & %CLOSE%)
  23.   If /I "!.Str!" == "Exit"  (Set "C_Out=04") Else If /I "!.Str!" == "Next" (Set "C_Out=02") Else If /I "!.Str!" == "Continue" (Set "C_Out=02") Else If /I "!.Str!" == "Back" (Set "C_Out=05") Else If /I "!.Str!" == "Return" (Set "C_Out=05")
  24.     <nul set /p ".=%DEL%" > " !_Str!"
  25.     findstr /v /a:!C_Out! /R "^$" " !_Str!" nul
  26.     del " !_Str!" > nul 2>&1
  27.     POPD
  28.     Echo/
  29. Exit /B 0
  30. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Key variable and Macro definition
  31. :main
  32. ::::::::::::::::::::: [ For readablities sake - %Menu% macro is built from the following ]:
  33. rem ::: Order of definition must be preserved.
  34. rem [* prepare default findstr color for non windows 10 users *]
  35. IF NOT EXIST "%TEMP%\colorize" md "%TEMP%\colorize"
  36.  For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
  37. rem [* default color args for Picked / Not Picked options. Overriden to Echo with Ascii Escape codes if Windows 10 *]
  38. rem [* Color /? for the full combination of colors - BG + FG colors must differ. [BG:a-f|0-9][FG:a-f|0-9] *]
  39.  Set "_nP=Call :C_Out 08"
  40.  Set "_P=Call :C_Out 06"
  41.  Set "_Win10="
  42.  Set "_End="
  43. rem [* Test if Windows 10 If true Overide defaults _nP and _P with Echo and VT codes for faster execution with windows 10 *]
  44.  wmic OS get OSArchitecture,caption | FIND "10" >nul && ((For /F %%a in ('echo prompt $E ^| cmd')do Set "\E=%%a")&Set "_Win10=_")
  45.  If Defined _Win10 (Set "_nP=Echo/%\E%[90m"& Set "_P=Echo/%\E%[33m")
  46. rem [* Menu supports 36 choices using _O array index with substring modification on _Cho var to index choice selection of Array Elements *]
  47.  Set "_Cho=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  48.  Set "DisplayArray=(Echo/!#Sel!| Findstr /LIC:"%%~G" > Nul 2> Nul && (%_P% {!_Cho:~%%z,1!} + %%~G) ) || (%_NP% [!_Cho:~%%z,1!] - %%~G)"
  49.  Set "#Sel=_Nil"
  50.  Set "ClearArray=(For /F "Tokens=1,2 Delims==" %%i in (' Set Opt[ 2^> Nul ')Do "Set %%i=")"
  51.  Set "ResetVars=(Set "#L=F" &  Set "OptL=" & Set "_O=0")"
  52.  Set "CLOSE=Endlocal & Set "_End=Y" & Exit /B 0"
  53.  Set "BuildArray=((If !_O! GTR 35 (Call :C_Out 04 "Maximum options [!_O!] Exceeded" & (Timeout /T 5 /NOBREAK) & %CLOSE%))&Set "OptL=!OptL!!_Cho:~%%z,1!"&Set "Opt[!_Cho:~%%z,1!]=%%~G")"
  54.  Set "MakeChoice=(For /F "Delims=" %%C in ('Choice /N /C:!OptL!')Do Call :!Opt[%%C]! "Param" 2> Nul || ((Echo/"!#Sel!"| Findstr /LIC:"!Opt[%%C]!" > Nul 2> Nul && (For /F "Delims=" %%r in ("!Opt[%%C]!")Do If Not "!#Sel!" == "" (Set "#Sel=!#Sel:"%%r"=!")Else (Set "#Sel=_Nil"))) || (Set "#Sel=!#Sel! "!Opt[%%C]!"")))"
  55.  Set "Return=For /L %%s in (0 1 4)Do (If not "!#Sel!" == "" (If "!#Sel:~0,1!" == " " (If "!#L!" == "F" (Set "#Sel=!#Sel:~1!"))Else (Set "#L=T"))Else (Set "#Sel=_Nil"))&if not "!#Sel!" == "_Nil" if not "!#Sel!" == "" (Set "#Sel=!#Sel:_Nil=!")"
  56.  Set "Menu=(If defined _End Goto :End) &For %%n in (1 2)Do if %%n==2 (%ClearArray% & %ResetVars% &(For %%G in (!Options!)Do For %%z in (!_O!)Do %BuildArray% & %DisplayArray% &Set /A "_O+=1")& %MakeChoice% & %Return% )Else Set Options="
  57.  For %%M in ( ClearArray ResetVars BuildArray DisplayArray MakeChoice Return )Do Set "%%M="
  58. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  59.  Setlocal EnableExtensions EnableDelayedExpansion & REM [* required to be activated AFTER definition of Macro's. *]
  60. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Commence script main body | Demonstration of usages
  61. :Loop
  62. rem [* doublequoting of options recommended - Required for strings containing standard delimiters. *]
  63.  If not defined _End cls
  64. rem [* Param Substring modification examples passing parameters to labels called when selected - Optional feature. *]
  65.  Set /A "PRM=!Random! %%100 + 100" & Rem [* Example param only *]
  66.  %Menu:Param=PRM% "Exit" "Next" "Option 1" "Option 2" "Option 3" "Option 4"
  67.  Echo/"!#Sel!" | Findstr.exe /LIC:"Exit" > Nul 2> Nul && (Goto :End)
  68. Goto :Loop
  69.  
  70. :Next
  71. rem [* Selection of a single option occurs by using the macro without a loop or resetting the #Sel variable
  72. rem - between %menu% use and next iteration of a loop *]
  73. rem [* Process #Sel items using the !#Sel! variable - then ** SET "#Sel-_Nil" prior to next usage of Menu macro** *]
  74.  Set "Menu1Opts=!#Sel!"
  75.  Set "#Sel="
  76.  Cls
  77.  Call :C_Out 03 "[Demo - Parameter usage.  %%%%1 ~ '%1' == '!%~1!' ] Selected =" !Menu1opts!
  78.  %Menu% "Exit" " + Continue" ".. Back"
  79.  Echo/!#Sel! | Findstr.exe /LIC:".. Back" > Nul 2> Nul && (Set "#Sel=!Menu1opts!"& Exit /B 0)
  80.  Echo/!#Sel! | Findstr.exe /LIC:"Exit" > Nul 2> Nul && (%CLOSE%)
  81.  Set "#Sel="
  82.  Echo/!Menu1opts! | Findstr.exe /LIC:"_Nil" > Nul 2> Nul && (Call :C_Out 04 "Selection Required."&(Pause & Exit /B 0)) || Call :C_Out 02 "Confirmed=" !Menu1opts!
  83.  Call :C_Out 02 "Example complete."
  84.  Pause
  85. rem [* to exit at end of script or by user selection *]
  86.  %CLOSE%
  87. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End of example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement