Advertisement
T3RRYT3RR0R

Menu_Macro

Jan 9th, 2024 (edited)
1,113
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.94 KB | Source Code | 0 0
  1. @Echo off
  2.  
  3. REM Modular menu macro by T3RRY
  4. REM Environment state may be either Disable or Enable delayedExpansion when
  5. REM defining and using this Menu macro.
  6. REM Macro definition and expansion environment trickery thanks to Jeb
  7. REM resources:
  8. REM https://www.dostips.com/forum/viewtopic.php?t=9265#p60294
  9. REM https://www.dostips.com/forum/viewtopic.php?f=3&t=10983&sid=f6937e02068d93bc5a97ef63d4e5319e
  10. REM Macros with arguments learning resource:
  11. REM https://www.dostips.com/forum/viewtopic.php?f=3&t=1827
  12.  
  13. REM Setlocal EnableDelayedExpansion
  14.  
  15. ==================================================== REM = Menu macro Definition BEGIN
  16. REM IMPORTANT - RESERVED VARIABLES: Menu oc[*] Option Option[Key] Option[#] div_line \n
  17.  
  18. (Set \n=^^^
  19.  
  20. %= Newline var \n for multi-line macro definition - Do not modify. =%)
  21.  
  22. REM - use REM / remove REM on the below line to enable / disable menu dividing line
  23. REM Goto :NoDividingLine
  24.  
  25. REM Enable DE environment to perform variable concatenation within a for loop
  26.  Setlocal EnableDelayedExpansion
  27. REM Get console width for dividing line
  28.  For /f "usebackq tokens=2* delims=: " %%W in (`mode con ^| %__APPDIR__%findstr.exe /LIC:"Columns"`) do Set /A "Console_Width=%%W"
  29.  Set "div_line=" & For /L %%i in (2 1 %Console_Width%)Do Set "div_line=!div_line!-"
  30.  Endlocal & Set "div_line=%div_line%"
  31. :NoDividingLine
  32.  
  33. REM keymap. translates literal keypress to the numeric position of the item in the menu list
  34. Set /a oc[0]=36,oc[1]=1,oc[2]=2,oc[3]=3,oc[4]=4,oc[5]=5,oc[6]=6,oc[7]=7,oc[8]=8,oc[9]=9,oc[a]=10,oc[b]=11,^
  35. oc[c]=12,oc[d]=13,oc[e]=14,oc[f]=15,oc[g]=16,oc[h]=17,oc[i]=18,oc[j]=19,oc[k]=20,oc[l]=21,oc[m]=22,oc[n]=23,^
  36. oc[o]=24,oc[p]=25,oc[q]=26,oc[r]=27,oc[s]=28,oc[t]=29,oc[u]=30,oc[v]=31,oc[w]=32,oc[x]=33,oc[y]=34,oc[z]=35
  37.  
  38. REM Valid choice characters
  39. Set "ChoList=123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0"
  40.  
  41. REM Menu macro Usage: %Menu% "quoted" "list of" "options"
  42. %= Outer for loop allows environment independant definition =% For /f %%! in ("! ^! ^^^!") Do ^
  43. %= IMPORTANT - No whitespace permitted here =%Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
  44. %= Detect Environment state                 =%  For /f "tokens=2" %%E in ("%%!%%! D """)Do (%\n%
  45. %= Conditionally enable Delayed Expansion   =%    For /f "delims=" %%E in ("%%~E")Do Setlocal EnableDelayedExpansion%\n%
  46. %= Output Dividing Line                     =%    If Defined Div_Line Echo(%%!div_line%%!%\n%
  47. %= Reset Choice.# index value for Opt[#]    =%    Set "Choice.#=0"%\n%
  48. %= Undefine choice option key list          =%    Set "Choice.Chars="%\n%
  49. %= For Each in list;                        =%    For %%G in (%%!Options%%!)Do (%\n%
  50. %= For Option Index value                   =%      For %%i in (%%!Choice.#%%!)Do If not %%i GTR 35 (%\n%
  51. %= Build the Choice key list and Opt[#]     =%        Set "Choice.Chars=%%!Choice.Chars%%!%%!ChoList:~%%i,1%%!"%\n%
  52. %= array using the character at the         =%        Set "Opt[%%!ChoList:~%%i,1%%!]=%%~G"%\n%
  53. %= current substring index.                 =%        Set "Option.Output=%%~G"%\n%
  54. %= Display [key] OptionString               =%        Echo([%%!ChoList:~%%i,1%%!] %%!Option.Output%%!%\n%
  55. %= Increment Opt[#] Index var 'Choice.#'    =%        Set /A "Choice.#+=1"%\n%
  56. %= Close Choice.# loop                      =%      )%\n%
  57. %= Close Options loop                       =%    )%\n%
  58. %= Output Dividing Line                     =%    If Defined Div_Line Echo(%%!div_line%%!%\n%
  59. %= Select option by character index         =%    For /f "delims=" %%o in ('%__APPDIR__%Choice.exe /N /C:%%!Choice.Chars%%!')Do For /f "tokens=1,2 delims=;" %%V in ("%%!Opt[%%o]%%!;%%!oc[%%o]%%!")Do (%\n%
  60. %= Conditonal Endlocal; tunnel returnVars   =%      (For /f "delims=" %%E in ("%%~E")Do Endlocal) ^& (%\n%
  61. %= exit [sub]script w/out modifying option  =%        If /I "%%V" == "Exit" Exit /B 2%\n%
  62. %= Assign 'Option' with literal string      =%        Set "Option=%%V"%\n%
  63. %= Assign 'Option[key] with key pressed     =%        Set "Option[Key]=%%o"%\n%
  64. %= Assign 'Option[#] with option number     =%        Set "Option[#]=%%~W"%\n%
  65. %= Return to previous script on Exit        =%  )  )  )%\n%
  66. %= Capture Macro input - Options List       =%)Else Set Options=
  67. ========================================== REM = Menu Macro Definition END
  68.  
  69. REM demonstrates retention of last option variables upon selection of exit
  70.  
  71. REM Call:DemoMenu
  72. REM Echo(You last chose %Option% by pressing %Option[key]% at index %option[#]%
  73. REM Exit /b 0
  74.  
  75. :DemoMenu
  76. CLS
  77.  
  78.  %Menu% exit List of your Options here. "Double qoute" "Delimited Strings" filler to extend option count. caps at 36 \ / \ / \ / \ / \ / \ / \ / \ / \ / \ . / \ .
  79.  
  80. REM Returns        Variable=    Value
  81. REM                Option=      the literal string
  82. REM                Option[Key]= the key used to select the option
  83. REM                Option[#]=   the Integer value of the options occurance in the list
  84.  
  85.  Echo(You Pressed %Option[key]% to Choose '%Option%'; option number = %option[#]%
  86.  
  87.  Pause
  88. Goto:DemoMenu
  89.  
  90.  
Tags: batch macro Menu
Advertisement
Comments
  • T3RRYT3RR0R
    128 days
    # text 0.52 KB | 0 0
    1. This macro is desinged to be easily copy / pastable into any script,
    2. Where definition occurs between:
    3. ==================================================== REM = Menu macro Definition BEGIN
    4. and:
    5. ========================================== REM = Menu Macro Definition END
    6.  
    7. The macro will be defined and execute correctly irregardless of whether the host script has DelayedExpansion Enabled or Disabled thanks to For metavariable expansion "trickery" pioneered by Jeb over on Dostips - see included links for more information.
Add Comment
Please, Sign In to add comment
Advertisement