color in messages

Peter Pentchev roam at ringlet.net
Fri Oct 3 07:55:25 UTC 2003


On Wed, Oct 01, 2003 at 07:33:05PM -0400, Gerald S Stoller wrote:
>           It is possible to put color in the  FreeBSD  messages, I have
> seen the font color change when I used 'ls -G'.  I would also like to
> change the color of the background.  Unfortunately i have seen no
> documentation on how to do this.  May i suggest that something brief be
> written about this so people can start using color (ASAP) and send me a
> copy, then do a more extensive version (if needed for a formal release.
>          I tried to find special characters in the output of 'ls -G' by
> piping it through a  tee  but it seems that  ls  checks on the
> destination of its  STDOUT  and if it not directly to the screen it
> removes the color modifying code from it.  Were i to have coded  ls , I
> would leave the color modifying code always in the output and maybe
> provide another utiliity that removes it through which one could pipe the
> output if one wanted the color modifying code removed.
>        Probably this question is better directed at the  XFree86  people
> but I haven't gotten a good reply from them.

No, not exactly XFree86.  Using colors in a terminal window predates
XFree86 (and even the X Window System) by a long shot :)

There are basically two ways for a program to display text in a
different color (or background): the easy, but possibly very, very wrong
way of using ANSI (CSI) escape sequences directly, and the somewhat
harder, but much more portable and mostly guaranteed to work better way
of querying the database of terminal capabilities.

The ANSI escape sequences are a more or less established standard for
common ways for a program to control its output on most terminals out
there.  They consist of sending an Escape character (ASCII code 27,
0x1C) followed by a '[' character, then some optional parameters, and a
command character (usually a letter).  For a list of the available
commands see e.g. <URL:http://www.vmunix.com/~gabor/ansi.html>.
However, using ANSI escape sequences unconditionally may be a Bad
Thing(tm) on a terminal that does not support them - it will either
output lots and lots of garbage (quite confusing for the user), or it
might even interpret the sequences as some different set of commands and
do something entirely unexpected.  Most terminals out there support most
ANSI sequences, but.. be warned :)

The better, more portable way to do this would be to see if the OS (or
rather, its application libraries such as libc) supports the termcap
database - a unified database of information about the display
capabilities of a wide range of terminals.  This is done using the
tgetent() and similar functions, described in the termcap(3) manual
page.  The basic idea is that the database contains an entry for each of
the common (and some quite uncommon) terminal types as defined by the
TERM environment variable, so that the program knows whether the
terminal can e.g. change colors or move the cursor to a desired position
or change characters and so on, and what exactly is the character
sequence that will make it do so.  For more information on the termcap
database, take a look at
<URL:http://www.cs.utah.edu/dept/old/texinfo/termcap/termcap_toc.html>.

Hope this helped a bit :)

G'luck,
Peter

-- 
Peter Pentchev	roam at ringlet.net    roam at sbnd.net    roam at FreeBSD.org
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
The rest of this sentence is written in Thailand, on
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-doc/attachments/20031003/97b4b296/attachment.sig>


More information about the freebsd-doc mailing list