Operating System - HP-UX
1843970 Members
2092 Online
110226 Solutions
New Discussion

print formatted text to center of a text terminal

 
SOLVED
Go to solution
Mike_Ca Li
Regular Advisor

print formatted text to center of a text terminal

I tried to use printf "String of text \n" with \n, \t, etc options but there is not a general program or script which I can find. Any suggestions or pointers? Thank you.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: print formatted text to center of a text terminal

Hi:

You can do something like this:

#!/usr/bin/sh
MSG="This message is centered!"
MSZ=${#MSG}
COL=`echo "(80-${MSZ})/2"|bc`
clear
tput cup 12 ${COL}
echo ${MSG}
tput cup 23 0
exit 0

Note that the string of text will be centered horizontally at row-12. See the man pages for 'tput' for more information.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: print formatted text to center of a text terminal

This should be close:

#!/usr/bin/sh

center_it()
{
if [[ ${#} -ne 1 ]]
then
echo "Function center_it expects exactly 1 arg" >&2
return 255
fi
WORDS=${1}
shift
WIDTH=$(tput cols)
LEN=${#WORDS}
SPACES=$(( (${WIDTH} - ${LEN}) / 2 ))
I=1
while [[ ${I} -le ${SPACES} ]]
do
echo " \c"
I=$((${I} + 1))
done
echo "${WORDS}"
return 0
} # center_it

center_it "Turnip grrens is good"

That should do it if my typing is ok.


If it ain't broke, I can fix that.
Mike_Ca Li
Regular Advisor

Re: print formatted text to center of a text terminal

Thank you James for the fast reply. Would there be something will will handle more than 1 line of text?
eg:
line1
line2 is longer
line3 is longest line

Thanks.
H.Merijn Brand (procura
Honored Contributor

Re: print formatted text to center of a text terminal

pointer: http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x7b530ea029a2d711abdc0090277a778c,00.html
has some suggestions. I answered:

# perl -e'$msg="@ARGV";printf"%*s%s\n",(($ENV{COLUMNS}||80)-length$msg)/2,"",$msg' Your message

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: print formatted text to center of a text terminal

Hi (again):

To handle multiple lines, simply create a shell function (as Clay has shown) that is passed the line (text string) that you want centered. If you want to place the line at a particular screen row, create or amend the function to understand the receipt of a second argument defining the row.

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: print formatted text to center of a text terminal

In which case all lines should be centered individually, or be centered as a left aligned block?

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: print formatted text to center of a text terminal

*each* line centered on its own would be

# perl -e'printf"%*s%s\n",(($ENV{COLUMNS}||80)-length)/2,"",$_ for@ARGV' "Line 1" "Longer line 2" "ln3"

The left aligned block centered something like

# perl -e'map{length($_)>$l and $l=length}@ARGV;$l=(($ENV{COLUMNS}||80)-$l)/2;printf"%*s%s\n",$l,"",$_ for@ARGV' "Line 1" "Longer line 2" "ln3"

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Mike_Ca Li
Regular Advisor

Re: print formatted text to center of a text terminal

Hi James:
Could you pls explain more.
I did not get an output from Clay's script. Pls advise what did I miss.

Thanks
Rodney Hills
Honored Contributor

Re: print formatted text to center of a text terminal

hpux has a standard command for simple formatting called "adjust".

for example-
adjust -c myfile

This will center each text line of "myfile" on the screen.

HTH

-- Rod Hills
There be dragons...