Linux: Management of the cursor with tput address [Quick Guide]

6:01 pm April 3, 201712941

I. introduction

in order to go further in the process of colorization of the text within a session shell, GNU / Linux provides a very valuable tool: tput . It is a command to manage the addressing of the cursor on the screen and manage terminal control sequences, as we saw in the previous post, regarding Escape Control sequences .

Say it like this, there is nothing really engaging. And yet, this simple command will have two essential functions:

– the positioning of the cursor in the screen of a terminal (address function)
– text effects (color, bold, italic…) by managing the Control Escape sequences.

The big advantage, compared to the previous is that there is more to enter the “Escape” codes whenever you want to put the effect in the text to display. Indeed, tput command introduced an option to specify the type of terminal. Thus, there is no need to worry about the conversion of the tables of characters and sequences to enter.

In addition, the option cup allows you to position the cursor on the screen at the exact location of a pixel. We can not only put the effect in the display of the text, but you can also, position the content to show where you want to.

II. Synopsis of the command

the order for four specific modes:

– initialization of the terminal Mode: tput init (or reset)
– address Mode of the cursor in the terminal screen: tput cup…
– mode of Scripting of commands: tput s
– effect of text and the cursor Mode: tput [t]

Note: there is an option to display the version of the underlying text program: ncurses. This is the option – V:

 # tput v 

examples: some examples of using the tput command:

 ==> # tput init to initialize the terminal # tput - Tvt220 reset ==> to reset the terminal type to vt220 # tput cup 10 4 ==> cursor line 10 and column 4 

II. The commands in terminal mode

so it is possible to initialize a terminal in the desired mode. To do this, we can use two commands:

 # tput init 

this first command initializes the terminal according to the type defined or detected since the TERM environment variable. Of course, this variable must exist in the .profile (or .bashrc) session initialization file.

Tip : to know the value of the type of terminal used, simply run the following command:

 # tput longname xterm with 256 colors 

for all of the properties of the terminal, simply run the following command:

 # infocmp screen 

If you want to overwrite the properties defined in the TERM variable, it must use the following command instead :

 # tput t  reset 

you can also move the cursor to a specific location on the display:

 # tput cup 2 5 

the first value corresponds to the number of line and the second column no.. If you want to see the number of columns for a terminal type, just run:

 # tput - Tvt220 passes 80 

the tput command can also have an influence on the prompt PS1 that is usually defined from a number of variables. If one wishes to view these variables, we can run the following command:

 # tput cup %i%p1%d; %p2%dH 

you can make the cursor completely invisible:

 # tput civis 

to make it visible again, must then run the following command:

 # tput cnorm 

the synopsis of the command is as follows :

 # tput [t] 

indicates the type of terminal. Default tput selects the value of TERM.
shows the used capacity (initialized in the termcap database).
provided the arguments of the selected option

III. The escape-control commands

as for been said in the preamble, the tput command can also allow to display text with effects: bold, italic, colorization… To do this, we can then define the different sequences encoded as follows, depending on the effects to provide:

 # tput setab [1-7] allows you to set a background color of screen (escape ANSI) # tput setb [1-7] allows you to set a background color of screen standard # tput setaf [1-7] allows you to set a foreground (escape ANSI) # tput setf color [1-7] allows you to define a standard foreground color # tput bold allows to enter bold mode # tput dim allows halve # tput smul brightness allows you to underline mode # tput RMW lets stop the highlight mode # tput rev to switch mode reverse # tput gum allows you to switch to highlight mode # tput rmso lets stop the highlight mode # tput sgr0 allows you to reset all criteria 

reminder : the color codes used by tput (as well as the control escape sequences) command are the following :

– 0 black
– 1 red
– Green
2 – 3 yellow
– blue
4 – 5 Magenta (Pink)
– 6 Cyan (light blue)
– 7 white

you will find the same operation with the control escape sequences. But, here, it is not necessary to type every time since can be perfectly defined a variable as needed:

 # bold = 'tput gum' # offbold = 'tput rmso' # echo - in "${bold} enter your name: ${offbold}" 

thanks to these two modes, you can easily create a script allowing to place the cursor where you want to define a type selection menu and offer users to make their selection.

Note : note quotes reversed to execute a command by returning the result. Directly in the environment variable.

There are to many other options (the options list is long enough). You can visit the following page for more details: GNU TPUT . There are Boolean, numeric, string capabilities…

There is a special option to script commands tput to pass. For example, for the following script:

... tput sc tput 3 45 rev tput cup 

Tip: you might rather run this script with the option – S (or – standard-input):

 # tput - S < EOF sc 3 45 rev EOF cup 

IV. View script

to illustrate this, we’ll do simple. You want to display a menu to display in the first option, the name of the machine and the last date reboot (uptime), optional 2 displays the disk space, option 3, to view the space used for the/home partition:

 #! / bin/bash # tput sample: A menu for the information  system  BKG_BLUE = "$(tput setab 4)" BKG_BLACK = "$(tput setab 0)" FOG_GREEN = "$(tput setaf 2)" FOG_WHITE = "$(tput setaf 7)" # Save screen tput smcup #. [Displaymenuselectionuntil==0while[[ $REPLY != 0 ]];
do echo-n ${BKG_BLUE} ${FOG_WHITE} clear cat <  "# Clear area beneath selection menu tput cup 10 0 n echo ${BKG_BLACK} ${FOG_GREEN} (tput ed tput cup 11 0 # Act we selection box $selection in 1) echo" Hostname: $HOSTNAME "uptime;"         
2)  df -h             
   ;;         
(3) if [[ $(id -u) -eq 0 ]]; then echo "Home Space Utilization (All Users)" - HS/home / * 2 >/dev/null else echo "Home Space Utilization ($USER)" of the $HOME s / * 2 >/dev/null | sort -nr             
    fi             
  ;;     
(4) lsb_release - a;    
0) break             
       ;;         
*) echo "Invalid entry."             
   ;;       
ESAC printf "nnPress any key to continue."       
read n 1 done # Restore screen tput rmcup echo "Program terminated." 

So, after rendering this executable script in the subdirectory in which you work, just run it:

 $ chmod 700 sample_tput.sh $./sample_tput.sh

Lors for this run will be displayed on the screen the following menu:

according to the options that we are going to enter, we’ll have different results, corresponding to the orders that we do run in the script :

– option 1:

– Option 2:

– Option 3:

– Option 4:

to get out of the infinite loop of the menu, simply type “0”. If you type another value or a letter, one receives an error message. I agree that small basic script does not go very far. But it shows the power of the tput command and management of the cursor.

V. Error Messages

order tput returns different messages based on whether the return is in error. If all goes well the return code is 0.

A return to 1 code means that the Boolean ability or character string that is mentioned is not available.

A 2 return code means that the tput command provides invalid arguments. Conversely, a return to 3 code means that the mentioned terminal type does not, exist in the variable TERM or by the value specified by the option – t.

ATTENTION: this last return code can also symbolize the fact that the basic termcap is not (or more) readable.

Finally, 4 return code means that the capacity mentioned in the parameter does not exist or is unknown to the base. A return above 4 code happens usually when using the script (- S option) we make an error in the syntax.

In conclusion, we can say that when it comes just to change a few lines with the text effect, we can use directly escape control sequences. Otherwise, you should use the command, tput, more complete and easier to handle. Besides, you will certainly see that with usage you will adopt very quickly, I’m sure, this command.