Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off
- PUSHD "%TEMP%"
- rem /* Macro Definitions */
- (Set \n=^^^
- %= macro newline Do not modify =%
- )
- (Set LF=^
- %= linefeed. Do not modify =%)
- If "!![" == "[" (
- Echo/%%COL%% macro must be defined prior to delayed expansion being enabled
- Goto :end
- )
- (Set COMMENT=rem {i} ^^^
- %= macro Comment line. Do not modify =%
- )
- For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
- rem /* %hCol% - Alternate color macro; escaped for use in COL macro. No error checking. Usage: (%hCol:?=HEXVALUE%Output String) */
- Set "hCol=For %%o in (1 2)Do if %%o==2 (^<nul set /p ".=%DEL%" ^> "!os!" ^& findstr /v /a:? /R "^$" "!os!" nul ^& del "!os!" ^> nul 2^>^&1 )Else Set os="
- rem /* %TB% - used with substitution within COL macro to format help output; not fit for general use, */
- Set "TB=^&^< nul Set /P "=.%DEL%!TAB!"^&"
- rem /* %COL% - main color output macro. Usage: (%COL%{[a-f0-9][a-f0-9]}{String to Print}) */
- rem /* IMPORTANT: maximimum single line output length of 227 characters for all macro Concatenations on same line. may vary depending on console properties. */
- Set COL=Set "_e=0"^&Set "Oline="^& For %%l in (1 2)Do if %%l==2 (%\n%
- %COMMENT:{i}= Reset vars Oline and _e prior to redefining Oline and commencing macro main body \n%
- If not "!Oline!" == "" (%\n%
- Set "_Str="%\n%
- %COMMENT:{i}= Capture {Arg 1 HEX} {Arg2 output String} \n%
- For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do (%\n%
- Set "Hex=%%G"%\n%
- Set "_Str=%%~H"%\n%
- )%\n%
- %COMMENT:{i}= test HEX sting is within valid range, flag true or false using _e and Str vars to control error handling. \n%
- %COMMENT:{i}= remove this comment line and the below line for faster performace. Incorrect hex values will print console default colors \n%
- Echo/!Hex!^|findstr /RX "[0-9a-fA-F][0-9a-fA-F]" ^> nul ^|^| (Echo/^&(%hCol:?=04%Invalid - )%TB%(%hCol:?=06%Bad Hex value.)%TB%(%hCol:?=01%%%COL%%{!Hex!}{!_Str!})%TB:TAB=LF%(%hCol:?=02%!Usage!)^&Set "_Str="^&Set "_e=1")%\n%
- If not "!_Str!" == "" (%\n%
- %COMMENT:{i}= Create file with the name of the output string to print in Color using findstr if all Args valid \n%
- ^<nul set /p ".=%DEL%" ^> "!_Str!"%\n%
- findstr /v /a:!Hex! /R "^$" "!_Str!" nul 2^> nul %\n%
- %COMMENT:{i}= delete the created file. \n%
- del "!_Str!" ^> nul 2^>^&1 %\n%
- )Else If not !_e! EQU 1 (%\n%
- %COMMENT:{i}= No Arg 2 Error handling \n%
- Set "_e=2"^&Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%Arg 2 absent.)%TB%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=04%Input is required for output string.)%TB:TAB=LF%(%hCol:?=02%!Usage!)%\n%
- )%\n%
- )Else (%\n%
- %COMMENT:{i}= No args Error Handling \n%
- Set "_e=3"^&Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%No Args)%TB:TAB=!TAB!!TAB!%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=02%!Usage!)%\n%
- )%\n%
- )Else Set Oline=
- Set "usage=%%COL%%{[a-f0-9][a-f0-9]}{String to Print}"
- For /F eol^=^%LF%%LF%^ delims^= %%A in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do Set "TAB=%%A"
- rem /* removes escaping from macros to enable use outside of COL macro */
- Set "hCol=%hCol:^=%"
- Set "TB=%TB:^=%"
- Setlocal EnableDelayedExpansion
- rem /* usage examples. NOTE: expanded macro concatenations should be kept to a minimum as mulitple expansions
- rem may exceed the line length limit of the parser. */
- (%COL%{02}{"green on black,"})
- (%COL%{10}{"black on blue"})
- Echo/
- (%COL%{04}{"red on black"})
- (%COL%{34}{" red on blue"})
- (%COL%{40}{"black on red"})
- Echo/& %COL%{03}{Demonstration of error handling-}
- rem /* error handling *
- Echo/%TB:TAB=!LF! % %hCol:?=20%Example 1 - No args
- %COL%
- Echo/%TB:TAB=!LF! % %hCol:?=20%Example 2 - Missing 2nd Arg
- %COL%{ff}
- Echo/%TB:TAB=!LF! % %hCol:?=20%Example 3 - Invalid hex value for 1st Arg
- %COL%{HF}{string}
- Echo/%TB:TAB=!LF! % %hCol:?=0d%Done
- :end
- POPD
- Goto :Eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement