How to center align text.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: How to center align text.

#16 Post by Thor » 12 Mar 2017 01:20

Thanks Jer for the clarification and added the "indent" feature.
The other scripts from aGerman, Sounak@9434, einstein1969, Aacini, ShadowThief all work fine.
It's amazed me to see lots of methods to do the same thing, which is to display centered text on screen.
It reminds me about an old saying: "All roads lead to Rome!" :mrgreen:

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: How to center align text.

#17 Post by Aacini » 12 Mar 2017 13:54

Well, when there are several programs that do the same thing, the logical next step is compare they all (and post the result). The simplest and obvious comparison is the time they takes to process a large file. Don't you think?

Antonio

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: How to center align text.

#18 Post by Thor » 12 Mar 2017 17:37

This is the result I've got when running the script to display a 1500 lines of text.
This is to arrange in fastest time first order.

"Aacini"

Start time: 16:12:08.28
End time: 16:12:08.97
Difference: 0.69

===========================
"aGerman"

Start time: 16:12:05.96
End time: 16:12:08.14
Difference: 2.18

===========================
"Sounak@9434"

Start time: 16:26:02.49
End time: 16:26:05.03
Difference: 2.54

===========================
"Jer without Indent"

Start time: 16:12:03.06
End time: 16:12:05.83
Difference: 2.77

===========================
"einstein1969"

Start time: 16:11:57.46
End time: 16:12:02.92
Difference: 5.46

===========================
"ShadowThief"
Start time: 16:11:48.71
End time: 16:11:57.32
Difference: 8.61
Attachments
Poem_1500.zip
(774 Bytes) Downloaded 104 times

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: How to center align text.

#19 Post by Sounak@9434 » 12 Mar 2017 21:34

I'm at the third position at least. :D

I'm not surprised with the Aacini's result. His scripts are usually the fastest I've seen. I even had not found any use of his discovery until I read his script. I'm going to update my alignment utility(again) with his script.

@Aacini: Maybe help me about the right side alignment? Thanks in advance.

Sounak

ShadowThief
Expert
Posts: 952
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to center align text.

#20 Post by ShadowThief » 12 Mar 2017 22:13

Sounak@9434 wrote:I'm at the third position at least. :D

I'm not surprised with the Aacini's result. His scripts are usually the fastest I've seen. I even had not found any use of his discovery until I read his script. I'm going to update my alignment utility(again) with his script.

@Aacini: Maybe help me about the right side alignment? Thanks in advance.

Sounak

Yeah, I'm not even remotely surprised that my script came in last; it has to make a temporary file for every single line. Heck, I'm surprised that it managed to finish in 8 seconds!

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: How to center align text.

#21 Post by fugitive » 12 Mar 2017 22:35

I did not expect that you are so interested in this issue, once again,I expressed gratitude to everyone.
And I learned from Sounak@9434 & einstein1969 a wonderful idea.Change a little as follows:


Code: Select all

@echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (poem.txt) do (set "s=%%a"
      for /l %%b in (1,1,40) do (if "!s:~78!" equ "" set "s= !s! ")
      echo;!s:~,79!
)
pause



Maybe I should find more questions like this one,let everyone of us answer together, just like playing games. :) :) :)
Last edited by fugitive on 13 Mar 2017 00:10, edited 1 time in total.

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: How to center align text.

#22 Post by Sounak@9434 » 12 Mar 2017 23:05

fugitive wrote:I did not expect that you are so interested in this issue, once again,I expressed gratitude to everyone.
And I learned from Sounak@9434 a wonderful idea.Change a little as follows:


Code: Select all

@echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (poem.txt) do (set "s=%%a"
      for /l %%b in (1,1,40) do (if "!s:~78!" equ "" set "s= !s! ")
      echo;!s:~,79!
)
pause


Edit your post fugitive, that method is shown by einstein1969, not me.
Or are you saying it because of this line which you edited?

Code: Select all

for /f "tokens=*" %%a in (filename.txt) do call align.bat /m "%%a"


[Offtopic]
By the way, is the poem written by you fugitive?
[/Offtopic]

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: How to center align text.

#23 Post by fugitive » 13 Mar 2017 00:24

Sounak@9434 wrote:Edit your post fugitive, that method is shown by einstein1969, not me.
[Offtopic]
By the way, is the poem written by you fugitive?
[/Offtopic]


Thanks for reminding. haha……I learned from both of you
“Tokens” ignore space character in front of line,and einstein 1969 's code does not need to calculate the length.I am unfamiliar with the forum, if there are any errors, please forgive me. 8)
In the end, this poem is just an example which I found online.

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: How to center align text.

#24 Post by Aacini » 13 Mar 2017 10:05

Sounak@9434 wrote:I'm not surprised with the Aacini's result. His scripts are usually the fastest I've seen. I even had not found any use of his discovery until I read his script. I'm going to update my alignment utility(again) with his script.

@Sounak@9434, please note that if a character is a digit or an operator the SET /A command process it, so such characters (and also others that may cause errors in the arithmetic expression) must be replaced before use this method. I am still doing tests to check which characters may cause errors in SET /A command, so this trick can NOT be used as a general method to get string lengths (yet).


ShadowThief wrote:Yeah, I'm not even remotely surprised that my script came in last; it has to make a temporary file for every single line. Heck, I'm surprised that it managed to finish in 8 seconds!

After I read this phrase I thought that this method would be faster if the lengths of all lines in the file could be obtained in just one operation. Then, I realized that the /O switch in FINDSTR command allows to get precisely this result! :shock: :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "file=poem.txt"

for /F "skip=4 tokens=2" %%a in ('mode con') do set /A "cols=%%a-1" & goto continue
:continue
set "spaces="
for /L %%i in (0,2,%cols%) do set "spaces=!spaces! "

set "str="
set "last=0"
for /F "tokens=1* delims=:" %%a in ('findstr /O "^" poem.txt') do (
   set /A "indent=(cols-(%%a-last-2))/2, last=%%a"
   if !indent! lss 0 set "indent=0"
   if defined str (
      for /F %%n in ("!indent!") do set "str=!spaces:~0,%%n!!str!"
      echo !str:~0,%cols%!
   )
   set "str=%%b"
)

rem Last line
for %%a in (poem.txt) do set /A "indent=(cols-(%%~Za-last-2))/2"
if %indent% lss 0 set "indent=0"
set "str=!spaces:~0,%indent%!!str!"
echo !str:~0,%cols%!

I wonder how fast is this method when compared vs. the other ones...

Antonio

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: How to center align text.

#25 Post by Thor » 13 Mar 2017 11:21

"Aacini 1" (Your previous method)

Start time: 10:17:44.05
End time: 10:17:44.73
Difference: 0.68

==========================
"Aacini 2" (Your latest method)

Start time: 10:17:44.86
End time: 10:17:45.26
Difference: 0.40

I'm very impressed with your latest discovery. :D

ShadowThief
Expert
Posts: 952
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to center align text.

#26 Post by ShadowThief » 13 Mar 2017 16:44

Woo, I'm a muse!

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: How to center align text.

#27 Post by Sounak@9434 » 13 Mar 2017 22:59

Aacini wrote:
Sounak@9434 wrote:I'm not surprised with the Aacini's result. His scripts are usually the fastest I've seen. I even had not found any use of his discovery until I read his script. I'm going to update my alignment utility(again) with his script.

@Sounak@9434, please note that if a character is a digit or an operator the SET /A command process it, so such characters (and also others that may cause errors in the arithmetic expression) must be replaced before use this method. I am still doing tests to check which characters may cause errors in SET /A command, so this trick can NOT be used as a general method to get string lengths (yet).


ShadowThief wrote:Yeah, I'm not even remotely surprised that my script came in last; it has to make a temporary file for every single line. Heck, I'm surprised that it managed to finish in 8 seconds!

After I read this phrase I thought that this method would be faster if the lengths of all lines in the file could be obtained in just one operation. Then, I realized that the /O switch in FINDSTR command allows to get precisely this result! :shock: :D

Code: Select all

::SKIPPED

I wonder how fast is this method when compared vs. the other ones...

Antonio


There is at least one bug Aacini which is if the line has more length than '%cols%-1' the remaining part is removed.

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: How to center align text.

#28 Post by Aacini » 13 Mar 2017 23:49

Sounak@9434 wrote:There is at least one bug Aacini which is if the line has more length than '%cols%-1' the remaining part is removed.

Yes, exactly as shown in the original request:

fugitive wrote:——————————now,I want it to be this way in the cmd (align center ?)

Code: Select all

                                When Day Is Done                               
                              If the day is done ,                             
                            If birds sing no more .                           
                        If the wind has fiagged tired ,                       
                 Then draw the veil of darkness thick upon me ,               
Even as thou hast wrapt the earth with The coverlet of sleep and tenderly close
                   The petals of the drooping lotus at dusk.                   
                               From the traverer,                             
         Whose sack of provisions is empty before the voyage is ended ,       
                     Whose garment is torn and dust-laden ,                   
             Whose strength is exhausted,remove shame and poverty ,           
                     And renew his life like a flower under                   
                        The cover of thy kindly night .                       


If you compare the longest line, the one that start with "Even as thou...", you'll realize that the output of my program is exactly the same as the requested one, where the final "d ," part is removed from the line...

Antonio

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: How to center align text.

#29 Post by Sounak@9434 » 14 Mar 2017 01:47

Aacini wrote:
Sounak@9434 wrote:There is at least one bug Aacini which is if the line has more length than '%cols%-1' the remaining part is removed.

Yes, exactly as shown in the original request:

fugitive wrote:——————————now,I want it to be this way in the cmd (align center ?)

Code: Select all

::SKIPPED


If you compare the longest line, the one that start with "Even as thou...", you'll realize that the output of my program is exactly the same as the requested one, where the final "d ," part is removed from the line...

Antonio

Any workaround Antonio?

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: How to center align text.

#30 Post by Aacini » 14 Mar 2017 11:49

Sounak@9434 wrote:
Aacini wrote:If you compare the longest line, the one that start with "Even as thou...", you'll realize that the output of my program is exactly the same as the requested one, where the final "d ," part is removed from the line...

Antonio

Any workaround Antonio?


I am confused by your question... If you are asking if "is there a way to delete such a feature from the program", then you just need to remove the part of the code that implement it! 8) Hint: such a feature is implemented in this line:

Code: Select all

echo !str:~0,%cols%!

Antonio

Post Reply