The Tput command initializes and operates your terminal session through the Terminfo database. By using Tput, you can change several terminal features, such as moving or changing the cursor, changing the text properties, and clearing the specific area of the terminal screen. Like most commands in UNIX, the Tput command can be used either on the shell command line or in a shell script. To give you a better understanding of tput, this article starts with the command line, followed by a sample shell script. The Tput command initializes and operates your terminal session through the Terminfo database. By using Tput You can change several terminal features, such as moving or changing the cursor, changing the text properties, and clearing the specific area of the terminal screen. Let's take a look at its common parameters:
Parameters
(1) string output parameter settings
Bel Alarm Bell
Blink flashing Mode
Bold bolded
Civis Hide Cursor
Clear Screen
Cnorm does not hide the cursor
Cup move cursor to screen position (x,y)
El Clear to end of line
Ell clear to the beginning of the line
SMSO Boot Highlighting mode
Rmso Stop Highlighting mode
Smul Start underline mode
Rmul End Underline mode
SC Save current cursor position
RC restore cursor to last saved location
Sgr0 Normal Screen
Rev Reversal View
(2) Digital output parameter setting
Number of cols columns
Ittab Set width
Number of lines screen lines
(3) Boolean output parameter setting
CHTs Cursor not visible
HS has status line
Application
Cursor Properties
It may be useful to move the cursor or change the cursor properties in a UNIX shell script or on the command line. In some cases, you may need to enter sensitive information, such as a password, or enter information in two different areas of the screen. In such cases, using tput may be helpful to you.
Move cursor
The use of tput makes it easy to implement the position of the cursor moving on each device. By using the cup option in Tput, or the cursor position, you can move the cursor to any X or Y coordinate in each row and column of the device. The coordinates of the upper-left corner of the device are (0,0).
To move the cursor to line 1th (Y) in column 5th (X) on the device, simply perform Tput Cup 5 1. Another example is Tput Cup 23 45, which moves the cursor to line 45th on column 23rd.
Move the cursor and display the information
Another useful technique for positioning the cursor is to move the cursor, execute the command used to display the information, and then return to the previous cursor position:
The code is as follows:
(Tput SC; tput Cup; echo "Input from Tput/echo at 23/45"; Tput RC)
Below we analyze the Subshell command:
The code is as follows:
Tput SC
You must first save the current cursor position. To save your current cursor position, include the SC option or save cursor position.
The code is as follows:
Tput Cup 23 45
After the cursor position is saved, the cursor coordinates are moved to (23,45).
The code is as follows:
echo "Input from Tput/echo at 23/45"
Displays the information in stdout.
The code is as follows:
Tput RC
Once this information is displayed, the cursor must return to the original location saved with Tput SC. To return the cursor to the location where it was last saved, include the RC option or the RESTORE cursor position.
Note: Since this article first details the execution of Tput through the command line, you may feel that it is simpler to execute a command in your own subshell than to execute each command individually and then display the prompts before each command executes.
Change the properties of the cursor
When displaying data to a device, you don't want to see the cursor many times. Converting the cursor to invisible can make the screen look neater when the data is scrolled. To make your cursor invisible, use the Civis option (for example, Tput Civis). After the data is fully displayed, you can use the Cnorm option to turn the cursor back to visible again.
Text properties
Changing the way text is displayed allows users to notice a set of words in a menu or alert users to some important content. You can change the text properties by making the text bold, underlining underneath the text, changing the background color and foreground color, and reversing the color scheme.
To change the color of the text, use the SETB option (used to set the background color) and the SETF option (for setting the foreground color) and the color values assigned in the Terminfo database. Typically, the assigned value corresponds to the color, but may vary depending on the UNIX system:
0: Black
1: Blue
2: The Green
3: Cyan
4: Red
5: Magenta
6: Yellow
7: White
Perform the following example command to change the background color to yellow and change the foreground color to red:
Copy Code
The code is as follows:
Tput SETB 6
Tput SETF 4
To display the current color scheme, simply perform the Tput rev.
Sometimes, text coloring is not enough, that is to say, you want to get the user's attention in another way. There are two ways to do this: one is to set the text to bold, and the second to underline the text.
To change the text to bold, use the bold option. To start adding underscores, use the SMUL option. When you finish displaying the underlined text, use the Rmul option.