Handling of terminal consoles

Author: Adrián Pérez <aperez@igalia.com>
License:GPL3
Copyright: 2008-2009 Igalia S.L.

Abstract

Handles settings of standard Unix terminal console devices. Provides support for nicer handling of features like ECMA-48 (ANSI) color codes.

Contents

ECMA-48 graphics rendition

Support for using ECMA-48 escape sequences (also known as ANSI color escapes) is provided by using tty_color_fg, tty_color_bg and other related functions.

Typical usage

There are some usage patterns which you may want to know prior to start using the module. The most important one follows the avoid doing the same operation twice motto: calling tty_color_fg and tty_color_bg is not cheap in terms of efficiency, but their result can be saved for later use. So instead of:

echo "$(tty_color_fg red) This is RED $(tty_color_fg reset)"
echo "$(tty_color_fg red) This one too... $(tty_color_fg reset)"

it is better to save results in variables and reuse the values:

red=$(tty_color_fg red)
reset=$(tty_color_fg reset)
echo "$red This is RED $reset"
echo "$red This one too... $reset"

This way your scripts will run faster and your code will be cleaner.

Functions

tty_color_alias

tty_color_alias colorname

Obtains the real color for a given color name, resolving aliases as needed.

Note

In the current implementation only one level of indirection is resolved. That should be enough for all cases.

tty_color_fg

tty_color_fg attribute1 [ attribute2 [ ... attributeN ] ]

Obtains the ECMA-48 escape sequence needed to apply a determinate graphics rendition in the foreground. The escape sequence will apply all the given attributes. See typical usage above for tips on how to use this function.

tty_color_bg

tty_color_bg attribute1 [ attribute2 [ ... attributeN ] ]

Obtains the ECMA-48 escape sequence needed to apply a determinate graphics rendition in the background. The escape sequence will apply all the given attributes. See typical usage above for tips on how to use this function.