Logo

Darmowy hosting online dla stacji roboczych

<Poprzedni | Spis treści | Następne>

Dodawanie koloru

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.


image

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 i termino). 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 \[ i \] 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


Sekwencja

Kolor tekstu

Sekwencja

Kolor tekstu

\033[0;30m

Czarny

\033[1;30m

Ciemny szary

\033[0;31m

Czerwony

\033[1;31m

Światło czerwone

\033[0;32m

Zielony

\033[1;32m

Green Light

\033[0;33m

Ryż Basmati Brązowy

\033[1;33m

Żółty

\033[0;34m

Niebieski

\033[1;34m

Jasnoniebieski

\033[0;35m

Fioletowy

\033[1;35m

Jasny fiolet

Dodawanie koloru


\033[0;36m

Cyan

\033[1;36m

Jasnobłękitny

\033[0;37m

Jasnoszary

\033[1;37m

Biały


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



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

$

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

$


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:



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

$

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

$


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


Sekwencja

Kolor tła

Sekwencja

Kolor tła

\033[0;40m

Czarny

\033[0;44m

Niebieski

\033[0;41m

Czerwony

\033[0;45m

Fioletowy

\033[0;42m

Zielony

\033[0;46m

Cyan

\033[0;43m

Ryż Basmati Brązowy

\033[0;47m

Jasnoszary


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



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

<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.



Najlepsze przetwarzanie w chmurze dla systemu operacyjnego w OnWorks: