Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Matt Williamson
- Posts: 82
- Joined: 30 Dec 2013 10:16
- Location: United States by the big waterfall
#1
Post
by Matt Williamson » 16 Apr 2014 08:11
This should be relatively easy but my brain isn't working well today. I borrowed some code from Saso's progressbar script and I'm using it to draw a box on the screen. That was easy enough once I figured out how it worked. Now, I want to add some text into the box and center it inside. It's almost working but I can't get the line drawing character off the end of my text. Here is the code:
Code: Select all
@echo off
setlocal
set "length=77"
set spaces= &rem
set "string=This is a test of centering text in a box and more now"
set "strng2=%spaces%"
call :strlen len string
set /a start_from=length/2-len/2
setlocal enableDelayedExpansion
set "strng3=!strng2:~0,%length%!"
for /l %%S in (1,1,!start_from!) do (
set "string= !string!"
)
endlocal & set string=%string%
call :createbinvalue 218
call :createbinvalue 196
call :createbinvalue 191
call :createbinvalue 179
call :createbinvalue 192
call :createbinvalue 217
call :createbinvalue 219
echo 1 196 %chr196%
echo 2 191 %chr191%
echo 3 218 %chr218%
echo 4 179 %chr179%
echo 5 192 %chr192%
echo 6 217 %chr217%
echo 7 219 %chr219%
set lines=%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%
set line1=%chr218%%lines%%lines%%lines%%lines%%lines%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr191%
set line2=%chr179%%strng3%%chr179%
set line3=%chr179%%string%%chr179%
set line4=%chr179%%strng3%%chr179%
set line5=%chr192%%lines%%lines%%lines%%lines%%lines%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr217%
setlocal enabledelayedexpansion
for /l %%a in (1,1,5) do echo !line%%a!
endlocal
goto :eof
:createbinvalue
set par1=%1
type nul > t.tmp
makecab /d compress=off /d reserveperdatablocksize=26 /d reserveperfoldersize=%par1% t.tmp %par1%.chr > nul
type %par1%.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
>nul copy /y temp.tmp /a %par1%.chr /b
del t.tmp temp.tmp
for /F "delims=" %%a in (%par1%.chr) do set "chr%par1%=%%a"
del %par1%.chr
goto :EOF
:strlen <resultVar> <stringVar>
setlocal EnableDelayedExpansion
set "s=!%~2!#" & set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" & set "s=!s:~%%P!" ))
endlocal &set "%~1=%len%"
exit /b
-
einstein1969
- Expert
- Posts: 976
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
#2
Post
by einstein1969 » 16 Apr 2014 08:33
hi,
i don't know if
this can help you to reduce size of code.
center a string without use strlen function
Code: Select all
@echo off & setlocal EnableDelayedExpansion
:: size of box or windows
set sizemax=70
set s=blablablablablabla
for /L %%# in (1,2,!sizemax!) do if "!s:~%sizemax%,1!" == "" set "s= !s! "
set s=!s:~1,%sizemax%!& echo(^|!s!^|
EDIT: correct an error.
einstein1969
-
Matt Williamson
- Posts: 82
- Joined: 30 Dec 2013 10:16
- Location: United States by the big waterfall
#3
Post
by Matt Williamson » 16 Apr 2014 11:11
Thank you for your response. Your simple example works just fine but doesn't in my script. Here is what I changed.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set sizemax=70
set s=This is a test of centering text in a box and more now
call :createbinvalue 218
call :createbinvalue 196
call :createbinvalue 191
call :createbinvalue 179
call :createbinvalue 192
call :createbinvalue 217
call :createbinvalue 219
echo 1 196 %chr196%
echo 2 191 %chr191%
echo 3 218 %chr218%
echo 4 179 %chr179%
echo 5 192 %chr192%
echo 6 217 %chr217%
echo 7 219 %chr219%
for /L %%# in (1,2,!sizemax!) do if "!s:~%sizemax%,1!" == "" set "s= !s! "
set s=!s:~1,%sizemax%!
set lines=%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%
set line1=%chr218%%lines%%lines%%lines%%lines%%lines%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr191%
set line2=%chr179%%chr179%
set line3=%chr179%!s!%chr179%
set line4=%chr179%%chr179%
set line5=%chr192%%lines%%lines%%lines%%lines%%lines%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr196%%chr217%
setlocal enabledelayedexpansion
for /l %%a in (1,1,5) do echo !line%%a!
endlocal
goto :eof
:createbinvalue
set par1=%1
type nul > t.tmp
makecab /d compress=off /d reserveperdatablocksize=26 /d reserveperfoldersize=%par1% t.tmp %par1%.chr > nul
type %par1%.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
>nul copy /y temp.tmp /a %par1%.chr /b
del t.tmp temp.tmp
for /F "delims=" %%a in (%par1%.chr) do set "chr%par1%=%%a"
del %par1%.chr
goto :EOF
The padding for the line2, 3, and 4 variables is off.
-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#4
Post
by aGerman » 16 Apr 2014 12:42
Does every line have the same length? You could make sure using a FOR /L loop. Note that
maxsize means the inner width of your box.
Try something like that.
Code: Select all
@echo off &setlocal
set "maxsize=30"
for /f "tokens=1-7" %%g in (
'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0xDA 0xC4 0xBF 0xB3 0xC0 0xD9 0xDB"'
) do (
set "chr218=%%g"& set "chr196=%%h"& set "chr191=%%i"& set "chr179=%%j"
set "chr192=%%k"& set "chr217=%%l"& set "chr219=%%m"
)
<nul set /p "=%chr218%"& (for /l %%# in (1,1,%maxsize%) do <nul set /p "=%chr196%")& echo %chr191%
call :center %maxsize% %chr179% "blablablabla"
call :center %maxsize% %chr179% "blabla bla"
<nul set /p "=%chr192%"& (for /l %%# in (1,1,%maxsize%) do <nul set /p "=%chr196%")& echo %chr217%
pause
goto :eof
:center inner_width frame_character string
setlocal DisableDelayedExpansion
set "w=%~1"& set "f=%~2"& set "s=%~3"& setlocal EnableDelayedExpansion
(for /L %%# in (1,2,!w!) do if "!s:~%w%,1!"=="" set "s= !s! ")& set s=!s:~1,%w%!& echo(!f!!s!!f!
endlocal& endlocal& goto :eof
The
:center function accepts 3 arguments. The inner width, the frame character to be used to enclose the string, and the string that you want to center.
Regards
aGerman
-
Matt Williamson
- Posts: 82
- Joined: 30 Dec 2013 10:16
- Location: United States by the big waterfall
#5
Post
by Matt Williamson » 17 Apr 2014 05:06
Thanks aGerman! that definitely tidies up the code a bit. How did you get the hex representations of the characters? I checked with charmap and copied and pasted into a hex editor but didn't get good results.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 17 Apr 2014 05:20
aGerman wrote:Does every line have the same length? You could make sure using a FOR /L loop. Note that maxsize means the inner width of your box.
That works well.
One modification could be to take the strings from a section of the script and get the maxsize automatically.
-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 17 Apr 2014 10:26
Matt Williamson wrote:How did you get the hex representations of the characters?
There are several ways. You could execute CHCP and then have a look at the found
OEM code page. Or you could open the Windows calculator (calc.exe) in "Programmer" style and simply convert your decimal values. The latter is what I did.
foxidrive wrote:One modification could be to take the strings from a section of the script and get the maxsize automatically.
Of course. I just wrote a quick sketch based on einstein1969's technique.
Regards
aGerman