Linux terminal no cursor

Linux terminal no cursor

While our command line environment is certainly powerful, it can be be somewhat lacking when it comes to visual appeal. Our terminals cannot create the rich environment of the graphical user interface, but it doesn’t mean we are doomed to always look at plain characters on a plain background.

In this adventure, we will look at tput , a command used to manipulate our terminal. With it, we can change the color of text, apply effects, and generally brighten things up. More importantly, we can use tput to improve the human factors of our scripts. For example, we can use color and text effects to better present information to our users.

Availability

tput is part of the ncurses package and is supplied with most Linux distributions.

What it Does/How it Works

Long ago, when computers were centralized, interactive computer users communicated with remote systems by using a physical terminal or a terminal emulator program running on some other system. In their heyday, there were many kinds of terminals and they all used different sequences of control characters to manage their screens and keyboards.

When we start a terminal session on our Linux system, the terminal emulator sets the TERM environment variable with the name of a terminal type. If we examine TERM , we can see this:

In this example, we see that our terminal type is named “xterm” suggesting that our terminal behaves like the classic X terminal emulator program xterm . Other common terminal types are “linux” for the Linux console, and “screen” used by terminal multiplexers such as screen and tmux . While we will encounter these 3 types most often, there are, in fact, thousands of different terminal types. Our Linux system contains a database called terminfo that describes them. We can examine a typical terminfo entry using the infocmp command followed by a terminal type name:

The example above is the terminfo entry for the terminal type “screen”. What we see in the output of infocmp is a comma-separated list of terminal capability names or capnames. Some of the capabilities are standalone — like the first few in the list — while others are assigned cryptic values. Standalone terminal capabilities indicate something the terminal can do. For example, the capability “am” indicates the terminal has an automatic right margin. Terminal capabilities with assigned values contain strings, which are interpreted as commands by the terminal. The values starting with “\E” (which represents the escape character) are sequences of control codes that cause the terminal to perform an action such as moving the cursor to a specified location, or setting the text color.

The tput command can be used to test for a particular capability or to output the assigned value. Here are some examples:

This outputs the full name of the current terminal type. We can specify another terminal type by including the -T option. Here, we will ask for the full name of the terminal type named “screen”:

We can inquire values from the terminfo database, like the number of supported colors and the number of columns in the current terminal:

Читайте также:  Astra linux проверка целостности системы

We can test for particular capability. For example, to see if the current terminal supports “bce” (background color erase — meaning that clearing or erasing text will be done using the currently defined background color) we type:

We can send instructions to the terminal. For example, to move the cursor to the position 20 characters to the right and 5 rows down:

There are many different terminal types defined in the terminfo database and there are many terminal capnames. The terminfo man page contains a complete list. Note, however, that in general practice, there are only a relative handful of capnames supported by all of the terminal types we are likely to encounter on Linux systems.

Reading Terminal Attributes

For the following capnames, tput outputs a value to stdout:

Capability names
Capname Description
longname Full name of the terminal type
lines Number of lines in the terminal
cols Number of columns in the terminal
colors Number of colors available

The lines and cols values are dynamic. That is, they are updated as the size of the terminal window changes. Here is a handy alias that creates a command to view the current size of our terminal window:

If we define this alias and execute it, we will see the size of the current terminal displayed. If we then change the size of the terminal window and execute the alias a second time, we will see the values have been updated.

One interesting feature we can use in our scripts is the SIGWINCH signal. This signal is sent each time the terminal window is resized. We can include a signal handler (i.e., a trap) in our scripts to detect this signal and act upon it:

With this script, we start an empty infinite loop, but since we set a trap for the SIGWINCH signal, each time the terminal window is resized the trap is triggered and the new terminal size is displayed. To exit this script, we type Ctrl-c .

term_size2

Controlling the Cursor

The capnames below output strings containing control codes that instruct the terminal to manipulate the cursor:

Cursor control capnames
Capname Description
sc Save the cursor position
rc Restore the cursor position
home Move the cursor to upper left corner (0,0)
cup Move the cursor to position row, col
cud1 Move the cursor down 1 line
cuu1 Move the cursor up 1 line
civis Set to cursor to be invisible
cnorm Set the cursor to its normal state

We can modify our previous script to use cursor positioning and to place the window dimensions in the center as the terminal is resized:

As in the previous script, we set a trap for the SIGWINCH signal and start an infinite loop. The redraw function in this script is a bit more complicated, since it has to calculate the center of the terminal window each time its size changes.

term_size3

Text Effects

Like the capnames used for cursor manipulation, the following capnames output strings of control codes that affect the way our terminal displays text characters:

Text effects capnames
Capname Description
bold Start bold text
smul Start underlined text
rmul End underlined text
rev Start reverse video
blink Start blinking text
invis Start invisible text
smso Start “standout” mode
rmso End “standout” mode
sgr0 Turn off all attributes
setaf Set foreground color
setab Set background color

Some capabilities, such as underline and standout, have capnames to turn the attribute both on and off while others only have a capname to turn the attribute on. In these cases, the sgr0 capname can be used to return the text rendering to a “normal” state. Here is a simple script that demonstrates the common text effects:

Text Color

Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256). Using the setaf and setab capabilities, we can set the foreground and background colors. The exact rendering of colors is a little hard to predict. Many desktop managers impose “system colors” on terminal windows, thereby modifying foreground and background colors from the standard. Despite this, here are what the colors should be:

Text colors
Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color

The following script uses the setaf and setab capabilities to display the available foreground/background color combinations:

Clearing the Screen

These capnames allow us to selectively clear portions of the terminal display:

Screen erasure capnames
Capname Description
smcup Save screen contents
rmcup Restore screen contents
el Clear from the cursor to the end of the line
el1 Clear from the cursor to the beginning of the line
ed Clear from the cursor to the end of the screen
clear Clear the entire screen and home the cursor

Using some of these terminal capabilities, we can construct a script with a menu and a separate output area to display some system information:

Making Time

For our final exercise, we will make something useful; a large character clock. To do this, we first need to install a program called banner . The banner program accepts one or more words as arguments and displays them like so:

This program has been around for a long time and there are several different implementations. On Debian-based systems (such as Ubuntu) the package is called “sysvbanner”, on Red Hat-based systems the package is called simply “banner”. Once we have banner installed we can run this script to display our clock:

Our script paints the screen blue and places the current time in the center of the terminal window. This script does not dynamically update the display’s position if the terminal is resized (that’s an enhancement left to the reader). A progress bar is displayed beneath the clock and it is updated every second until the next minute is reached, when the clock itself is updated.

One interesting feature of the script is how it deals with painting the screen. Terminals that support the “bce” capability erase using the current background color. So, on terminals that support bce, this is easy. We simply set the background color and then clear the screen. Terminals that do not support bce always erase to the default color (usually black).

To solve this problem, our this script creates a long string of spaces that will fill the screen. On terminal types that do not support bce (for example, screen) the background color is set, the cursor is moved to the home position and then the string of spaces is drawn to fill the screen with the desired background color.

Summing Up

Using tput , we can easily add visual enhancements to our scripts. While it’s important not to get carried away, lest we end up with a garish, blinking mess, adding text effects and color can increase the visual appeal of our work and improve the readability of information we present to our users.

Further Reading

The terminfo man page contains the entire list of terminal capabilities defined terminfo database.

On most systems, the /lib/terminfo and /usr/share/terminfo directories contain the all of the terminals supported by terminfo.

Bash Hacker’s Wiki has a good entry on the subject of text effects using tput . The page also has some interesting example scripts.

Greg’s Wiki contains useful information about setting text colors using tput .

Bash Prompt HOWTO discusses using tput to apply text effects to the shell prompt.

© 2000-2021, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

Linux® is a registered trademark of Linus Torvalds.

Источник

How to hide the mouse cursor

I’m building a kiosk using Ubuntu Precise on a touch screen. Now I’m looking for the appropriate way to make the mouse cursor disappear. As people know where they are pointing, displaying an arrow under their finger is useless, and having an arrow where they last pointed even more so.

My best bet would be some kind of cursor theme consisting only of transparent cursors. I’m a bit surprised to find no UI to switch and maybe install cursor themes in the default Unity UI, but as I won’t be using Unity, that’s not much of a problem. It appears that the alternatives listed in update-alternatives —list x-cursor-theme all refer to .theme files, so I searched the package list for those. The resulting list does not list any likely candidates, i.e. no packages containing “invisible” or “transparent” in their name.

So far, some googled result yielding a readme for “XCursor Transparent Theme” is my best bet. That would mean compiling those sources myself, perhaps putting them into my PPA. I’m also a bit sceptical about that result as said readme is dated from 2003. And I’m not sure that I’m not making things overly complicated. After all, there is quite some support in Precise for touch devices, so I don’t believe I’m the first one who wants to get rid of his mouse cursor.

  • Is there another way which doesn’t involve user-compiled binary code?
  • Is there a theme package for transparent cursors which I’ve overlooked?
  • Is there some other mechanism to make the cursor disappear without changing the cursor theme?

I’ll be using Matchbox WM, Firefox and Java applets, so I’ll be happy with any solution working under such a setup. I’m not interested in any solutions twiddling with Gnome or Compiz, as I’ll not be running either.

Источник

Читайте также:  Hp probook 4540s установка windows
Оцените статью