Page 1 of 1

center text with echo

Posted: 17 Oct 2013 08:08
by pumi
Hello,

how can I center a text with echo in a batch?
Can you give me a hint please?

Thank in advance for help and
greetings pumi

Re: center text with echo

Posted: 17 Oct 2013 08:27
by ShadowThief
First, figure out how wide your terminal window is. The default is 80 characters, but yours might be different.
Next, find the length of the string you wish to center. There are some string length calculation functions on this site, I assume you know how to search.
Then subtract the length of the string from the width of the window and divide that number by two, rounding down to the nearest whole number.
Finally, add that many spaces to the start of your string and print it.

Re: center text with echo

Posted: 17 Oct 2013 08:51
by npocmaka_

Code: Select all

@echo off

set "string=blahblahblahblahblbllahblahblahblahblahblbllah"
call  :strlen0.3 string len
for /f "tokens=1,2 delims=: " %%a in ('mode^| find "Columns"') do @set cols=%%b
set /a start_from=cols/2-len/2
setlocal enableDelayedExpansion

for /l %%S in (1,1,!start_from!) do (
   set "string= !string!"
)
endlocal & set string=%string%

echo %string%

goto :eof
:strlen0.3  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%A in (2187 729 243 81 27 9 3 1) do (
   set /A mod=2*%%A
   for %%Z in (!mod!) do (
      if "!s:~%%Z,1!" neq "" (
         set /a "len+=%%Z"
         set "s=!s:~%%Z!"
         
      ) else (
         if "!s:~%%A,1!" neq "" (
            set /a "len+=%%A"
            set "s=!s:~%%A!"
         )
      )
   )
  )
  endlocal & if "%~2" neq "" (set %~2=%len%) else echo **%len%**
exit /b

Re: center text with echo

Posted: 17 Oct 2013 09:44
by einstein1969
or more simple, need more test:

Code: Select all

@echo off & setlocal enableDelayedExpansion

set /a size=80-1 & rem screen size minus one

set s=b
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=bla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!


result:

Code: Select all

                                        b
                                       bla
                                    blablabla
                                  blablablabla
                                 blablablablabla
                               blablablablablabla


EDIT: Correct for better centering and minor iteration (div 2)

Einstein1969

Re: center text with echo

Posted: 18 Oct 2013 01:36
by pumi
thanks for your help guys.
I'll take the second one.
Maybe it's not nessesary to take the size as count.
The half size as count should work.
I'll try that...

btw: what do the "(" in "echo(!s!" ??
curiously there is no error in syntax but should be...!?

Re: center text with echo

Posted: 18 Oct 2013 04:07
by einstein1969
pumi wrote:thanks for your help guys.
I'll take the second one.
Maybe it's not nessesary to take the size as count.
The half size as count should work.
I'll try that...

btw: what do the "(" in "echo(!s!" ??
curiously there is no error in syntax but should be...!?


I have learned here, by jeb, to use this synthase that allows you to do the echo of empty variables. Otherwise it would give a message echo enabled / disabled.
It seems that the character "(" is the only one that is safe can be used anywhere without syntax errors.

"echo," instead of "echo." or "echo\"

PS. I have correct the previus code.
Einstein1969

Re: center text with echo

Posted: 18 Oct 2013 14:05
by pumi
thanks!