Advertisement
T3RRYT3RR0R

Batch StdOut output alignment Macro v2.0

Apr 6th, 2020
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.79 KB | None | 0 0
  1. ::: v2.0 Called as subroutine or .Bat file
  2. ::: strings must be assigned to variables using the Set "var=string" Syntax to display problem characters
  3. ::: Not built for multiline variables. Only the first line of Multiline variables will be aligned.
  4. ::: console widths greater than 170 columns will require modification of this script
  5.  
  6. @Echo Off
  7.     Setlocal DisableDelayedExpansion
  8.  
  9.     (Set LF=^
  10.  
  11.  
  12.     %= NewLine =%)
  13.  
  14.     Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  15.  
  16. %= Define console width and values for alignment =%
  17.  
  18.     For /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set "Console_Width=%%W"
  19.     Set /A Console_Width+=0
  20.     IF %Console_Width% GTR 170 Set "Console_Width=170"
  21.     Set Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
  22.     Set Align_Right=Set /A "%%H=(Console_Width - Len)"
  23.     Set Align_Left=Set /A "%%H=0"
  24.  
  25. %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
  26. %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
  27.  
  28.     Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  29.         For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
  30.             If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
  31.             Set LenTrim=Start%\n%
  32.             For /L %%a in (1,1,170) Do (%\n%
  33.                 IF NOT "!LenTrim!"=="" (%\n%
  34.                     Set LenTrim=!TextOut:~0,-%%a!%\n%
  35.                     If "!LenTrim!"=="" Set "Len=%%a"%\n%
  36.                 ) %\n%
  37.             ) %\n%
  38.             IF /I "%%H"=="C" %Align_Centre% %\n%
  39.             IF /I "%%H"=="R" %Align_Right% %\n%
  40.             IF /I "%%H"=="L" %Align_Left% %\n%
  41.             For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
  42.             Echo(!TextOut!^&^& Endlocal %\n%
  43.         ) %\n%
  44.     ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  45.  
  46.     :Align <varName> <Switch L C R>
  47.     %@Align% %~1 %~2
  48.     Endlocal
  49.     Goto :Eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement