Free Hosting Online for WorkStations

< Previous | Contents | Next >

Adding Color

Most terminal emulator programs respond to certain non-printing character sequences to control such things as character attributes (like color, bold text, and the dreaded blinking text) and cursor position. We'll cover cursor position in a little bit, but first we'll look at color.


Terminal Confusion

Back in ancient times, when terminals were hooked to remote computers, there were many competing brands of terminals and they all worked differently. They had different keyboards and they all had different ways of interpreting control in- formation. Unix and Unix-like systems have two rather complex subsystems to deal with the babel of terminal control (called termcap and terminfo). If you look in the deepest recesses of your terminal emulator settings you may find a set- ting for the type of terminal emulation.

In an effort to make terminals speak some sort of common language, the Ameri- can National Standards Institute (ANSI) developed a standard set of character se- quences to control video terminals. Old time DOS users will remember the AN- SI.SYS file that was used to enable interpretation of these codes.


Character color is controlled by sending the terminal emulator an ANSI escape code em- bedded in the stream of characters to be displayed. The control code does not “print out” on the display, rather it is interpreted by the terminal as an instruction. As we saw in the table above, the \[ and \] sequences are used to encapsulate non-printing characters. An ANSI escape code begins with an octal 033 (the code generated by the escape key), fol- lowed by an optional character attribute, followed by an instruction. For example, the code to set the text color to normal (attribute = 0), black text is:

\033[0;30m

Here is a table of available text colors. Notice that the colors are divided into two groups, differentiated by the application of the bold character attribute (1) which creates the ap- pearance of “light” colors:


Table 13- 2: Escape Sequences Used To Set Text Colors


Sequence

Text Color

Sequence

Text Color

\033[0;30m

Black

\033[1;30m

Dark Gray

\033[0;31m

Red

\033[1;31m

Light Red

\033[0;32m

Green

\033[1;32m

Light Green

\033[0;33m

Brown

\033[1;33m

Yellow

\033[0;34m

Blue

\033[1;34m

Light Blue

\033[0;35m

Purple

\033[1;35m

Light Purple

Adding Color


\033[0;36m

Cyan

\033[1;36m

Light Cyan

\033[0;37m

Light Grey

\033[1;37m

White


Let's try to make a red prompt. We'll insert the escape code at the beginning:



<me@linuxbox ~>$ PS1="\[\033[0;31m\]<\u@\h \W>\$ "

<me@linuxbox ~>$

<me@linuxbox ~>$ PS1="\[\033[0;31m\]<\u@\h \W>\$ "

<me@linuxbox ~>$


That works, but notice that all the text that we type after the prompt is also red. To fix this, we will add another escape code to the end of the prompt that tells the terminal emu- lator to return to the previous color:



<me@linuxbox ~>$ PS1="\[\033[0;31m\]<\u@\h \W>\$\[\033[0m\] "

<me@linuxbox ~>$

<me@linuxbox ~>$ PS1="\[\033[0;31m\]<\u@\h \W>\$\[\033[0m\] "

<me@linuxbox ~>$


That's better!

It's also possible to set the text background color using the codes listed below. The back- ground colors do not support the bold attribute.


Table 13-3: Escape Sequences Used To Set Background Color


Sequence

Background Color

Sequence

Background Color

\033[0;40m

Black

\033[0;44m

Blue

\033[0;41m

Red

\033[0;45m

Purple

\033[0;42m

Green

\033[0;46m

Cyan

\033[0;43m

Brown

\033[0;47m

Light Grey


We can create a prompt with a red background by applying a simple change to the first escape code:



<me@linuxbox ~>$ PS1="\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\] "

<me@linuxbox ~>$

<me@linuxbox ~>$ PS1="\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\] "

<me@linuxbox ~>$


Try out the color codes and see what you can create!


Note: Besides the normal (0) and bold (1) character attributes, text may also be given underscore (4), blinking (5), and inverse (7) attributes as well. In the interests of good taste, many terminal emulators refuse to honor the blinking attribute, how- ever.



Top OS Cloud Computing at OnWorks: