Advertisement
T3RRYT3RR0R

Batch Macro Align text Centre Right or Left

Apr 6th, 2020
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.18 KB | None | 0 0
  1. ::: v1.0 macro for use within a batch file REM: More efficient
  2. @Echo Off & Title Batch text alignment macro.
  3.  
  4. %= Outputs strings assigned to variables to the console at the chosen alignment position =%
  5.  
  6. %= From my Answer here: https://stackoverflow.com/a/61046030/12343998 =%
  7.  
  8.     Setlocal DisableDelayedExpansion
  9.  
  10.     (Set LF=^
  11.  
  12.  
  13.     %= NewLine =%)
  14.  
  15.     Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  16.  
  17. %= Define console width and values for alignment =%
  18.  
  19.     For /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set "Console_Width=%%W"
  20.     Set /A Console_Width+=0
  21.     IF %Console_Width% GTR 160 Set "Console_Width=160"
  22.  
  23. %= SubMacros to Facilitate Arithmetic operations during Macro Execution =%
  24.     Set Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
  25.     Set Align_Right=Set /A "%%H=(Console_Width - Len)"
  26.     Set Align_Left=Set /A "%%H=0"
  27.  
  28. %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
  29. %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
  30.  
  31.     Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  32.         For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
  33.             If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
  34.             Set LenTrim=Start%\n%
  35.             For /L %%a in (1,1,160) Do (%\n%
  36.                 IF NOT "!LenTrim!"=="" (%\n%
  37.                     Set LenTrim=!TextOut:~0,-%%a!%\n%
  38.                     If "!LenTrim!"=="" Set "Len=%%a"%\n%
  39.                 ) %\n%
  40.             ) %\n%
  41.             IF /I "%%H"=="@C" %Align_Centre% %\n%
  42.             IF /I "%%H"=="@R" %Align_Right% %\n%
  43.             IF /I "%%H"=="@L" %Align_Left% %\n%
  44.             For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
  45.             Echo(!TextOut!^&^& Endlocal %\n%
  46.         ) %\n%
  47.     ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  48.  
  49. %= Script Break. below lines are to demonstrate Usage only =%
  50.     For %%B in (Show this in centre) do Set "Text=%%B" & %@Align% Text @C
  51.     For %%B in (Show this on right) do Set "Text=%%B" & %@Align% Text @R
  52.     For %%B in (Show this on left) do Set "Text=%%B" & %@Align% Text @L
  53.     Set "string=This Left String" & %@Align% string @L
  54.     Set "string=This Centre String" & %@Align% string @C
  55.     Set "string=This Right String" & %@Align% string @R
  56.  
  57.     Pause
  58.     Exit /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement