TrumanWong

tput

Initialize and operate terminal sessions through the terminfo database

Supplementary instructions

The tput command will initialize and operate your terminal session through the terminfo database. By using tput, you can change several terminal functions, such as moving or changing the cursor, changing text properties, and clearing specific areas of the terminal screen.

What is the terminfo database?

The terminfo database on UNIX systems is used to define the properties and capabilities of terminals and printers, including the number of rows and columns for each device (for example, the terminal and printer) and the properties of the text to be sent to the device. Several common programs in UNIX rely on the terminfo database to provide these properties and many others, including the vi and emacs editors and the curses and man programs.

Like most commands in UNIX, the tput command can be used either from the shell command line or from a shell script. To give you a better understanding of tput, this article starts with the command line, followed by shell script examples.

Cursor properties

In a UNIX shell script or at the command line, it can be useful to move the cursor or change cursor properties. In some cases, you may need to enter sensitive information (such as a password) or enter information in two different areas on the screen. In such cases, using tput may help you.

tput clear # clear screen
tput sc # Save the current cursor position
tput cup 10 13 # Move the cursor to row col
tput civis # The cursor is invisible
tput cnorm # The cursor is visible
tput rc #display output
exit 0

Move cursor

Use tput to easily move the cursor position on various devices. By using the cup option with tput, or the cursor position, you can move the cursor to any X or Y coordinate within the rows and columns of the device. The coordinates of the upper left corner of the device are (0,0).

To move the cursor on the device to column 5 (X), row 1 (Y), just do tput cup 5 1. Another example is tput cup 23 45, which will move the cursor to column 23, row 45.

Move cursor and display information

Another useful cursor positioning technique is to move the cursor, execute a command that displays information, and then return to the previous cursor position:

(tput sc; tput cup 23 45; echo “Input from tput/echo at 23/45”; tput rc)

Let’s analyze the subshell command:

tput sc

The current cursor position must first be saved. To save the current cursor position, include the sc option or "save cursor position".

tput cup 23 45

After the cursor position is saved, the cursor coordinates will move to (23,45).

echo "Input from tput/echo at 23/45"

Display information to stdout.

tputrc

After displaying this information, the cursor must be returned to the original position saved with tput sc. To return the cursor to its last saved position, include the rc option or "restore cursor position".

Note: Since this article first details executing tput from the command line, you may find it cleaner to execute the commands in your own subshell than to execute each command individually and then display a prompt before each command is executed.

Change the properties of the cursor

When displaying data to a device, many times you don't want to see the cursor. Turning the cursor invisible makes the screen look cleaner when data is scrolling. To make the cursor invisible, use the civis option (for example, tput civis). After the data is fully displayed, you can use the cnorm option to make the cursor visible again.

Text Properties

Changing the way text is displayed can draw users' attention to a set of words in a menu or alert users to something important. You can change text properties by making text bold, underlining text, changing background and foreground colors, reversing the color scheme, and more.

To change the color of text, use the setb option (for setting the background color) and the setf option (for setting the foreground color) with the color value assigned in the terminfo database. Typically, the assigned values correspond to colors as follows, but this may vary depending on your UNIX system:

  • 0: black *1: blue *2: Green *3: cyan *4: red *5: Magenta *6: yellow *7: White

Execute the following example commands to change the background color to yellow and the foreground color to red:

tput setb 6 tput setf 4

To reverse the current color scheme, just execute tput rev.

Sometimes, just coloring the text isn't enough, that is, you want to draw the user's attention in another way. This can be achieved in two ways: by making the text bold or by underlining the text.

To change text to bold, use the bold option. To start adding underlines, use the smul option. When you are finished displaying underlined text, use the rmul option.

Example

Make the output string have color, background color, and boldness:

#!/bin/bash
printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0)

for((i=0; i<=7; i++)); do
     echo $(tput setaf $i)"show me the money"$(tput sgr0)
done

printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n'

for((i=0,j=7; i<=7; i++,j--)); do
     echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0)
done

exit 0

Output format control function:

#!/bin/bash

# $1 str print string
# $2 color 0-7 set color
# $3 bgcolor 0-7 set background color
# $4 bold 0-1 set bold font
# $5 underline 0-1 set underline

function format_output(){
     str=$1
     color=$2
     bgcolor=$3
     bold=$4
     underline=$5
     normal=$(tput sgr0)

     case "$color" in
         0|1|2|3|4|5|6|7)
             setcolor=$(tput setaf $color;) ;;
         *)
             setcolor="" ;;
     esac

     case "$bgcolor" in
         0|1|2|3|4|5|6|7)
             setbgcolor=$(tput setab $bgcolor;) ;;
         *)
             setbgcolor="" ;;
     esac

     if [ "$bold" = "1" ]; then
         setbold=$(tput bold;)
     else
         setbold=""
     fi

     if [ "$underline" = "1" ]; then
         setunderline=$(tput smul;)
     else
         setunderline=""
     fi

     printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n"
}

format_output "Yesterday Once more" 2 5 1 1

exit 0

Cursor attribute example:

#!/bin/bash
# clear the screen
tput clear
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
# set a foreground color using ANSI escape
tput setaf 3
echo "XYX Corp LTD."
tput sgr0
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
tput cup 7 15
echo "1\. User Management"
tput cup 8 15
echo "2\. service Management"
tput cup 9 15
echo "3\. Process Management"
tput cup 10 15
echo "4\. Backup"
# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
tput clear
tput sgr0
tputrc

exit 0