package Term::ANSIColor;
require 5.001;
-$VERSION = '2.01';
+$VERSION = '2.02';
use strict;
use vars qw($AUTOLOAD $AUTOLOCAL $AUTORESET @COLORLIST @COLORSTACK $EACHLINE
use Exporter ();
BEGIN {
- @COLORLIST = qw(CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE
- CONCEALED BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
- ON_BLACK ON_RED ON_GREEN ON_YELLOW ON_BLUE ON_MAGENTA
- ON_CYAN ON_WHITE);
+ @COLORLIST = qw(CLEAR RESET BOLD DARK FAINT UNDERLINE UNDERSCORE BLINK
+ REVERSE CONCEALED BLACK RED GREEN YELLOW BLUE MAGENTA
+ CYAN WHITE ON_BLACK ON_RED ON_GREEN ON_YELLOW ON_BLUE
+ ON_MAGENTA ON_CYAN ON_WHITE);
@ISA = qw(Exporter);
@EXPORT = qw(color colored);
- @EXPORT_OK = qw(uncolor colorstrip);
+ @EXPORT_OK = qw(uncolor colorstrip colorvalid);
%EXPORT_TAGS = (constants => \@COLORLIST,
pushpop => [ @COLORLIST,
qw(PUSHCOLOR POPCOLOR LOCALCOLOR) ]);
return wantarray ? @string : join ('', @string);
}
+# Given a list of color attributes (arguments for color, for instance), return
+# true if they're all valid or false if any of them are invalid.
+sub colorvalid {
+ my @codes = map { split } @_;
+ for (@codes) {
+ unless (defined $ATTRIBUTES{lc $_}) {
+ return;
+ }
+ }
+ return 1;
+}
+
##############################################################################
# Module return value and documentation
##############################################################################
use Term::ANSIColor qw(colorstrip);
print colorstrip '\e[1mThis is bold\e[0m', "\n";
+ use Term::ANSIColor qw(colorvalid);
+ my $valid = colorvalid ('blue bold', 'on_magenta');
+ print "Color string is ", $valid ? "valid\n" : "invalid\n";
+
use Term::ANSIColor qw(:constants);
print BOLD, BLUE, "This text is in bold blue.\n", RESET;
=head1 DESCRIPTION
This module has two interfaces, one through color() and colored() and the
-other through constants. It also offers the utility functions uncolor()
-and colorstrip(), which have to be explicitly imported to be used (see
-L</SYNOPSIS>).
+other through constants. It also offers the utility functions uncolor(),
+colorstrip(), and colorvalid(), which have to be explicitly imported to be
+used (see L</SYNOPSIS>).
=head2 Function Interface
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).
+handle, or do anything else with it that you might care to). color()
+throws an exception if given an invalid attribute, so you can also use it
+to check attribute names for validity (see L</EXAMPLES>).
uncolor() performs the opposite translation, turning escape sequences
into a list of strings.
returning the modified strings separately in array context or joined
together in scalar context. Its arguments are not modified.
+colorvalid() takes attribute strings the same as color() and returns true
+if all attributes are known and false otherwise.
+
The recognized non-color attributes are clear, reset, bold, dark, faint,
underline, underscore, blink, reverse, and concealed. Clear and reset
(reset to default attributes), dark and faint (dim and saturated), and
=head2 Constant Interface
Alternately, if you import C<:constants>, you can use the constants CLEAR,
-RESET, BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK, REVERSE, CONCEALED,
-BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, ON_BLACK, ON_RED,
-ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, and ON_WHITE directly.
-These are the same as color('attribute') and can be used if you prefer
-typing:
+RESET, BOLD, DARK, FAINT, UNDERLINE, UNDERSCORE, BLINK, REVERSE,
+CONCEALED, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE,
+ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, and
+ON_WHITE directly. These are the same as color('attribute') and can be
+used if you prefer typing:
print BOLD BLUE ON_WHITE "Text", RESET, "\n";
clear bold faint under blink reverse conceal
------------------------------------------------------------------------
- xterm yes yes no yes bold yes yes
+ xterm yes yes no yes yes yes yes
linux yes yes yes bold yes yes no
rxvt yes yes no yes bold/black yes no
dtterm yes yes yes yes reverse yes yes
# under the same terms as Perl itself.
use strict;
-use Test::More tests => 43;
+use Test::More tests => 47;
BEGIN {
delete $ENV{ANSI_COLORS_DISABLED};
- use_ok ('Term::ANSIColor', qw/:pushpop color colored uncolor colorstrip/);
+ use_ok ('Term::ANSIColor',
+ qw/:pushpop color colored uncolor colorstrip colorvalid/);
}
# Various basic tests.
# Make sure DARK is exported. This was omitted in versions prior to 1.07.
is ((DARK "testing"), "\e[2mtesting\e[0m", 'DARK');
+# Check faint as a synonym for dark.
+is (colored ('test', 'faint'), "\e[2mtest\e[0m", 'colored supports faint');
+is ((FAINT "test"), "\e[2mtest\e[0m", '...and the FAINT constant works');
+
# Test colored with 0 and EACHLINE.
$Term::ANSIColor::EACHLINE = "\n";
is (colored ('0', 'blue', 'bold'), "\e[34;1m0\e[0m",
"\e[2cSome other code\e and stray [0m stuff",
'colorstrip does not remove non-color stuff');
+# Test colorvalid.
+is (colorvalid ("blue bold dark", "blink on_green"), 1,
+ 'colorvalid returns true for valid attributes');
+is (colorvalid ("green orange"), undef,
+ '...and false for invalid attributes');
+
# Test error handling.
my $output = eval { color 'chartreuse' };
is ($output, undef, 'color on unknown color name fails');