Term::ANSIColor 1.10 review

Download
by rbytes.net on

Term::ANSIColor is a color screen output using ANSI escape sequences. SYNOPSIS use Term::ANSIColor; print color 'bold

License: Perl Artistic License
File size: 11K
Developer: Russ Allbery
0 stars award from rbytes.net

Term::ANSIColor is a color screen output using ANSI escape sequences.

SYNOPSIS

use Term::ANSIColor;
print color 'bold blue';
print "This text is bold blue.n";
print color 'reset';
print "This text is normal.n";
print colored ("Yellow on magenta.n", 'yellow on_magenta');
print "This text is normal.n";
print colored ['yellow on_magenta'], "Yellow on magenta.n";

use Term::ANSIColor qw(uncolor);
print uncolor '01;31', "n";

use Term::ANSIColor qw(:constants);
print BOLD, BLUE, "This text is in bold blue.n", RESET;

use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
print BOLD BLUE "This text is in bold blue.n";
print "This text is normal.n";

This module has two interfaces, one through color() and colored() and the other through constants. It also offers the utility function uncolor(), which has to be explicitly imported to be used (see SYNOPSIS).

color() takes any number of strings as arguments and considers them to be space-separated lists of attributes. It then forms and returns the escape sequence to set those attributes. It doesn't print it out, just returns it, so you'll have to print it yourself if you want to (this is so that you can save it as a string, pass it to something else, send it to a file handle, or do anything else with it that you might care to).
uncolor() performs the opposite translation, turning escape sequences into a list of strings.

The recognized attributes (all of which should be fairly intuitive) are clear, reset, dark, bold, underline, underscore, blink, reverse, concealed, black, red, green, yellow, blue, magenta, on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and on_white. Case is not significant. Underline and underscore are equivalent, as are clear and reset, so use whichever is the most intuitive to you. The color alone sets the foreground color, and on_color sets the background color.
Note that not all attributes are supported by all terminal types, and some terminals may not support any of these sequences. Dark, blink, and concealed in particular are frequently not implemented.

Attributes, once set, last until they are unset (by sending the attribute "reset"). Be careful to do this, or otherwise your attribute will last after your script is done running, and people get very annoyed at having their prompt and typing changed to weird colors.

As an aid to help with this, colored() takes a scalar as the first argument and any number of attribute strings as the second argument and returns the scalar wrapped in escape codes so that the attributes will be set as requested before the string and reset to normal after the string. Alternately, you can pass a reference to an array as the first argument, and then the contents of that array will be taken as attributes and color codes and the remainder of the arguments as text to colorize.

Normally, colored() just puts attribute codes at the beginning and end of the string, but if you set $Term::ANSIColor::EACHLINE to some string, that string will be considered the line delimiter and the attribute will be set at the beginning of each line of the passed string and reset at the end of each line. This is often desirable if the output is being sent to a program like a pager that can be confused by attributes that span lines. Normally you'll want to set $Term::ANSIColor::EACHLINE to "n" to use this feature.

Requirements:
Perl

Term::ANSIColor 1.10 search tags