Advertisement
T3RRYT3RR0R

Menu Macro - Color Version

Mar 27th, 2021 (edited)
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.37 KB | None | 0 0
  1. ==================================================================
  2. @Echo off
  3. :# [Menu] Macro Author: T3RRY = Version: 1.0.5
  4. :# Supports Coloring of menu on Windows 10
  5. :# IMPORTANT - RESERVED VARIABLES: \n [Menu]*
  6. :#  - [Menu] macro variables prefixed with [Menu] to avoid potential conflict with other script variables.
  7. :#  - \n is a standard definition of an escaped linefeed for creating multiline Macro variables.
  8.  
  9. :# Menu macro escaped for Definition with DelayedExpansion Enabled. Ensures correct Environment:
  10.  If not "!" == "" (
  11.   Setlocal EnableExtensions EnableDelayedExpansion
  12.  )
  13.  
  14. :# Windows Version control. Assigns flag true if system is windows 10 build GTR 10586
  15. :# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
  16. :# Version 1511 build number = 10.0.10586
  17.  Set "[menu]Win10="
  18.  For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "[menu]Win10=True"
  19.  
  20. :# Test if virtual terminal codes enabled ; enable if false
  21. :# removes [menu]win10 flag definition if version does not support Virtual Terminal sequences
  22.  If defined [menu]Win10 (
  23.   Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel    REG_DWORD    0x1" > nul || (
  24.     Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
  25.   ) > Nul || Set "[menu]Win10="
  26.  )
  27.  
  28. (Set \n=^^^
  29.  
  30. %= Newline var \n for multi-line macro definition - Do not modify. =%)
  31.  
  32. :# Key index list Allows maximum 36 menu options [ 0 indexed ]. Component of Menu Macro
  33.  Set "[menu]Key_List=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  34.  
  35. :# Get console width for dividing line
  36.  for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %SystemRoot%\System32\findstr.exe /LIC:"Columns"`) do Set /A "[menu]Console_Width=%%W"
  37.  
  38. :# Build Menu option color array [ RGB colormix ; red biased scaling darker. ]
  39.  If defined [menu]Win10 For /F %%E in ('Echo prompt $E^|cmd')Do Set "\E=%%E"
  40.  
  41.  Set "[menu]i=0"
  42.  If defined [menu]Win10 (
  43.   Set "[Menu]Color_Off=%\E%[0m"
  44.   For /L %%i in (232 -4 96)Do (
  45.    Set /A "[menu]BB=(%%i/4)+50,[menu]GG=%%i-[menu]BB,[menu]RR=[menu]GG+[menu]BB+(%%i/2)"
  46.    Set "[menu]Color[![menu]i!]=%\E%[38;2;![menu]RR!;![menu]GG!;![menu]BB!m"
  47.    Set /A [menu]i+=1
  48.  ))
  49.  Set "[menu]i="
  50.  
  51. :# Define highlight color to use for a selected option
  52.  If defined [menu]Win10 (
  53.   Set "[menu]Highlight_Color=%\E%[0m%\E%[48;2;50;150;200m%\E%[31m%\E%[K"
  54.  )
  55.  
  56. :# Assign Flag true to enable Highlighting of selected option.
  57. :# - Clears screen before menu is output in order to count line position of options.
  58. :# - Highlight will only be seen if the screen output is visible for a noticible length of time after
  59. :# - expansion of the [Menu] macro. Use 'Pause', 'Timeout' or another method of effecting a delay if the screen buffer
  60. :# - will be refreshed or scrolled by subsequent output immediately after menu selection to allow highlight to be seen.
  61. :# - If not defined as true, screen will not be cleared prior to menu output and selection will not be highlighted.
  62.  If defined [menu]Win10 (
  63.   Set "[menu]Highlight=true"
  64.  )
  65.  
  66. :# Force console line dimensions to a size that supports highlight mode by preventing buffer scroll
  67.  If /I "%[menu]Highlight%" == "true" (
  68.   for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %SystemRoot%\System32\findstr.exe /LIC:"Lines"`) do If %%W LSS 40 Mode ![menu]Console_Width!,40
  69.  )
  70.  
  71. :# Build dividing line for menu output.
  72.  Set "[menu]Divider=" & For /L %%i in (2 1 %[menu]Console_Width%)Do Set "[menu]Divider=![menu]Divider!-"
  73.  
  74. :# Define dividing line Color
  75.  If defined [menu]Win10 (
  76.   Set "[menu]Divider=%\E%[33m%\E%[4m![menu]Divider!%\E%[0m"
  77.  )
  78.  
  79. :# Menu macro Usage: %Menu% "quoted" "list of" "options"
  80. :# Supports 36 options [ options are zero indexed ]
  81.  
  82.      Set [Menu]=For %%n in (1 2)Do if %%n==2 (%\n%
  83. %= Clear screen if highlight flag true  =%  If /I "^![menu]Highlight^!" == "true" CLS%\n%
  84. %= Output Dividing Line                 =%  Echo(^^![menu]Divider^^!%\n%
  85. %= Reset Index value for Opt[#]         =%  Set "[menu]Index=0"%\n%
  86. %= Undefine choice option key list      =%  Set "[menu]Choice_Keys="%\n%
  87. %= For Each in list;                    =%  For %%G in (^^![menu]Options^^!)Do If not ^^![menu]Index^^! GTR 35 (%\n%
  88. %= For Option Index value               =%   For %%i in (^^![menu]Index^^!)Do (%\n%
  89. %= Build the Choice key list and Opt[#] =%    Set "[menu]Choice_Keys=^![menu]Choice_Keys^!^![menu]Key_List:~%%i,1^!"%\n%
  90. %= arrays using the character at the    =%    Set "[menu]Opt[^![menu]Key_List:~%%i,1^!]=%%~G"%\n%
  91. %= current substring index.             =%    Set "[menu]Option_Output=%%~G"%\n%
  92. %!!%    Echo(^^![menu]Color[%%i]^^![^^![menu]Key_List:~%%i,1^^!] ^^![menu]Option_Output:#=^^!![menu]Color_Off!%\n%
  93. %= Store line number of options         =%    Set "[menu]Line#^![menu]Key_List:~%%i,1^!=%%i"%\n%
  94. %= Increment Opt[#] Index var 'Index'   =%    Set /A "[menu]Index+=1"%\n%
  95. %= Close Index loop                     =%   )%\n%
  96. %= Close Options loop                   =%  )%\n%
  97. %= Output Dividing Line                 =%  Echo(^^![menu]Divider^^!%\n%
  98. %= Select option by character index     =%  For /F "Delims=" %%o in ('%SystemRoot%\System32\Choice.exe /N /C:^^![menu]Choice_Keys^^!')Do (%\n%
  99. %= Assign return var 'OPTION' with the  =%   Set "[menu]Selection=^![menu]Opt[%%o]^!"%\n%
  100. %= Highlight selected option with Line# =%   If /I "^![menu]Highlight^!" == "true" (For /F "Delims=" %%X in ("^![menu]Line#%%o^!")Do For /F "Delims=" %%Y in ('Set /A "%%X+2"')Do Echo(%\E%[%%Y;1H^^![menu]Highlight_Color^^!{^^![menu]Key_List:~%%X,1^^!} ^^![menu]Opt[%%o]^^!![menu]Color_Off!)%\n%
  101. %= value selected from Opt[#] array.    =%   If /I "^![menu]Selection^!" == "Exit"   CLS ^& Exit /B 1%\n%
  102. %= Exit type determines Errorlevel.     =%   If /I "^![menu]Selection^!" == "Previous" CLS ^& Exit /B 0%\n%
  103. %= Return to previous script on Exit    =%  )%\n%
  104. %= Move cursor to end of menu field     =%  If /I "^![menu]Highlight^!" == "true" For /F "Delims=" %%Y in ('Set /A [menu]Index + 2')Do Echo(%\E%[%%Y;1H%\n%
  105. %= Capture Macro input - Options List   =% )Else Set [menu]Options=
  106. ========================================== :# End Definition of Menu Macro
  107.  
  108. :# Usage example
  109. :Menu
  110.  %[Menu]% Exit Demo list of options to select from. Each Displayed with different color tone and index key for selecting the desired choice. "Doublequote multiword options"
  111.  Pause
  112. Goto :Menu
  113. :# Selected option returned in '[menu]Selection' variable.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement