Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 41.9 Commands to Adjust Your Terminal Chapter 41
Terminal and Serial Line Settings
Next: 41.11 How termcap and terminfo Describe Terminals

41.10 Using terminfo Capabilities in Shell Programs

The weakness of the technique described in article 41.9 is that it requires you to hardcode the escape sequences for a particular terminal. If you use more than one kind of terminal, you have to create separate aliases for each one. That's exactly the kind of problem that the termcap and terminfo databases were designed to solve.

For each terminal in the database, there is a list of terminal capabilities (41.11). The tput program (standard on any system with terminfo) lets you print out the value of any individual capability. The tcap program does the same for systems using termcap. (tcap was originally named tc. We renamed it to avoid conflicts with tc, the ditroff interpreter program for Tektronix 4015 terminals.) This makes it possible to use terminal capabilities such as those for standout mode in shell programs.

For example, a prompt issued by a shell program could be highlighted by the following code using tput:

# Store the terminfo capability to start standout mode into
# the variable HIGHLIGHT; this might be bold, or inverse video
HIGHLIGHT=`tput smso` 
# Store the terminfo capability to end standout mode into
# the variable NORMAL 
NORMAL=`tput rmso` 
# Echo a highlighted prompt
echo "${HIGHLIGHT}Press Return to accept value: ${NORMAL}\c"

Capabilities that accept arguments (such as cursor movement sequences) will interpolate values that follow the capability name on the command line. For example, to issue the cursor motion sequence to move to the upper-left corner of the screen (row 0, column 0), you could type:

$ tput cup 0 0

Another case where tput comes in useful is when command sequences accidentally get sent to the screen, leaving output garbled or in a distracting highlight mode. It sometimes happens that a user reads a non-ASCII file, or reads a mail message with a control character accidentally imbedded, and ends up with gibberish. This is often because the sequence for entering an alternate character set has been sent to the terminal, and the screen is no longer readable to the human eye. The user can return to the normal character set two ways: by rebooting the terminal, or by entering tput init (5.12) on the command line. Using tput, obviously, is much more efficient.


Previous: 41.9 Commands to Adjust Your Terminal UNIX Power ToolsNext: 41.11 How termcap and terminfo Describe Terminals
41.9 Commands to Adjust Your Terminal Book Index41.11 How termcap and terminfo Describe Terminals

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System