Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off
- REM Modular menu macro by T3RRY
- REM Environment state may be either Disable or Enable delayedExpansion when
- REM defining and using this Menu macro.
- REM Macro definition and expansion environment trickery thanks to Jeb
- REM resources:
- REM https://www.dostips.com/forum/viewtopic.php?t=9265#p60294
- REM https://www.dostips.com/forum/viewtopic.php?f=3&t=10983&sid=f6937e02068d93bc5a97ef63d4e5319e
- REM Macros with arguments learning resource:
- REM https://www.dostips.com/forum/viewtopic.php?f=3&t=1827
- REM Setlocal EnableDelayedExpansion
- ==================================================== REM = Menu macro Definition BEGIN
- REM IMPORTANT - RESERVED VARIABLES: Menu oc[*] Option Option[Key] Option[#] div_line \n
- (Set \n=^^^
- %= Newline var \n for multi-line macro definition - Do not modify. =%)
- REM - use REM / remove REM on the below line to enable / disable menu dividing line
- REM Goto :NoDividingLine
- REM Enable DE environment to perform variable concatenation within a for loop
- Setlocal EnableDelayedExpansion
- REM Get console width for dividing line
- For /f "usebackq tokens=2* delims=: " %%W in (`mode con ^| %__APPDIR__%findstr.exe /LIC:"Columns"`) do Set /A "Console_Width=%%W"
- Set "div_line=" & For /L %%i in (2 1 %Console_Width%)Do Set "div_line=!div_line!-"
- Endlocal & Set "div_line=%div_line%"
- :NoDividingLine
- REM keymap. translates literal keypress to the numeric position of the item in the menu list
- 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,^
- 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,^
- 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
- REM Valid choice characters
- Set "ChoList=123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0"
- REM Menu macro Usage: %Menu% "quoted" "list of" "options"
- %= Outer for loop allows environment independant definition =% For /f %%! in ("! ^! ^^^!") Do ^
- %= IMPORTANT - No whitespace permitted here =%Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
- %= Detect Environment state =% For /f "tokens=2" %%E in ("%%!%%! D """)Do (%\n%
- %= Conditionally enable Delayed Expansion =% For /f "delims=" %%E in ("%%~E")Do Setlocal EnableDelayedExpansion%\n%
- %= Output Dividing Line =% If Defined Div_Line Echo(%%!div_line%%!%\n%
- %= Reset Choice.# index value for Opt[#] =% Set "Choice.#=0"%\n%
- %= Undefine choice option key list =% Set "Choice.Chars="%\n%
- %= For Each in list; =% For %%G in (%%!Options%%!)Do (%\n%
- %= For Option Index value =% For %%i in (%%!Choice.#%%!)Do If not %%i GTR 35 (%\n%
- %= Build the Choice key list and Opt[#] =% Set "Choice.Chars=%%!Choice.Chars%%!%%!ChoList:~%%i,1%%!"%\n%
- %= array using the character at the =% Set "Opt[%%!ChoList:~%%i,1%%!]=%%~G"%\n%
- %= current substring index. =% Set "Option.Output=%%~G"%\n%
- %= Display [key] OptionString =% Echo([%%!ChoList:~%%i,1%%!] %%!Option.Output%%!%\n%
- %= Increment Opt[#] Index var 'Choice.#' =% Set /A "Choice.#+=1"%\n%
- %= Close Choice.# loop =% )%\n%
- %= Close Options loop =% )%\n%
- %= Output Dividing Line =% If Defined Div_Line Echo(%%!div_line%%!%\n%
- %= 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%
- %= Conditonal Endlocal; tunnel returnVars =% (For /f "delims=" %%E in ("%%~E")Do Endlocal) ^& (%\n%
- %= exit [sub]script w/out modifying option =% If /I "%%V" == "Exit" Exit /B 2%\n%
- %= Assign 'Option' with literal string =% Set "Option=%%V"%\n%
- %= Assign 'Option[key] with key pressed =% Set "Option[Key]=%%o"%\n%
- %= Assign 'Option[#] with option number =% Set "Option[#]=%%~W"%\n%
- %= Return to previous script on Exit =% ) ) )%\n%
- %= Capture Macro input - Options List =%)Else Set Options=
- ========================================== REM = Menu Macro Definition END
- REM demonstrates retention of last option variables upon selection of exit
- REM Call:DemoMenu
- REM Echo(You last chose %Option% by pressing %Option[key]% at index %option[#]%
- REM Exit /b 0
- :DemoMenu
- CLS
- %Menu% exit List of your Options here. "Double qoute" "Delimited Strings" filler to extend option count. caps at 36 \ / \ / \ / \ / \ / \ / \ / \ / \ / \ . / \ .
- REM Returns Variable= Value
- REM Option= the literal string
- REM Option[Key]= the key used to select the option
- REM Option[#]= the Integer value of the options occurance in the list
- Echo(You Pressed %Option[key]% to Choose '%Option%'; option number = %option[#]%
- Pause
- Goto:DemoMenu
Advertisement
Comments
-
- This macro is desinged to be easily copy / pastable into any script,
- Where definition occurs between:
- ==================================================== REM = Menu macro Definition BEGIN
- and:
- ========================================== REM = Menu Macro Definition END
- 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