Advertisement
T3RRYT3RR0R

Findstr color print macro

Nov 24th, 2020 (edited)
1,630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.84 KB | None | 0 0
  1. @Echo off
  2. PUSHD "%TEMP%"
  3. rem /* Macro Definitions */
  4. (Set  \n=^^^
  5. %= macro newline Do not modify =%
  6. )
  7. (Set LF=^
  8.  
  9.  
  10. %= linefeed. Do not modify =%)
  11.  If "!![" == "[" (
  12.   Echo/%%COL%% macro must be defined prior to delayed expansion being enabled
  13.   Goto :end
  14.  )
  15. (Set COMMENT=rem {i} ^^^
  16. %= macro Comment line. Do not modify =%
  17. )
  18.  For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
  19. rem /* %hCol% - Alternate color macro; escaped for use in COL macro. No error checking. Usage: (%hCol:?=HEXVALUE%Output String) */
  20.  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="
  21. rem /* %TB%   - used with substitution within COL macro to format help output; not fit for general use, */
  22.  Set "TB=^&^< nul Set /P "=.%DEL%!TAB!"^&"
  23. rem /* %COL%  - main color output macro. Usage: (%COL%{[a-f0-9][a-f0-9]}{String to Print}) */
  24. rem /* IMPORTANT: maximimum single line output length of 227 characters for all macro Concatenations on same line. may vary depending on console properties. */
  25.  Set COL=Set "_e=0"^&Set "Oline="^& For %%l in (1 2)Do if %%l==2 (%\n%
  26.  %COMMENT:{i}= Reset vars Oline and _e prior to redefining Oline and commencing macro main body \n%
  27.   If not "!Oline!" == "" (%\n%
  28.    Set "_Str="%\n%
  29.  %COMMENT:{i}= Capture {Arg 1 HEX} {Arg2 output String} \n%
  30.    For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do (%\n%
  31.     Set "Hex=%%G"%\n%
  32.     Set "_Str=%%~H"%\n%
  33.    )%\n%
  34.  %COMMENT:{i}= test HEX sting is within valid range, flag true or false using _e and Str vars to control error handling. \n%
  35.  %COMMENT:{i}= remove this comment line and the below line for faster performace. Incorrect hex values will print console default colors \n%
  36.    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%
  37.    If not "!_Str!" == "" (%\n%
  38.  %COMMENT:{i}= Create file with the name of the output string to print in Color using findstr if all Args valid \n%
  39.     ^<nul set /p ".=%DEL%" ^> "!_Str!"%\n%
  40.     findstr /v /a:!Hex! /R "^$" "!_Str!" nul 2^> nul %\n%
  41.  %COMMENT:{i}= delete the created file. \n%
  42.     del "!_Str!" ^> nul 2^>^&1 %\n%
  43.    )Else If not !_e! EQU 1 (%\n%
  44.  %COMMENT:{i}= No Arg 2 Error handling  \n%
  45.     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%
  46.    )%\n%
  47.   )Else (%\n%
  48.  %COMMENT:{i}= No args Error Handling \n%
  49.    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%
  50.   )%\n%
  51.  )Else Set Oline=
  52.  Set "usage=%%COL%%{[a-f0-9][a-f0-9]}{String to Print}"
  53.  For /F eol^=^%LF%%LF%^ delims^= %%A in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do Set "TAB=%%A"
  54. rem /* removes escaping from macros to enable use outside of COL macro */
  55.  Set "hCol=%hCol:^=%"
  56.  Set "TB=%TB:^=%"
  57.  Setlocal EnableDelayedExpansion
  58. rem /* usage examples. NOTE: expanded macro concatenations should be kept to a minimum as mulitple expansions
  59. rem    may exceed the line length limit of the parser. */
  60.  (%COL%{02}{"green on black,"})
  61.  (%COL%{10}{"black on blue"})
  62.  Echo/
  63.  (%COL%{04}{"red on black"})
  64.  (%COL%{34}{" red on blue"})
  65.  (%COL%{40}{"black on red"})
  66.  Echo/& %COL%{03}{Demonstration of error handling-}
  67. rem /* error handling *
  68.  Echo/%TB:TAB=!LF! % %hCol:?=20%Example 1 - No args
  69. %COL%
  70.  Echo/%TB:TAB=!LF! % %hCol:?=20%Example 2 - Missing 2nd Arg
  71. %COL%{ff}
  72.  Echo/%TB:TAB=!LF! % %hCol:?=20%Example 3 - Invalid hex value for 1st Arg
  73. %COL%{HF}{string}
  74.  Echo/%TB:TAB=!LF! % %hCol:?=0d%Done
  75. :end
  76.  POPD
  77. Goto :Eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement