In a recent post, we covered a technique that can produce colored text on the command line. Today, we will look at a more general approach to producing not only text effects, but also gaining more visual control of our terminal.
A Little History
Back in the old days, when computers were connected to remote terminals, many brands of terminals existed and they were all a little different in terms of their feature sets and capabilities. As a result, different terminals used different sets of commands to control them.
Terminals responds to codes (called control codes) embedded in the stream of text sent to them. Some of these codes are standard and familiar like carriage return and line feed. Others, like those to turn on bold text or underlining are not. Terminals can, in fact, perform many kinds of functions. As microprocessors became available and the advent of the personal computer loomed, terminals became increasingly "smart" and feature laden.
However the proliferation of terminal brands and feature sets posed a problem for software developers. Software had to be painstakingly customized to support a particular terminal. What was needed was a software system that supported hardware independence so that applications could use a standard set of commands to deal with any terminal. This problem was addressed in two ways. First a standard set of control sequences were developed by ANSI (American National Standards Institute) and adopted (in varying degrees) by terminal manufactures to give all terminals a common set of commands. We looked at the ANSI commands in a an earlier post. The second approach was development of an intermediary layer (much like today's notion of a device driver) that translates a standardized command into the specific control codes used by a particular terminal.
In the Unix world, there are two such systems, the original, termcap and the more recent terminfo. Both contain a database of control code sequences used by different kinds of terminals.
Enter tput
tput is a command that can query the terminfo database to see if a particular terminal can support a particular feature. It can also accept terminal commands and output (via standard output) the control code sequences for that terminal. tput is generally used like this:
tput capname [parameters...]
where capname is the name of a terminal capability and parameters are any option parameters associated with the specified capability. For example, to output the sequence of instructions needed to move the cursor to the upper left corner of the screen (the "home" position):
tput cup 0 0
which means cursor position row 0, column 0.
Since tput actually outputs the sequence to standard output (you won't see the sequence since it is interpreted by your terminal emulator as an instruction), you can store the sequences in variables. Here we will store the sequences to control bold text:
bold_on=$(tput bold)
bold_off=$(tput sgr0)
Now, to highlight some text, you could:
echo "This is ${bold_on}important${bold_off}."
and you get this:
This is important.
There are a huge number of terminal capabilities, though most terminals only support a small subset. Besides changing text colors and positioning the cursor, it is possible to erase text, insert text, and control text attributes. The terminfo man page lists all the terminal capabilities and the Bash Prompt HOWTO section 6.5 (see "Further Reading" below) describes the ones most useful for ordinary screen control.
Before we leave, here is a version of the prompt_colors script that uses tput to set foreground and background text colors:
#!/bin/bash
# prompt_colors -- demonstrate prompt color combinations.
for fore in {0..7}; do
set_foreground=$(tput setf $fore)
for back in {0..7}; do
set_background=$(tput setb $back)
echo -n $set_background$set_foreground
printf ' F:%s B:%s ' $fore $back
done
echo $(tput sgr0)
done
Further Reading
No comments:
Post a Comment