T3RRYT3RR0R

Batch Mirror macro for ascii art

Nov 5th, 2021 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.53 KB | None | 0 0
  1. @REM this script demonstrates the use of my fast performing Mirror macro to output symetrical ASCII art.
  2.  
  3.  
  4.  @Cls & @Call:DefMirror
  5.  @If errorlevel 1 @(
  6.   @Call:Defmirror "/?"
  7.   @Exit/b
  8.  )
  9.  
  10.  @rem usage example: Eye of providence.
  11.  %Mirror% "        ^","      `/~","    ``/(@@","   .`<---","     ``'*","    /""""""""","   /;    ","  /;     "," /;      ","<---@MDCCLXXVI"
  12.  
  13.  @Rem example macro repetition features
  14.  @Pause > nul
  15.  @Cls
  16.  %Mirror:#=2% "        ^[4]","      `/~[4]","    ``/(@@[4]","   .`<---[4]","     ``'*[4]","    /""""""""[4]","   /;    [4]","  /;     [4]"," /;      [4]","<---@MDCCLXXVI[4]","{1} "
  17.  %Mirror% "-[69]"
  18.  %Mirror% "[14] '-{3}"
  19. @Rem Demonstrate non alhpanum character support and escaping. Escaped ! requires ^ to also be ^^^ escaped
  20.  %Mirror% "^^^^ ~^^^!@@#$%%&*()_+=-\|]}[{`';:/?.><"""
  21.  @Pause > nul
  22.  %Mirror:#=3% "  ^[5]"," / [5]","/ M[5]" /F
  23.  @Pause > nul
  24. @Rem demonstrate speed of macro. 1000 * 10 horizontal = 10,000 repeats
  25. @Rem execution time [ machine specs will cause varying results ] : ~ 14 seconds : Total of ~ 700 repeats/second
  26.  %Mirror:#=1000% "[10] (--","[10]  \~","[10]   X","[10]  /~"
  27.  
  28. @Goto:Eof
  29.  
  30. :: ===================== | Mirror MACRO Definition. Author - T3RRY | =====================
  31. :DefMirror USAGE:
  32. :: *** Version changes 11/11/2021:
  33. ::     /F 'Flip' switch added. Mirrors pattern vertically.
  34. :: *** Version changes 9/11/2021:
  35. ::     [Int] and {Int} partial repetition subargs may now be interchangeably positioned at string left or right.
  36. ::     Doublequote is now escaped with itself: "" to print " - Each paired " is substituted to `DQ` before string processing.
  37. ::     Multiple linefeeds may now be effected using %Mirror% "{Int}"
  38. ::     Usage examples and help info updated to reflect changes.
  39. ::
  40. :: Call :DefMirror ["/?"]|[Int]
  41. :: "/?" This help info.
  42. :: Int - Overrides default max string length [ Default = 50 characters per "string" ]
  43. ::
  44. :: Mirror Macro Usage:
  45. :: %Mirror% "string" [/F]
  46. :: %Mirror% "string" "otherstring" [/F]
  47. :: %Mirror% "string","otherstring" [/F]
  48. :: %Mirror% "string", "otherstring" [/F]
  49. :: %Mirror% "string" , "otherstring" [/F]
  50. ::
  51. :: Whole Pattern repetition:
  52. :: Repeat the whole Pattern Int number of times:
  53. :: %Mirror:#=Int% "String","OtherString"
  54. ::
  55. :: Partial Pattern repetition:
  56. :: %Mirror% "string[Int]" or "[Int]string" ; Where Int is a number [1-99]: Repeat mirrored string Int times Horizontally.
  57. :: %Mirror% "string{Int}" or "{Int}string ; Where Int is a number [1-99]: Repeat mirrored string Int times Vertically.
  58. :: ** Repetition features may be combined: %Mirror:#=2% "{Int}string[Int]" or %Mirror:#=2% "[Int]string{Int}"
  59. :: ** Strings beginning / ending with {Int} or [Int] ALWAYS trigger this behaviour If Int is GTR 0 LSS 100.
  60. ::
  61. :: Additional Info:
  62. :: ** Delayed expansion must NOT be enabled prior to calling DefMirror
  63. :: ** Delayed expansion is toggled on/off by %Mirror% macro and is NOT REQUIRED to be enabled prior to expansion the macro.
  64. :: ** Mirror macro does not require echo state to be off, and expands in it's own local environment.
  65. ::    - 'Reverse*CHAR' 'Flip*CHAR' '_Hash' and 'LF' variables are defined in the calling scripts environment.
  66. :: ** Macro args MUST be doublequoted if multiple args are supplied, failure to doublequote will result in
  67. ::    the string being treated as a single arg and may cause unexpected behaviour if the string contains poison characters.
  68. :: ** A temporary file is used to facilitate handling of `*` character, which cannot be handled directly in a for loop.
  69. :: ** Calling :DefMirror with a MaxLength Shorter than a supplied strings length will result in the macro hanging.
  70. :: ** To print a newline using the macro, The string value must contain 2 or more whitespaces: "  "
  71. ::    To print multiple newlines: "{Int}" ; Where Int is number GTR 0 LSS 100
  72. ::
  73. :: Supports all ASCII Characters, Observing the following usage information and ESCAPING Requirements:
  74. ::  - ; Is reserved as a shading character - It will print on the left hand side of the string, and be replaced
  75. ::      with a whitespace on the right.
  76. ::  - @ is reserved for signalling the end of mirroring. Text following @ is printed between mirrored text.
  77. ::      to use @ literally, escape it with itself: @@ [note: doing so will result in all text following @@ being mirrored.]
  78. ::  - To print a doublequote, escape it with itself: "str""ing" will print: str"inggni"rts
  79. ::  - ! must be triple escaped: ^^^!
  80. ::    - /F Switch is incompatable with !, even when escaped.
  81. ::    - Any string containing ^^^! will require literal carets to also be triple escaped:
  82. ::      IE: "^^^^Hello, World^^^! " to print: "^Hello, World!  !dlroW ,olleH^"
  83. ::  - % must be escaped: %%
  84.  
  85.  @Echo("%~1" | findstr /lic:"/?" && @("%__APPDIR__%Findstr.exe" /blic:"::" "%~f0" | more && Exit/b)
  86.  @If "!!"=="" @(Echo(Delayed expansion not permitted prior to macro definition.& @Pause & @Exit/b 1)
  87.  @Set "_Hash=#"
  88. @(Set LF=^
  89.  
  90.  
  91. %= LF Linefeed variable Used in macro. Do not Modify. =%)
  92.  @For %%G in ("(=)" ")=(" "[=]" "]=[" "{=}" "}={" "/=\" "\=/")Do @Set "Replace%%~G"
  93.  @For %%G in ("M=W" "W=M" "6=9" "9=6" "v=^" "^=v")Do @Set "Flip%%~G"
  94.  @Set "_EX=^!"
  95.  @Set "Replace>=<"
  96.  @Set "Replace<=>"
  97.  
  98.  @Setlocal
  99.  @Set "MaxLen=0"
  100.  @For %%G in (%*)Do @Set/A "MaxLen=%%~G" 2> nul || @Set "MaxLen=50"
  101.  @If %MaxLen% LEQ 0 @Set "MaxLen=50"
  102.  
  103. @(Set \n=^^^
  104.  
  105. %= \n Newline variable for macro definition. Do not Modify =%)
  106.  
  107.  @Endlocal & @Set Mirror=@For %%v in (1 2)Do @if %%v==2 (%\n%
  108.   @If Defined Mirror.Input (%\n%
  109.    @Set "[F]="%\n%
  110.    @If /I "!Mirror.Input:~-3!"==" /F" @(%\n%
  111.     @Set "Mirror.Input=!Mirror.Input:~0,-3!"%\n%
  112.     @Set "[F]=true"%\n%
  113.    )%\n%
  114.    @Set "#="%\n: Undefined in local environment to ensure no false positives below =%
  115.    @If not "!_Hash!"=="#" @(Set /A "1 / #" 2^> nul ^&^& (@Set "_Repeat=#") ^|^| @Set "_Repeat=1")Else @Set _Repeat=1%\n%
  116.    @For /l %%T in (1 1 !_Repeat!)Do @(%\n: Repeat entire pattern from args list. For loop input will repeated each input. =%
  117.     @Set "_F="%\n%
  118.     @Set "Mirror.Input=!Mirror.Input:*"="!"%\n: Strip leading characters that precede first doublequote =%
  119.     @Set "Mirror.Input=!Mirror.Input:""=`DQ`!"%\n: Enact temp Substitution of Escaped Doublequotes =%
  120.     @For %%G in ("!LF!")Do @(%\n: Delim args with a newline prior to outputting to file. =%
  121.      @Set "Mirror.Input=!Mirror.Input:" , "=%%G!"%\n%
  122.      @Set "Mirror.Input=!Mirror.Input:", "=%%G!"%\n%
  123.      @Set "Mirror.Input=!Mirror.Input:","=%%G!"%\n%
  124.      @Set "Mirror.Input=!Mirror.Input:" "=%%G!"%\n%
  125.     )%\n%
  126.     ^>"%~dpn0_In.~tmp" (Echo(!Mirror.Input!)%\n: Output prepared args to allow processing of all ASCII characters. =%
  127.     @For /f "usebackq Delims=" %%G in ("%~dpn0_In.~tmp")Do @(%\n: Read and process args line by line =%
  128.      @Set "_P="%\n: nul var used to Flag escaping of @ character =%
  129.      @Set "_S="%\n: nul Arg output var =%
  130.      @Set "_V=%%~G"%\n: Define Arg string =%
  131.      @Set/A"_Y=_X=1"%\n: Define default Horizontal and vertical Arg repeat counts =%
  132.      @Set ^"_V=!_V:"=!"%\n: Strip Doublequotes from Arg =%
  133.     @Rem %\n: Begin Assessment and flagging of [Int] X {Int} Y Partial repetition subargs =%
  134.      @Set /A 1 / !_V:~1,1! 2^> nul ^&^& @If "!_V:~0,1!!_V:~2,1!"=="{}" (%\n%
  135.       @Set "_Y=!_V:~1,1!"%\n%
  136.       @Set "_V=!_V:~3,%MaxLen%!"%\n%
  137.      )Else @If "!_V:~0,1!!_V:~2,1!"=="[]" (%\n%
  138.       @Set "_X=!_V:~-1,1!"%\n%
  139.       @Set "_V=!_V:~1,1!"%\n%
  140.      )%\n%
  141.      @Set /A 1 / !_V:~1,2! 2^> nul ^&^& @If "!_V:~0,1!!_V:~3,1!"=="{}" (%\n%
  142.       @Set "_Y=!_V:~1,2!"%\n%
  143.       @Set "_V=!_V:~4,%MaxLen%!"%\n%
  144.      )Else @If "!_V:~0,1!!_V:~3,1!"=="[]" (%\n%
  145.       @Set "_X=!_V:~1,2!"%\n%
  146.       @Set "_V=!_V:~4,%MaxLen%!"%\n%
  147.      )%\n%
  148.      @Set /A 1 / !_V:~-2,1! 2^> nul ^&^& @If "!_V:~-3,1!!_V:~-1!"=="[]" (%\n%
  149.       @Set "_X=!_V:~-2,1!"%\n%
  150.       @Set "_V=!_V:~0,-3!"%\n%
  151.      )Else @If "!_V:~-3,1!!_V:~-1!"=="{}" (%\n%
  152.       @Set "_Y=!_V:~-2,1!"%\n%
  153.       @Set "_V=!_V:~0,-3!"%\n%
  154.      )%\n%
  155.      @Set /A 1 / !_V:~-3,2! 2^> nul ^&^& @If "!_V:~-4,1!!_V:~-1!"=="[]" (%\n%
  156.       @Set "_X=!_V:~-3,2!"%\n%
  157.       @Set "_V=!_V:~0,-4!"%\n%
  158.      )Else @If "!_V:~-4,1!!_V:~-1!"=="{}" (%\n%
  159.       @Set "_Y=!_V:~-3,2!"%\n%
  160.       @Set "_V=!_V:~0,-4!"%\n%
  161.      )%\n: Next Line enacts escaping of @@ or splitting of Mirror / nonmirrored substrings =%
  162.      @If "!_V:@@=!"=="!_V!" (@For /f "tokens=1,2 Delims=@" %%G in ("!_V!")Do @Set "_V=%%~G"^&@set "_P=%%~H")Else @If defined _V @set "_V=!_V:@@=@!"%\n%
  163.      @If defined _V @Set ^"_V=!_V:`DQ`="!"%\n: enacts substitution to reinsert doublequotes =%
  164.      @Set "_I="%\n: nul string length variables =%
  165.      @Set "_Pi="%\n: Next line gets full string length =%
  166.      @For /l %%i in (0 1 %MaxLen%)Do @If not defined _I @If "!_V:~%%i!"=="" @Set/A"_I=%%i,_O=%%i-2" 2^> nul%\n%
  167.      @If defined _P (%\n: Get offset length if @ used to split substring =%
  168.       @For /l %%i in (0 1 %MaxLen%)Do @If not defined _Pi @If "!_P:~%%i!"=="" @Set/A"_Pi=%%i"%\n%
  169.       @Set /A _O=_Pi-_O%\n%
  170.      )%\n%
  171.      @If Defined _V @For /l %%i in (0 1 !_I!)Do @Set "_S=!_S!!_V:~%%i,1!"%\n: Build Left half of output string =%
  172.      @Set "_S=!_S!!_P!"%\n: Append unmirrored component if defined =%
  173.      @For /l %%i in (!_O! -1 0)Do @(%\n: Append mirror of string enacting replacements =%
  174.       @If "!_V:~%%i,1!"==";" @Set "_S=!_S! "%\n%
  175.       @For /f "delims=" %%v in ("!_V:~%%i,1!")Do @(If not "!Replace%%v!"=="" @(Set "_S=!_S!!Replace%%v!")Else @Set "_S=!_S!!_V:~%%i,1!")%\n%
  176.      )%\n%
  177.      @Set "_M="%\n: nul _M var used to join horizontal repetitions =%
  178.      @For /l %%i in (1 1 !_X!)Do @Set "_M=!_M!!_S!"%\n: append horizontal repetitions =%
  179.      @If Defined _M @Set "_S=!_M!"%\n: Swap out appended string if defined =%
  180.      @Set "_Fc="%\n%
  181.      @If Defined [F] @For /l %%i in (0 1 %MaxLen%)Do @If not "!_S:~%%i,1!"=="" @(%\n: define vertical mirror of substring enacting replacements =%
  182.       @For /f "delims=" %%v in ("!_S:~%%i,1!")Do @(If not "!Replace%%v!"=="" @(Set "_Fc=!_Fc!!Replace%%v!")Else @If not "!Flip%%v!"=="" @(Set "_Fc=!_Fc!!Flip%%v!")Else (@Set "_Fc=!_Fc!!_S:~%%i,1!"))%\n%
  183.      )%\n%
  184.      @For /l %%i in (1 1 !_Y!)Do @(%\n%
  185.       @Echo(!_S!%\n: repeat string vertically =%
  186.      )%\n%
  187.      @If Defined [F] @(@If not Defined _F @(@Set "_F=!_Fc!")Else @Set "_F=!_Fc!!LF!!_F!")%\n: Append vertical Mirror =%
  188.     )%\n%
  189.     @If Defined [F] @Echo(!_F!%\n%
  190.     @Del "%~dpn0_In.~tmp" 2^> nul%\n%
  191.    )%\n: remove the temp file used. =%
  192.    @Endlocal%\n: Ends the local environment if args supplied. =%
  193.   )Else @Endlocal%\n: Ends the local environment if no args supplied to macro. =%
  194.  )Else @Setlocal ENableDelayedExpansion ^& @Set Mirror.Input=
  195. @Exit /b 0
Add Comment
Please, Sign In to add comment