T3RRYT3RR0R

Batch mastery of Console output and properties

Apr 9th, 2020
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.19 KB | None | 0 0
  1. ::: Batch console property mastery for windows 10
  2. ::: Resize & Position the Console at (top left) or centre
  3. ::: *!UPDATE!* 18th april 2020 Console centering algorithm adjusted to correctly centre for all resolutions.
  4. ::: Position text at Left / Centre / Right of console
  5. ::: Color text
  6.  
  7. @Echo Off & CD "%~dp0"
  8.    
  9.     Set "AlignFile=%~dpnx0"
  10.  
  11.     Setlocal DisableDelayedExpansion
  12.  
  13.     (Set LF=^
  14.  
  15.  
  16.     %= NewLine =%)
  17.  
  18.     Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  19.  
  20. %= Define console width and values for text alignment =%
  21.  
  22.     Set @Align_Centre=Set /A "%%H=(Console_Width / 2) - (Len / 2)"
  23.     Set @Align_Right=Set /A "%%H=(Console_Width - Len)"
  24.     Set @Align_Left=Set /A "%%H=0"
  25.  
  26. %= @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation =%
  27. %= Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console. =%
  28.  
  29.     Set @Align=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  30.         For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
  31.             If not "!%%~G!"=="" (Set "TextOut=!%%~G!") Else (Set "TextOut=%%~G")%\n%
  32.             Set LenTrim=Start%\n%
  33.             For /L %%a in (1,1,!Console_Width!) Do (%\n%
  34.                 IF NOT "!LenTrim!"=="" (%\n%
  35.                     Set LenTrim=!TextOut:~0,-%%a!%\n%
  36.                     If "!LenTrim!"=="" Set "Len=%%a"%\n%
  37.                 ) %\n%
  38.             ) %\n%
  39.             IF /I "%%H"=="C" %@Align_Centre% %\n%
  40.             IF /I "%%H"=="R" %@Align_Right% %\n%
  41.             IF /I "%%H"=="L" %@Align_Left% %\n%
  42.             For /L %%# in (1,1,!%%H!) Do Set "TextOut= !TextOut!" %\n%
  43.             Echo(!Color!!TextOut!!white!^&^& Endlocal %\n%
  44.         ) %\n%
  45.     ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  46.  
  47. REM Color Macro Variables
  48.     Set "@Color=Call :Color "
  49.     Setlocal EnableDelayedExpansion
  50.     Set /A Red=31,Green=32,Yellow=33,dark.blue=34,Purple=35,light.Blue=36,White=0,Grey=90,Pink=91,Beige=93,Aqua=94,Magenta=95,Teal=96
  51.     For %%A in (Red,Green,Yellow,dark.blue,Purple,light.Blue,White,Grey,Pink,Beige,Aqua,Magenta,Teal) do Call Set "%%A=[!%%A!m"
  52.     Setlocal DisableDelayedExpansion
  53.     Set "@Hold=Call :ColorLetters "Next." & Echo. & Pause>nul"
  54.  
  55.     If Not "%~3"=="" (
  56.         Set "AlignFile=%~3"
  57.         Set "Console_Width=%~2"
  58.         Goto :%~1
  59.     ) Else (Goto :main)
  60.  
  61. %= Subroutine to process output of wmic command into usable variables  for screen dimensions (resolution) =%
  62.  
  63.     :ChangeConsole <Lines> <Columns> <Label to Resume From> <If a 4th parameter is Defined, Aligns screen at top left>
  64.     Setlocal EnableDelayedExpansion
  65. %= Get screen Dimensions =%
  66.     For /f "delims=" %%# in  ('"wmic path Win32_VideoController  get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
  67.         Set "%%#">nul
  68.     )
  69.  
  70. %= Calculation of X axis relative to screen resolution and console size. Resolution scales to Max Columns - 170 =%
  71.     Set /A XresScale=CurrentHorizontalResolution / 170
  72.     Set /A HorzCentre=CurrentHorizontalResolution / 2
  73.     Set /A CentreX= HorzCentre - ( ( %~2 * XresScale ) / 2 )
  74. %= Calculation of Y axis relative to screen resolution and console size. Resolution scales to Max Lines - 40 =%
  75.     Set /A YresScale= CurrentVerticalResolution / 40
  76.     Set /A VertCentre=CurrentVerticalResolution / 2
  77.     Set /A CentreY= VertCentre - ( ( %~1 * YresScale ) / 2 )
  78.  
  79. %= Optional 4th parameter can be used to align console at top left of screen instead of screen centre =%
  80.     If Not "%~4"=="" (Set /A CentreY=0,CentreX=-8)
  81.  
  82. %= .Vbs script creation and launch to reopen batch with new console settings, combines with =%
  83.     Set "Console_Width=%~2"
  84.  
  85. %= Creates a batch file to reopen the main script using Call with parameters to define properties for console change and the label to resume from =%
  86.         (
  87.         Echo.@Mode Con: lines=%~1 cols=%~2
  88.         Echo.@Title Res: %CurrentHorizontalResolution%x%CurrentVerticalResolution% X,Y Pos: %CentreX%,%CentreY% Con Size: Cols = %~2 Lines = %~1
  89.         Echo.@Call "%AlignFile%" "%~3" "%~2" "%AlignFile%"
  90.         )>"%temp%\ChangeConsole.bat"
  91.  
  92.         (
  93.         Echo.Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
  94.         Echo.Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
  95.         Echo.objConfig.SpawnInstance_
  96.         Echo.objConfig.X = %CentreX%
  97.         Echo.objConfig.Y = %CentreY%
  98.         Echo.Set objNewProcess = objWMIService.Get^("Win32_Process"^)
  99.         Echo.intReturn = objNewProcess.Create^("%temp%\ChangeConsole.bat", Null, objConfig, intProcessID^)
  100.         )>"%temp%\Consolepos.vbs"
  101.  
  102. %= Starts the companion batch script to Change Console properties, ends the parent =%
  103.     Start "" "%temp%\Consolepos.vbs" & Exit
  104.  
  105. :Color
  106.     Setlocal EnableDelayedExpansion
  107.     Set "Color=!%~1!"
  108.     ( Endlocal & Set "Color=%Color%" )
  109.     Exit /B
  110.  
  111. :Colorwords
  112.     Setlocal EnableDelayedExpansion
  113.     Set #A=31
  114.     For %%A in (%*) do (
  115.         Set "Word=%%~A"
  116.         Call :ColorPrint "!Word!"
  117.         <nul set /p=.
  118.     )
  119.     Endlocal
  120. Exit /B
  121.  
  122. :ColorLetters
  123.     Setlocal EnableDelayedExpansion
  124.     Set #A=31
  125.     For %%A in (%*) do (
  126.         Set "Word=%%~A"
  127.         For %%B In (a b c d e f g h i j k l m n o p q r s t u v w x y z . [ ] ) do Set "Word=!Word:%%~B=%%~B !
  128.         Call :ColorPrint "!Word!"
  129.         <nul set /p=.
  130.     )
  131.     Endlocal
  132. Exit /B
  133.  
  134. :ColorPrint
  135. For %%C in (%~1) do (
  136.     <nul set /p=[!#A!m%%~C
  137.     Set /A #A+=1
  138.     IF "!#A!"=="37" (Set #A=31)
  139. )
  140. Exit /B
  141.  
  142. :main
  143. %= Remainder of Script examples the usage of Subroutines and macro's =%
  144.  
  145.  
  146. %= If a 4rd parameter is used, Console will be positioned at top left of screen =%
  147.  
  148.     Call :ChangeConsole 50 50 Display_Text_1 top
  149.    
  150. :Display_Text_1
  151.    
  152.     %@Color% red & For %%B in ("Show this" "in centre") do Set "Text=%%~B" & %@Align% Text C
  153.     %@hold%
  154.     %@Color% green & For %%B in ("Show this" "on right") do Set "Text=%%~B" & %@Align% Text R
  155.     %@hold%
  156.     %@Color% light.blue & For %%B in ("Show this" "on left") do Set "Text=%%~B" & %@Align% Text L
  157.     %@hold%
  158.  
  159.     Call :ChangeConsole 40 150 Display_Text_2
  160.    
  161. :Display_Text_2
  162.  
  163.     %@Color% pink & Set "string=<< %%A Left String%% \" & %@Align% string L
  164.  
  165.     %@hold%
  166.  
  167.     Call :ChangeConsole 30 175 Display_Text_3
  168.    
  169. :Display_Text_3
  170.  
  171.     %@Color% teal & Set "string=|^ A Centred String ^|" & %@Align% string C
  172.  
  173.     %@hold%
  174.  
  175.     Call :ChangeConsole 20 30 Display_Text_4
  176.    
  177. :Display_Text_4
  178.  
  179.     %@Color% magenta & Set "string=/ A !Right String!>>" & %@Align% string R
  180.  
  181.     %@hold%
  182.  
  183.     (taskkill /pid WScript.exe /f /t) >nul 2>nul
  184.     Timeout 1 >nul
  185.     Del /F "%temp%\Consolepos.vbs" >nul 2>nul
  186.     Del /F "%temp%\ChangeConsole.bat" >nul 2>nul
  187.     exit /b
Advertisement
Add Comment
Please, Sign In to add comment