Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==================================================================
- @Echo off
- :# [Menu] Macro Author: T3RRY = Version: 1.0.5
- :# Supports Coloring of menu on Windows 10
- :# IMPORTANT - RESERVED VARIABLES: \n [Menu]*
- :# - [Menu] macro variables prefixed with [Menu] to avoid potential conflict with other script variables.
- :# - \n is a standard definition of an escaped linefeed for creating multiline Macro variables.
- :# Menu macro escaped for Definition with DelayedExpansion Enabled. Ensures correct Environment:
- If not "!" == "" (
- Setlocal EnableExtensions EnableDelayedExpansion
- )
- :# Windows Version control. Assigns flag true if system is windows 10 build GTR 10586
- :# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
- :# Version 1511 build number = 10.0.10586
- Set "[menu]Win10="
- For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "[menu]Win10=True"
- :# Test if virtual terminal codes enabled ; enable if false
- :# removes [menu]win10 flag definition if version does not support Virtual Terminal sequences
- If defined [menu]Win10 (
- Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel REG_DWORD 0x1" > nul || (
- Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
- ) > Nul || Set "[menu]Win10="
- )
- (Set \n=^^^
- %= Newline var \n for multi-line macro definition - Do not modify. =%)
- :# Key index list Allows maximum 36 menu options [ 0 indexed ]. Component of Menu Macro
- Set "[menu]Key_List=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- :# Get console width for dividing line
- for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %SystemRoot%\System32\findstr.exe /LIC:"Columns"`) do Set /A "[menu]Console_Width=%%W"
- :# Build Menu option color array [ RGB colormix ; red biased scaling darker. ]
- If defined [menu]Win10 For /F %%E in ('Echo prompt $E^|cmd')Do Set "\E=%%E"
- Set "[menu]i=0"
- If defined [menu]Win10 (
- Set "[Menu]Color_Off=%\E%[0m"
- For /L %%i in (232 -4 96)Do (
- Set /A "[menu]BB=(%%i/4)+50,[menu]GG=%%i-[menu]BB,[menu]RR=[menu]GG+[menu]BB+(%%i/2)"
- Set "[menu]Color[![menu]i!]=%\E%[38;2;![menu]RR!;![menu]GG!;![menu]BB!m"
- Set /A [menu]i+=1
- ))
- Set "[menu]i="
- :# Define highlight color to use for a selected option
- If defined [menu]Win10 (
- Set "[menu]Highlight_Color=%\E%[0m%\E%[48;2;50;150;200m%\E%[31m%\E%[K"
- )
- :# Assign Flag true to enable Highlighting of selected option.
- :# - Clears screen before menu is output in order to count line position of options.
- :# - Highlight will only be seen if the screen output is visible for a noticible length of time after
- :# - expansion of the [Menu] macro. Use 'Pause', 'Timeout' or another method of effecting a delay if the screen buffer
- :# - will be refreshed or scrolled by subsequent output immediately after menu selection to allow highlight to be seen.
- :# - If not defined as true, screen will not be cleared prior to menu output and selection will not be highlighted.
- If defined [menu]Win10 (
- Set "[menu]Highlight=true"
- )
- :# Force console line dimensions to a size that supports highlight mode by preventing buffer scroll
- If /I "%[menu]Highlight%" == "true" (
- 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
- )
- :# Build dividing line for menu output.
- Set "[menu]Divider=" & For /L %%i in (2 1 %[menu]Console_Width%)Do Set "[menu]Divider=![menu]Divider!-"
- :# Define dividing line Color
- If defined [menu]Win10 (
- Set "[menu]Divider=%\E%[33m%\E%[4m![menu]Divider!%\E%[0m"
- )
- :# Menu macro Usage: %Menu% "quoted" "list of" "options"
- :# Supports 36 options [ options are zero indexed ]
- Set [Menu]=For %%n in (1 2)Do if %%n==2 (%\n%
- %= Clear screen if highlight flag true =% If /I "^![menu]Highlight^!" == "true" CLS%\n%
- %= Output Dividing Line =% Echo(^^![menu]Divider^^!%\n%
- %= Reset Index value for Opt[#] =% Set "[menu]Index=0"%\n%
- %= Undefine choice option key list =% Set "[menu]Choice_Keys="%\n%
- %= For Each in list; =% For %%G in (^^![menu]Options^^!)Do If not ^^![menu]Index^^! GTR 35 (%\n%
- %= For Option Index value =% For %%i in (^^![menu]Index^^!)Do (%\n%
- %= Build the Choice key list and Opt[#] =% Set "[menu]Choice_Keys=^![menu]Choice_Keys^!^![menu]Key_List:~%%i,1^!"%\n%
- %= arrays using the character at the =% Set "[menu]Opt[^![menu]Key_List:~%%i,1^!]=%%~G"%\n%
- %= current substring index. =% Set "[menu]Option_Output=%%~G"%\n%
- %!!% Echo(^^![menu]Color[%%i]^^![^^![menu]Key_List:~%%i,1^^!] ^^![menu]Option_Output:#=^^!![menu]Color_Off!%\n%
- %= Store line number of options =% Set "[menu]Line#^![menu]Key_List:~%%i,1^!=%%i"%\n%
- %= Increment Opt[#] Index var 'Index' =% Set /A "[menu]Index+=1"%\n%
- %= Close Index loop =% )%\n%
- %= Close Options loop =% )%\n%
- %= Output Dividing Line =% Echo(^^![menu]Divider^^!%\n%
- %= Select option by character index =% For /F "Delims=" %%o in ('%SystemRoot%\System32\Choice.exe /N /C:^^![menu]Choice_Keys^^!')Do (%\n%
- %= Assign return var 'OPTION' with the =% Set "[menu]Selection=^![menu]Opt[%%o]^!"%\n%
- %= 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%
- %= value selected from Opt[#] array. =% If /I "^![menu]Selection^!" == "Exit" CLS ^& Exit /B 1%\n%
- %= Exit type determines Errorlevel. =% If /I "^![menu]Selection^!" == "Previous" CLS ^& Exit /B 0%\n%
- %= Return to previous script on Exit =% )%\n%
- %= 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%
- %= Capture Macro input - Options List =% )Else Set [menu]Options=
- ========================================== :# End Definition of Menu Macro
- :# Usage example
- :Menu
- %[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"
- Pause
- Goto :Menu
- :# Selected option returned in '[menu]Selection' variable.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement