Settings

The page will reload to apply your changes.
Theme

ASCII Art Font

ASCII Art Menu

Original Usenet thread from alt.ascii-art, started 6 Mar 1995.
 _   _                     _        _             _     _           
| | | |___  ___ _ __   ___| |_     / \   _ __ ___| |__ (_)_   _____ 
| | | / __|/ _ \ '_ \ / _ \ __|   / _ \ | '__/ __| '_ \| \ \ / / _ \
| |_| \__ \  __/ | | |  __/ |_   / ___ \| | | (__| | | | |\ V /  __/
 \___/|___/\___|_| |_|\___|\__| /_/   \_\_|  \___|_| |_|_| \_/ \___|
Question: Centering Stuff

Question: Centering Stuff

alt.ascii-art · 4 messages · 6 Mar 1995 - 13 Mar 1995
Brad <syb...@aber.ac.uk> ASCII art 6 Mar 1995 00:59 · permalink · report
	Does anybody have a script that centers text or pictures or whatever?
I know this is only a really simple tr command, but I can't figure it out
right now. :) Thanks... 

  ___  _                      _a' /(   <.  Simon Bradley, Knight Protector!
 / __><_>._ _ _  ___ ._ _  ~~ _}\ \(  _  ) E-mail: syb...@aber.ac.uk
 \__ \| || ' ' |/ . \| ' |       \(._(.)'  Finger: syb...@osfb.aber.ac.uk
 <___/|_||_|_|_|\___/|_|_|      ._>. _>.   WWW: http://www.aber.ac.uk/~syb3/

Tony Nugent <T.N...@sct.gu.edu.au> ASCII art 7 Mar 1995 08:21 · permalink · report
Brad <syb...@aber.ac.uk> writes:

>	Does anybody have a script that centers text or pictures or whatever?
>I know this is only a really simple tr command, but I can't figure it out
>right now. :) Thanks... 

tr(1) is used to _translate_ characters... I don't think you can
use it to do text centering.

Ok, I have *two* solutions...

This awk script will center text according to the
length of *each* input line:

========8<----cut-all-above-this-line-and-keep-all-below-------
#!/usr/bin/awk -f
#
# center
#
# Centers all input text on an 80-column line according to the
# length of each input line.
#
# Tony Nugent ** T.N...@sct.gu.edu.au ** tnu...@gucis.cit.gu.edu.au
#
{
  pad=""
  if ( length < 78 ) {
    le = ( 80-length ) / 2
    for ( i=0 ; i<le ; i++ ) {
      pad=pad" "
    };
  } print pad $0
}
========8<----cut-all-below-this-line-and-keep-all-above-------

This could easily be modified so that you can give it a parameter
to set the right hand margin to something other than 80 columns.

However, Brad and I have had some email after he also posted this
question into the ascii art mailing list.  He was looking for
something that would evenly center lines in a file that had
uneven length.  Specifically, he was wanting to center ascii art,
and if you use the above script on files with lines of uneven
length then the result would be mashed.

Ok, so I've come up with a solution that should solve this
problem.  The following script will center lines in a file so
that the centering offset depend on the length of the longest
line in the file:

========8<----cut-all-above-this-line-and-keep-all-below-------
#!/usr/bin/awk -f
#
# centermax
#
# Centres all input text on an 80-column line using the length of
# the longest input line as the centering offset.
#
# Tony Nugent ** T.N...@sct.gu.edu.au ** tnu...@gucis.cit.gu.edu.au
#
{
  if ( length > le ) {
    le = length
  }
  line[numlines++] = $0
}
END {
  pad=""
  if ( le < 78 ) {
    le = ( 80-le ) / 2
    for ( i=0 ; i<=le ; i++ ) {
      pad=pad" "
    };
  }
  for ( i=0; i<numlines; i++ ) {
    print pad line[i]
  }
}
========8<----cut-all-below-this-line-and-keep-all-above-------

This one does seem to work the way he was wanting it to.

I hope others find it useful.

BTW, I do have another much longer two-part script that does
left/center/right justification, and you can also specify the
right-hand margin.  If anyone wants a copy of this then email
me... but warning - I haven't used it in a while and I don't know
how reliable it is :)

Share and Enjoy!  :-)

(Cc. to Brad... just to let you know mate that I've posted it in
r.a.a,etc,etc with some minor changes including the deletion of a
debugging line and fixing of the i count :)

Cheers
Tony
        MMM             \|/              www              __^__
       (o o)            @ @             (o o)            /(o o)\
  -.ooO-(_)-Ooo.-+-.oOO-(_)-OOo.-+-.oOO--(_)--OOo.-+-.oOO==(_)==OOo.-----+
  |    Tony Nugent  |-| Griffith University  T.N...@sct.gu.edu.au      |--.
  |     __'!`__     | | Brisbane, Queensland tnu...@gucis.cit.gu.edu.au |  |
  |      (o o)      | | Australia                                        |  |
  `-ooO---(=)---Ooo-' `--------------------------------------------------'  |
    `------------------' `--------------------------------------------------'

Peter Samuelson <psa...@osiris.hmc.edu> ASCII art 10 Mar 1995 05:00 · permalink · report
[If there's two copies of this message -- sorry...ignore this one]

> 	Does anybody have a script that centers text or pictures or
> whatever?  I know this is only a really simple tr command, but I
> can't figure it out right now. :) Thanks...

I don't know of any way, offhand, so I threw together a little shell
script that appears to work (in UNIX at least).  Clip below (not
including) the line, call it "center" and make it executable.

And I wouldn't recommend centering pictures, unless they have two
straight margins (like a converted GIF).

Peter
-----------------------------------cut-----------------------------------
#!/bin/sh
#
# Center: centers standard input
# Something of a hack, but it works (for me at least)
# Test with something like "./this-file < this-file"
#
half=`expr ${COLUMNS-80} / 2 + 1`;
spaces="                                                                      "
while read line; do
  w1=`echo "$line" | wc -c`
  w2=`expr $half - $w1 / 2`
  if [ $w2 -gt 0 ]; then
    blank=`echo "$spaces"|cut -c-$w2`;
    echo "$blank""$line"
  else
    echo $line;
  fi;
done;

Tony Nugent <T.N...@sct.gu.edu.au> wrote a lovely awk script to 
center text



nice one!

will be keeping a copy until it comes in handy.

awk certainly is powerful, shame it's so complicated (IMHO)



Mark

http://ion.le.ac.uk/~mar5

About this thread. These messages were posted publicly to the newsgroup alt.ascii-art in 1995 and are mirrored here unchanged as part of the Historic Archives – only email addresses are masked. The artwork and text belong to their original authors: if you copy a piece, keep the artist's initials or signature intact and credit them where you can. Are you one of the authors? Contact us to get your posts attributed, connected to your artist profile, or removed.

Report this message

Help us keep the archive clean and accurate. Reports are reviewed by a person – nothing is changed automatically.

Help classify this post