T3RRYT3RR0R

PRO Cursor color and output control Macro

Jan 22nd, 2021 (edited)
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 14.75 KB | None | 0 0
  1. ::: Author T3RRY : Created 09/04/2021 : Version 1.0.5
  2. ::: Version changes:
  3. ::: - ADDED random subarg for /C color switch.
  4. ::: - ADDED optional companion switch to /C - '/B'
  5. :::  - When /C random subarg is used, /B may be used with any ONE of the following: R G B C M Y
  6. ::: - To bias the output color towards Red Green Blue Cyan Magenta or Yellow
  7. ::: - Support REMOVED for switch usage pattern /Switch:value
  8. ::: - ADDED support for switches with common prefix.
  9. ::: - ADDED /T timeout switch for subsecond delays
  10. ::: - CORRECTED Switch validation method to handle Switches at EOL with no subargs
  11. ::: - ADDED /E Switch to allow /C value to be preserved or Color to be changed at EOL with an integer subarg.
  12. :::
  13. ::: Purpose      : Color and cursor position macro for windows 10 batch files
  14. ::: - Allows rapid display of colored output at specified screen position.
  15. :::   For more information, read the usage.
  16. :::
  17. ::: Uses macro parameter and switch handling template.
  18. :::  - See :  https://pastebin.com/gzL7AYpC
  19.  
  20. @Echo off
  21.  
  22. :# Windows Version control. Assigns flag true if system is windows 10.
  23.  Set "Win10="
  24.  Ver | Findstr /LIC:" 10." > nul && Set "Win10=true"
  25.  
  26. :# Test if virtual terminal codes enabled ; enable if false
  27. :# removes win10 flag definition if version does not support Virtual Terminal sequences
  28.  If defined Win10 (
  29.   Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel    REG_DWORD    0x1" > nul || (
  30.     Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
  31.   ) > Nul || Set "Win10="
  32.  )
  33.  If not defined Win10 (
  34.   Echo(Virtual terminal sequences not supported on your system
  35.   Exit /B 1
  36.  )
  37.  
  38.  If "%~1" == "" (
  39.   Mode 200,150
  40.   Cls
  41.  )
  42. (Set \n=^^^
  43.  
  44. %= \n macro newline variable. Do not modify =%)
  45.  
  46. :# assign virtual terminal control character 0x27 'escape' variable \E
  47.  For /F %%a in ( 'Echo prompt $E ^| cmd' )Do Set "\E=%%a"
  48.  
  49. :# Virtual Terminal 'VT' sequence Resource :
  50. :# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  51.  
  52. ::# usage: %$Cout% [/?] | [/Alt | /Main] [/H [-|+]] [/T Int] [/X Int | /L Int | /R Int]
  53. ::#                [/Y Int | /U Int | /D Int] [/K |/Del Int | /I Int] [/N]
  54. ::#                [/C Int | /C Int,Int | /C Int;Int | /C random] [/S "String"] [/E [Optional:{0}|{Int}]
  55. ::# -----------------------------------------------------------------------------------------------------
  56. ::# Available Switches     : Description:
  57. ::# -----------------------------------------------------------------------------------------------------
  58. ::# /?                     : This help screen
  59. ::#
  60. ::# /S String              : String to be output. Tested for strings of 500 characters.
  61. ::# /S String{Substituion} : The following characters must be substituted for output
  62. ::# /S ^!Variable:/={FS}^! : {AS}:* {DQ}:" {FS}:/ {EQ}:=
  63. ::#
  64. ::# /C Integer             : Declare output color using VT sequence
  65. ::# /C Integer,Integer     : Chain   mulitple VT color sequences
  66. ::# /C Integer;Integer     : Combine multiple VT values into the one sequence
  67. ::# /C random              : Random RGB foreground color
  68. ::# /B R|G|B|C|M|Y         : Bias /C random color toward Red Green Blue
  69. ::#                        : Cyan Magenta or Yellow. /C random must be used.
  70. ::# /E                     : Preserves /C Color value until /E 0 is used. /C must be used.
  71. ::# /E 0                   : Restores color to Black BG White FG after string output.
  72. ::# /E Integer             : Change color after string output to supplied value.
  73. ::#
  74. ::# /Y Integer             : Move cursor to Line Integer   [ absolute ]
  75. ::# /X Integer             : Move cursor to Column Integer [ absolute ]
  76. ::# /U Integer             : Move cursor Up by Integer
  77. ::# /D Integer             : Move cursor Down by Integer
  78. ::# /R Integer             : Move cursor Right by Integer
  79. ::# /L Integer             : Move cursor Left by Integer
  80. ::#
  81. ::# /H -                   : Hide the cursor  : Note - If Cursor state is changed during a code block
  82. ::#                          redirected to a file, it cannot be changed again except within a code block.
  83. ::# /H +                   : Show the cursor
  84. ::# /Alt                   : Switch to alternate   buffer [ main buffer is preserved ]
  85. ::# /Main                  : Return to main screen buffer [ alternate buffer is cleared ]
  86. ::# /K                     : Clears text to right of current cursor position
  87. ::# /Del Integer           : Deletes Integer columns right of the cursor, shifting existing text left
  88. ::# /I Integer             : Inserts whitespace into Integer columns right of Cursor, shifting text right
  89. ::# /N                     : Output a newline after other switches are executed.
  90. ::# /T Integer             : Subsecond Delay after output. 500 = ~1 Second [ Depending on clockspeed ]
  91. ::#
  92. ::#                                              Note:
  93. ::# The size of $Cout Macro exceeds the ability of CMD's Parser to support Concatenation of Expansions.
  94. ::# -----------------------------------------------------------------------------------------------------
  95.  
  96.  
  97.  Set $Cout_Switches="Y" "X" "U" "D" "R" "L" "H" "Alt" "Main" "K" "Del" "I" "N" "T" "B" "C" "E" "S"
  98.  
  99.  Set $Cout=For %%n in (1 2)Do if %%n==2 (%\n%
  100.   For %%G in ( %$Cout_Switches% )Do Set "$Cout_Switch[%%~G]="%\n%
  101.   Set "$Cout_leading.args=!$Cout_args:*/=!"%\n%
  102.   For /F "Delims=" %%G in ("!$Cout_leading.args!")Do Set "$Cout_leading.args=!$Cout_args:/%%G=!"%\n%
  103.   Set ^"$Cout_args=!$Cout_args:"=!"%\n%
  104.   Set "$Cout_i.arg=0"%\n%
  105.   For %%G in (!$Cout_leading.args!)Do (%\n%
  106.    Set /A "$Cout_i.arg+=1"%\n%
  107.    Set "$Cout_arg[!$Cout_i.arg!]=%%~G"%\n%
  108.   )%\n%
  109.   If "!$Cout_Args:~-2,1!" == "/" (%\n%
  110.    Set "$Cout_Switch[!$Cout_Args:~-1!]=true"%\n%
  111.    If not "!$Cout_Args:/?=!." == "!$Cout_Args!." Set "$Cout_Switch[help]=true"%\n%
  112.    Set "$Cout_Args=!$Cout_Args:~0,-2!"%\n%
  113.   )%\n%
  114.   For %%G in ( %$Cout_Switches% )Do If not "!$Cout_args:/%%~G =!" == "!$Cout_args!" (%\n%
  115.    Set "$Cout_Switch[%%~G]=!$Cout_Args:*/%%~G =!"%\n%
  116.    If not "!$Cout_Switch[%%~G]:*/=!" == "!$Cout_Switch[%%~G]!" (%\n%
  117.     Set "$Cout_Trail[%%~G]=!$Cout_Switch[%%~G]:*/=!"%\n%
  118.     For %%v in ("!$Cout_Trail[%%~G]!")Do (%\n%
  119.      Set "$Cout_Switch[%%~G]=!$Cout_Switch[%%~G]: /%%~v=!"%\n%
  120.      Set "$Cout_Switch[%%~G]=!$Cout_Switch[%%~G]:/%%~v=!"%\n%
  121.     )%\n%
  122.     Set "$Cout_Trail[%%~G]="%\n%
  123.     If "!$Cout_Switch[%%~G]:~-1!" == " " Set "$Cout_Switch[%%~G]=!$Cout_Switch[%%~G]:~0,-1!"%\n%
  124.     If "!$Cout_Switch[%%~G]!" == "" Set "$Cout_Switch[%%~G]=true"%\n%
  125.    )%\n%
  126.   )%\n%
  127.   If /I "!$Cout_Switch[C]!" == "random" (%\n%
  128.    If not "!$Cout_Switch[B]!" == "" (Set "$Cout_MOD=100")Else Set "$Cout_MOD=200"%\n%
  129.    Set /A "$Cout_RR=!random! %% !$Cout_MOD! + 50,$Cout_GG=!random! %% !$Cout_MOD! + 50,$Cout_BB=!random! %% !$Cout_MOD! + 50"%\n%
  130.    If /I "!$Cout_Switch[B]!" == "R" Set "$Cout_RR=250"%\n%
  131.    If /I "!$Cout_Switch[B]!" == "G" Set "$Cout_GG=250"%\n%
  132.    If /I "!$Cout_Switch[B]!" == "B" Set "$Cout_BB=250"%\n%
  133.    If /I "!$Cout_Switch[B]!" == "M" Set /A "$Cout_RR=!Random! %% 50 + 200,Cout_GG=0,$Cout_BB=!Random! %% 50 + 200"%\n%
  134.    If /I "!$Cout_Switch[B]!" == "Y" Set /A "$Cout_RR=!Random! %% 90 + 100,Cout_GG=!Random! %% 90 + 90,$Cout_BB=0"%\n%
  135.    If /I "!$Cout_Switch[B]!" == "C" Set /A "$Cout_RR=0,Cout_GG=!Random! %% 120 + 30,$Cout_BB=175"%\n%
  136.    Set "$Cout_Switch[C]=38;2;!$Cout_RR!;!$Cout_GG!;!$Cout_BB!"%\n%
  137.   )%\n%
  138.   If "!$Cout_Switch[help]!" == "true" ((For /F "Tokens=1,2 Delims=#" %%Y in ('findstr /BLIC:"::#" "%~f0"')Do @Echo(%%Z)^| @More)%\n%
  139.   If not "!$Cout_Switch[C]!" == ""    (Set "$Cout_Color=%\E%[!$Cout_Switch[C]:,=m%\E%[!m")Else Set "$Cout_Color="%\n%
  140.   If not "!$Cout_Switch[Y]!" == ""    (Set "$Cout_Ypos=%\E%[!$Cout_Switch[Y]!d")Else Set "$Cout_Ypos="%\n%
  141.   If not "!$Cout_Switch[X]!" == ""    (Set "$Cout_Xpos=%\E%[!$Cout_Switch[X]!G")Else Set "$Cout_Xpos="%\n%
  142.   If not "!$Cout_Switch[U]!" == ""    (Set "$Cout_Yoffset=%\E%[!$Cout_Switch[U]!A")Else Set "$Cout_Yoffset="%\n%
  143.   If not "!$Cout_Switch[D]!" == ""    Set "$Cout_Yoffset=%\E%[!$Cout_Switch[D]!B"%\n%
  144.   If not "!$Cout_Switch[R]!" == ""    (Set "$Cout_Xoffset=%\E%[!$Cout_Switch[R]!C")Else Set "$Cout_Xoffset="%\n%
  145.   If not "!$Cout_Switch[L]!" == ""    Set "$Cout_Xoffset=%\E%[!$Cout_Switch[L]!D"%\n%
  146.   If "!$Cout_Switch[H]!" == "-"       Set "$Cout_Cursor=%\E%[?25l"%\n%
  147.   If "!$Cout_Switch[H]!" == "+"       Set "$Cout_Cursor=%\E%[?25h"%\n%
  148.   If "!$Cout_Switch[Main]!" == "true" (Set "$Cout_Buffer=%\E%[?1049l")Else Set "$Cout_Buffer="%\n%
  149.   If "!$Cout_Switch[Alt]!" == "true"  Set "$Cout_Buffer=%\E%[?1049h"%\n%
  150.   If not "!$Cout_Switch[K]!" == ""    (Set "$Cout_LineClear=%\E%[K")Else Set "$Cout_LineClear="%\n%
  151.   If not "!$Cout_Switch[Del]!" == ""  (Set "$Cout_Delete=%\E%[!$Cout_Switch[Del]!P")Else Set "$Cout_Delete="%\n%
  152.   If not "!$Cout_Switch[I]!" == ""    (Set "$Cout_Insert=%\E%[!$Cout_Switch[I]!@")Else Set "$Cout_Insert="%\n%
  153.   If not "!$Cout_Switch[S]!" == ""    (%\n%
  154.    Set "$Cout_String=!$Cout_Switch[S]:{FS}=/!"%\n%
  155.    Set "$Cout_String=!$Cout_String:{EQ}==!"%\n%
  156.    Set "$Cout_String=!$Cout_String:{AS}=*!"%\n%
  157.    Set ^"$Cout_String=!$Cout_String:{DQ}="!"%\n%
  158.   )Else (Set "$Cout_String=")%\n%
  159.   If "!$Cout_Switch[E]!" == "true"    (Set "$Cout_EOLC=!$Cout_Color!")%\n%
  160.   If not "!$Cout_Switch[E]!" == ""    (Set "$Cout_EOLC=%\E%[!$Cout_Switch[E]!m")%\n%
  161.   If "!$Cout_EOLC!" == ""             (Set "$Cout_EOLC=%\E%[0m")%\n%
  162.   ^< nul set /P "=!$Cout_Buffer!!$Cout_Cursor!!$Cout_Ypos!!$Cout_YOffset!!$Cout_Xpos!!$Cout_XOffset!!$Cout_Delete!!$Cout_Insert!!$Cout_Color!!$Cout_LineClear!!$Cout_String!!$COUT_EOLC!"%\n%
  163.   If "!$Cout_Switch[N]!" == "true"    Echo(%\n%
  164.   If not "!$Cout_Switch[T]!" == ""    (For /L %%T in (1 1 !$Cout_Switch[T]!)Do Call :$Cout_Delay_FalseLabel 2^> nul)%\n%
  165.   If "!$Cout_Switch[help]!" == "true" Pause%\n%
  166.  ) Else Set $Cout_args=
  167.  
  168. :# enable macro
  169. Setlocal EnableExtensions EnableDelayedExpansion
  170.  
  171. :# facilitate testing of the macro using parameters from the command line
  172.  
  173.  if not "%~1" == ""  (
  174.   %$Cout% %*
  175.   Exit /B !Errorlevel!
  176.  )
  177.  
  178. :# usage example
  179. (
  180. %$Cout% /H - /C 1,33 /S "                ,      .-;" /N
  181. %$Cout% /C 1,33 /S "             ,  |\    {FS} {FS}  __," /N
  182. %$Cout% /C 1,33 /S "             |\ '.`-.|  |.'.-'" /N
  183. %$Cout% /C 1,33 /S "              \`'-:  `; : {FS}" /N
  184. %$Cout% /C 1,33 /S "               `-._'.  \'|" /N
  185. %$Cout% /C 1,33 /S "              ,_.-` ` `  ~,_" /N
  186. %$Cout% /C 1,33 /S "               '--,.    "
  187. %$Cout% /C 31 /S ".-. "
  188. %$Cout% /C 1,33 /S ",-^{EQ}." /N
  189. %$Cout% /C 1,33 /S "                 {FS}     "
  190. %$Cout% /C 31 /S "{ "
  191. %$Cout% /C 1,36 /S "} "
  192. %$Cout% /C 31 /S ")"
  193. %$Cout% /C 1,33 /S "`"
  194. %$Cout% /C 33 /S ";-."
  195. %$Cout% /C 1,33 /S "}" /N
  196. %$Cout% /C 1,33 /S "                 |      "
  197. %$Cout% /C 31 /S "'-' "
  198. %$Cout% /C 33 /S "{FS}__ |" /N
  199. %$Cout% /C 1,33 /S "                 {FS}          "
  200. %$Cout% /C 33 /S "\_,\|" /N
  201. %$Cout% /C 1,33 /S "                 |          (" /N
  202. %$Cout% /C 1,33 /S "             "
  203. %$Cout% /C 31 /S "__ "
  204. %$Cout% /C 1,33 /S "{FS} '          \" /N
  205. %$Cout% /C random /B G /S "     {FS}\_    "
  206. %$Cout% /C 31 /S "{FS},'`"
  207. %$Cout% /C 1,33 /S "|     '   "
  208. %$Cout% /C 31 /S ".-~^~~-." /N
  209. %$Cout% /C random /B G /S "     |`.\_ "
  210. %$Cout% /C 31 /S "|   "
  211. %$Cout% /C 1,33 /S "{FS}  ' ,    "
  212. %$Cout% /C 31 /S "{FS}        \" /N
  213. %$Cout% /C random /B G /S "   _{FS}  `, \"
  214. %$Cout% /C 31 /S "|  "
  215. %$Cout% /C 1,33 /S "; ,     . "
  216. %$Cout% /C 31 /S "|  ,  '  . |" /N
  217. %$Cout% /C random /B G /S "   \   `,  "
  218. %$Cout% /C 31 /S "|  "
  219. %$Cout% /C 1,33 /S "|  ,  ,   "
  220. %$Cout% /C 31 /S "|  :  !!  : !!| /N
  221. %$Cout% /C random /B G /S "   _\  `,  "
  222. %$Cout% /C 31 /S "\  "
  223. %$Cout% /C 1,33 /S "|.     ,  "
  224. %$Cout% /C 31 /S "|  |  |  | |" /N
  225. %$Cout% /C random /B G /S "   \`  `.   "
  226. %$Cout% /C 31 /S "\ "
  227. %$Cout% /C 1,33 /S "|   '     "
  228. %$Cout% /C 1,32 /S "|"
  229. %$Cout% /C 31 /S "\_|-'|_,'\|" /N
  230. %$Cout% /C random /B G /S "   _\   `,   "
  231. %$Cout% /C 1,32 /S "`"
  232. %$Cout% /C 1,33 /S "\  '  . ' "
  233. %$Cout% /C 1,32 /S "| |  | |  |           "
  234. %$Cout% /C random /B G /S "__" /N
  235. %$Cout% /C random /B G /S "   \     `,   "
  236. %$Cout% /C 33 /S "| ,  '    "
  237. %$Cout% /C 1,32 /S "|_{FS}'-|_\_{FS}     "
  238. %$Cout% /C random /B G /S "__ ,-;` {FS}" /N
  239. %$Cout% /C random /B G /S "    \    `,    "
  240. %$Cout% /C 33 /S "\ .  , ' .| | | | |   "
  241. %$Cout% /C random /B G /S "_{FS}' ` _-`|" /N
  242. %$Cout% /C random /B G /S "     `\    `,   "
  243. %$Cout% /C 33 /S "\     ,  | | | | |"
  244. %$Cout% /C random /B G /S "_{FS}'   .-^ {FS}" /N
  245. %$Cout% /C random /B G /S "     \`     `,   "
  246. %$Cout% /C 33 /S "`\      \{FS}|,| ;"
  247. %$Cout% /C random /B G /S "{FS}'   .-^   |" /N
  248. %$Cout% /C random /B G /S "      \      `,    "
  249. %$Cout% /C 33 /S "`\' ,  | ; "
  250. %$Cout% /C random /B G /S "{FS}'    -^   _{FS}" /N
  251. %$Cout% /C random /B G /S "       `\     `,  "
  252. %$Cout% /C random /B M /S ".-^-. "
  253. %$Cout% /C 1,33 /S "': "
  254. %$Cout% /C random /B G /S "{FS}'     -^   .{FS}" /N
  255. %$Cout% /C random /B G /S "    jgs _`\    ;"
  256. %$Cout% /C random /B M /S "_{  '   ; "
  257. %$Cout% /C random /B G /S "{FS}'    -^     {FS}" /N
  258. %$Cout% /C random /B G /S "       _\`-{FS}__"
  259. %$Cout% /C random /B M /S ".~  `."
  260. %$Cout% /C 1,35,7,48;2;130;100;0 /S "8"
  261. %$Cout% /C random /B M /S ".'.^`~-. "
  262. %$Cout% /C random /B G /S "-^    _,{FS}" /N
  263. %$Cout% /C random /B G /S "    __\      "
  264. %$Cout% /C random /B M /S "{   '-."
  265. %$Cout% /C 1,35,7,48;2;150;130;0 /S "|"
  266. %$Cout% /C random /B M /S ".'.--~'`}"
  267. %$Cout% /C random /B G /S "     _{FS}" /N
  268. %$Cout% /C random /B G /S "    \    .-^` "
  269. %$Cout% /C random /B M /S "}.-~^'"
  270. %$Cout% /C 1,35,7,48;2;170;150;0 /S "@"
  271. %$Cout% /C random /B M /S "'-. '-..'  "
  272. %$Cout% /C random /B G /S "__{FS}" /N
  273. %$Cout% /C random /B G /S "   _{FS}  .^   "
  274. %$Cout% /C random /B M /S "{  -'.~('-._,.'"
  275. %$Cout% /C random /B G /S "\_,{FS}" /N
  276. %$Cout% /C random /B G /S "  {FS}  .^   _{FS}'"
  277. %$Cout% /C random /B M /S "`--; ;  `.  ;" /N
  278. %$Cout% /C random /B G /S "   .-^  _{FS}'      "
  279. %$Cout% /C random /B M /S "`-..__,-'" /N
  280. %$Cout% /C random /B G /S "     __{FS}'" /N
  281. ) > "%~dp0parrot.brd"
  282. TYPE "%~dp0parrot.brd"
  283. DEL "%~dp0parrot.brd"
  284.  
  285. :# Just a bit of animation
  286. For /L %%i in (1 1 50)Do (
  287.  %$Cout% /T 25 /Y 8 /X 26 /C random /B C /S }
  288.  %$Cout% /D 2 /R 5 /I 2
  289.  %$Cout% /U 1 /R 1 /C 33 /S \
  290.  %$Cout% /Y 25 /X 19 /C random /B M /S ".-^-. "
  291.  %$Cout% /D 1 /X 17 /C random /B M /S "_{  '   ; "
  292.  %$Cout% /D 1 /X 15 /C random /B M /S ".~  `."
  293.  %$Cout% /R 1 /C random /B M /S ".'.^`~-. "
  294.  %$Cout% /D 1 /X 14 /C random /B M /S "{   '-."
  295.  %$Cout% /R 1 /C random /B M /S ".'.--~'`}"
  296.  %$Cout% /D 1 /X 15 /C random /B M /S "}.-~^'"
  297.  %$Cout% /R 1 /C random /B M /S "'-. '-..'  "
  298.  %$Cout% /D 1 /X 14 /C random /B M /S "{  -'.~('-._,.'"
  299.  %$Cout% /D 1 /X 15 /C random /B M /S "`--; ;  `.  ;"
  300.  %$Cout% /D 1 /X 19 /C random /B M /S "`-..__,-'"
  301.  %$Cout% /T 25 /Y 8 /X 26 /C random /B B /S {EQ}
  302.  %$Cout% /D 2 /R 5 /Del 2
  303.  %$Cout% /U 1 /R 1 /C 33 /S "|"
  304.  If %%i EQU 50 %$Cout% /H + /Y 34 /L 1
  305. )
  306.  
  307. Goto :Eof
Add Comment
Please, Sign In to add comment