T3RRYT3RR0R

Batch centre console text stdout

Apr 5th, 2020
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.45 KB | None | 0 0
  1. ::: Script
  2.  
  3. @Echo Off & REM centre.bat
  4.     Setlocal DisableDelayedExpansion
  5.  
  6.     (Set LF=^
  7.  
  8.  
  9.     %= NewLine =%)
  10.  
  11.     Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  12.  
  13.     Endlocal & Set @strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  14.         For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
  15.             Set "tmpLen=!%%~G!"%\n%
  16.             Set LenTrim=Start%\n%
  17.             For /L %%a in (1,1,160) Do (%\n%
  18.                 IF NOT "!LenTrim!"=="" (%\n%
  19.                     Set LenTrim=!tmpLen:~0,-%%a!%\n%
  20.                     If "!LenTrim!"=="" Endlocal ^& Set %%H=%%a%\n%
  21.                 )%\n%
  22.             ) %\n%
  23.         ) %\n%
  24.     ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  25.  
  26.     IF "%~1"=="" (
  27.         Setlocal EnableDelayedExpansion
  28.         Echo(Must Call with a quoted string.
  29.         Timeout 3 >nul
  30.         Endlocal
  31.         Exit /B
  32.     )
  33.  
  34.     Setlocal EnableDelayedExpansion
  35.     Set "TextOut=%~1"
  36.     %@strLen% TextOut Len
  37.     For /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set "Console_Width=%%W"
  38.     IF !Console_Width! GTR 160 (Set Console_Width=160)
  39.     Set /A Len/=2
  40.     Set /A Console_Width+=0
  41.     Set /A Column=(Console_Width / 2) - Len
  42.     For /L %%A in (1,1,!Column!) Do Set "TextOut= !TextOut!"
  43.     Echo(!TextOut!
  44.  
  45.     (
  46.     Endlocal
  47.     Exit /B
  48.     )
  49.  
  50. ::: Comments
  51.  
  52. REM An example of a call to centre.bat (from the command line):
  53.  
  54. Cls & @for %a in (1. 2. 3. 4. 5.) do @for %b in (this is an example of usage) do @Call centre.bat "%a %b"
  55.  
  56. REM With a small change to the calculation of column relative to console width and string length,
  57. REM this could be used to Right-Align text
Advertisement
Add Comment
Please, Sign In to add comment