# Term::ANSIColor -- Color screen output using ANSI escape sequences.
-# $Id: ANSIColor.pm 64 2007-03-23 17:58:18Z eagle $
#
-# Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006
-# by Russ Allbery <rra@stanford.edu> and Zenin
+# Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009
+# Russ Allbery <rra@stanford.edu> and Zenin
+# PUSH/POP support submitted 2007 by openmethods.com voice solutions
#
# This program is free software; you may redistribute it and/or modify it
# under the same terms as Perl itself.
package Term::ANSIColor;
require 5.001;
+$VERSION = '2.00';
+
use strict;
-use vars qw($AUTOLOAD $AUTORESET $EACHLINE @ISA @EXPORT @EXPORT_OK
- %EXPORT_TAGS $VERSION %attributes %attributes_r);
+use vars qw($AUTOLOAD $AUTOLOCAL $AUTORESET @COLORLIST @COLORSTACK $EACHLINE
+ @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION %ATTRIBUTES
+ %ATTRIBUTES_R);
use Exporter ();
-@ISA = qw(Exporter);
-@EXPORT = qw(color colored);
-@EXPORT_OK = qw(uncolor);
-%EXPORT_TAGS = (constants => [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)]);
-Exporter::export_ok_tags ('constants');
-
-$VERSION = '1.12';
+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);
+ @ISA = qw(Exporter);
+ @EXPORT = qw(color colored);
+ @EXPORT_OK = qw(uncolor);
+ %EXPORT_TAGS = (constants => \@COLORLIST,
+ pushpop => [ @COLORLIST,
+ qw(PUSHCOLOR POPCOLOR LOCALCOLOR) ]);
+ Exporter::export_ok_tags ('pushpop');
+}
##############################################################################
# Internal data structures
##############################################################################
-%attributes = ('clear' => 0,
+%ATTRIBUTES = ('clear' => 0,
'reset' => 0,
'bold' => 1,
'dark' => 2,
+ 'faint' => 2,
'underline' => 4,
'underscore' => 4,
'blink' => 5,
'white' => 37, 'on_white' => 47);
# Reverse lookup. Alphabetically first name for a sequence is preferred.
-for (reverse sort keys %attributes) {
- $attributes_r{$attributes{$_}} = $_;
+for (reverse sort keys %ATTRIBUTES) {
+ $ATTRIBUTES_R{$ATTRIBUTES{$_}} = $_;
}
##############################################################################
#
# The sub also needs to handle the case where it has no arguments correctly.
# Maintaining all of this as separate subs would be a major nightmare, as well
-# as duplicate the %attributes hash, so instead we define an AUTOLOAD sub to
+# as duplicate the %ATTRIBUTES hash, so instead we define an AUTOLOAD sub to
# define the constant subs on demand. To do that, we check the name of the
# called sub against the list of attributes, and if it's an all-caps version
# of one of them, we define the sub on the fly and then run it.
#
-# If the environment variable ANSI_COLORS_DISABLED is set, turn all of the
-# generated subs into pass-through functions that don't add any escape
-# sequences. This is to make it easier to write scripts that also work on
-# systems without any ANSI support, like Windows consoles.
+# If the environment variable ANSI_COLORS_DISABLED is set, just return the
+# arguments without adding any escape sequences. This is to make it easier to
+# write scripts that also work on systems without any ANSI support, like
+# Windows consoles.
sub AUTOLOAD {
- my $enable_colors = !defined $ENV{ANSI_COLORS_DISABLED};
+ if (defined $ENV{ANSI_COLORS_DISABLED}) {
+ return join ('', @_);
+ }
my $sub;
($sub = $AUTOLOAD) =~ s/^.*:://;
- my $attr = $attributes{lc $sub};
+ my $attr = $ATTRIBUTES{lc $sub};
if ($sub =~ /^[A-Z_]+$/ && defined $attr) {
- $attr = $enable_colors ? "\e[" . $attr . 'm' : '';
+ $attr = "\e[" . $attr . 'm';
eval qq {
sub $AUTOLOAD {
if (\$AUTORESET && \@_) {
- '$attr' . "\@_" . "\e[0m";
+ return '$attr' . join ('', \@_) . "\e[0m";
+ } elsif (\$AUTOLOCAL && \@_) {
+ return PUSHCOLOR ('$attr') . join ('', \@_) . POPCOLOR;
} else {
- ('$attr' . "\@_");
+ return '$attr' . join ('', \@_);
}
}
};
}
}
+# Append a new color to the top of the color stack and return the top of
+# the stack.
+sub PUSHCOLOR {
+ my ($text) = @_;
+ my ($color) = ($text =~ m/^((?:\e\[[\d;]+m)+)/);
+ if (@COLORSTACK) {
+ $color = $COLORSTACK[-1] . $color;
+ }
+ push (@COLORSTACK, $color);
+ return $text;
+}
+
+# Pop the color stack and return the new top of the stack (or reset, if
+# the stack is empty).
+sub POPCOLOR {
+ pop @COLORSTACK;
+ if (@COLORSTACK) {
+ return $COLORSTACK[-1] . join ('', @_);
+ } else {
+ return RESET (@_);
+ }
+}
+
+# Surround arguments with a push and a pop.
+sub LOCALCOLOR {
+ return PUSHCOLOR (join ('', @_)) . POPCOLOR ();
+}
+
##############################################################################
# Implementation (attribute string form)
##############################################################################
my $attribute = '';
foreach (@codes) {
$_ = lc $_;
- unless (defined $attributes{$_}) {
+ unless (defined $ATTRIBUTES{$_}) {
require Carp;
Carp::croak ("Invalid attribute name $_");
}
- $attribute .= $attributes{$_} . ';';
+ $attribute .= $ATTRIBUTES{$_} . ';';
}
chop $attribute;
- ($attribute ne '') ? "\e[${attribute}m" : undef;
+ return ($attribute ne '') ? "\e[${attribute}m" : undef;
}
# Return a list of named color attributes for a given set of escape codes.
}
for (@nums) {
$_ += 0; # Strip leading zeroes
- my $name = $attributes_r{$_};
+ my $name = $ATTRIBUTES_R{$_};
if (!defined $name) {
require Carp;
Carp::croak ("No name for escape sequence $_" );
}
push (@result, $name);
}
- @result;
+ return @result;
}
# Given a string and a set of attributes, returns the string surrounded by
return $string if defined $ENV{ANSI_COLORS_DISABLED};
if (defined $EACHLINE) {
my $attr = color (@codes);
- join '',
- map { $_ ne $EACHLINE ? $attr . $_ . "\e[0m" : $_ }
+ return join '',
+ map { ($_ ne $EACHLINE) ? $attr . $_ . "\e[0m" : $_ }
grep { length ($_) > 0 }
split (/(\Q$EACHLINE\E)/, $string);
} else {
- color (@codes) . $string . "\e[0m";
+ return color (@codes) . $string . "\e[0m";
}
}
Term::ANSIColor - Color screen output using ANSI escape sequences
+=for stopwords
+cyan colorize namespace runtime TMTOWTDI cmd.exe 4nt.exe command.com NT
+ESC Delvare SSH OpenSSH aixterm ECMA-048 Fraktur overlining Zenin
+reimplemented Allbery PUSHCOLOR POPCOLOR LOCALCOLOR openmethods.com
+
=head1 SYNOPSIS
use Term::ANSIColor;
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";
+ {
+ local $Term::ANSIColor::AUTORESET = 1;
+ print BOLD BLUE "This text is in bold blue.\n";
+ print "This text is normal.\n";
+ }
+
+ use Term::ANSIColor qw(:pushpop);
+ print PUSHCOLOR RED ON_GREEN "This text is red on green.\n";
+ print PUSHCOLOR BLUE "This text is blue on green.\n";
+ print RESET BLUE "This text is just blue.\n";
+ print POPCOLOR "Back to red on green.\n";
+ print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\n";
+ print "This text is red on green.\n";
+ {
+ local $Term::ANSIColor::AUTOLOCAL = 1;
+ print ON_BLUE "This text is red on blue.\n";
+ print "This text is red on green.\n";
+ }
+ print POPCOLOR "Back to whatever we started as.\n";
=head1 DESCRIPTION
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).
+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, cyan, white, 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.
+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
+underline and underscore are equivalent, so use whichever is the most
+intuitive to you. The recognized foreground color attributes are black,
+red, green, yellow, blue, magenta, cyan, and white. The recognized
+background color attributes are on_black, on_red, on_green, on_yellow,
+on_blue, on_magenta, on_cyan, and on_white. Case is not significant.
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.
+terminals may not support any of these sequences. Dark and faint, 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.
+C<clear> or C<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
$Term::ANSIColor::EACHLINE to C<"\n"> to use this feature.
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, 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";
print BOLD BLUE "Text\n";
-will reset the display mode afterwards, whereas:
+will reset the display mode afterward, whereas:
print BOLD, BLUE, "Text\n";
twenty-two in the constants interface. On the flip side, the constants
interface has the advantage of better compile time error checking, since
misspelled names of colors or attributes in calls to color() and colored()
-won't be caught until runtime whereas misspelled names of constants will be
-caught at compile time. So, pollute your namespace with almost two dozen
-subroutines that you may not even use that often, or risk a silly bug by
-mistyping an attribute. Your choice, TMTOWTDI after all.
+won't be caught until runtime whereas misspelled names of constants will
+be caught at compile time. So, pollute your namespace with almost two
+dozen subroutines that you may not even use that often, or risk a silly
+bug by mistyping an attribute. Your choice, TMTOWTDI after all.
+
+As of Term::ANSIColor 2.0, you can import C<:pushpop> and maintain a stack
+of colors using PUSHCOLOR, POPCOLOR, and LOCALCOLOR. PUSHCOLOR takes the
+attribute string that starts its argument and pushes it onto a stack of
+attributes. POPCOLOR removes the top of the stack and restores the
+previous attributes set by the argument of a prior PUSHCOLOR. LOCALCOLOR
+surrounds its argument in a PUSHCOLOR and POPCOLOR so that the color
+resets afterward.
+
+When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it's particularly
+important to not put commas between the constants.
+
+ print PUSHCOLOR BLUE "Text\n";
+
+will correctly push BLUE onto the top of the stack.
+
+ print PUSHCOLOR, BLUE, "Text\n"; # wrong!
+
+will not, and a subsequent pop won't restore the correct attributes.
+PUSHCOLOR pushes the attributes set by its argument, which is normally a
+string of color constants. It can't ask the terminal what the current
+attributes are.
=head1 DIAGNOSTICS
=item ANSI_COLORS_DISABLED
If this environment variable is set, all of the functions defined by this
-module (color(), colored(), and all of the constants not previously used in
-the program) will not output any escape sequences and instead will just
+module (color(), colored(), and all of the constants not previously used
+in the program) will not output any escape sequences and instead will just
return the empty string or pass through the original text as appropriate.
This is intended to support easy use of scripts using this module on
platforms that don't support ANSI escape sequences.
but the syntax of Perl doesn't allow this. You need a comma after the
string. (Of course, you may consider it a bug that commas between all the
-constants aren't required, in which case you may feel free to insert commas
-unless you're using $Term::ANSIColor::AUTORESET.)
+constants aren't required, in which case you may feel free to insert
+commas unless you're using $Term::ANSIColor::AUTORESET or
+PUSHCOLOR/POPCOLOR.)
For easier debugging, you may prefer to always use the commas when not
-setting $Term::ANSIColor::AUTORESET so that you'll get a fatal compile error
-rather than a warning.
+setting $Term::ANSIColor::AUTORESET or PUSHCOLOR/POPCOLOR so that you'll
+get a fatal compile error rather than a warning.
=head1 NOTES
The codes generated by this module are standard terminal control codes,
-complying with ECMA-48 and ISO 6429 (generally referred to as "ANSI color"
-for the color codes). The non-color control codes (bold, dark, italic,
-underline, and reverse) are part of the earlier ANSI X3.64 standard for
-control sequences for video terminals and peripherals.
+complying with ECMA-048 and ISO 6429 (generally referred to as "ANSI
+color" for the color codes). The non-color control codes (bold, dark,
+italic, underline, and reverse) are part of the earlier ANSI X3.64
+standard for control sequences for video terminals and peripherals.
Note that not all displays are ISO 6429-compliant, or even X3.64-compliant
-(or are even attempting to be so). This module will not work as expected on
-displays that do not honor these escape sequences, such as cmd.exe, 4nt.exe,
-and command.com under either Windows NT or Windows 2000. They may just be
-ignored, or they may display as an ESC character followed by some apparent
-garbage.
+(or are even attempting to be so). This module will not work as expected
+on displays that do not honor these escape sequences, such as cmd.exe,
+4nt.exe, and command.com under either Windows NT or Windows 2000. They
+may just be ignored, or they may display as an ESC character followed by
+some apparent garbage.
Jean Delvare provided the following table of different common terminal
-emulators and their support for the various attributes and others have helped
-me flesh it out:
+emulators and their support for the various attributes and others have
+helped me flesh it out:
- clear bold dark under blink reverse conceal
+ clear bold faint under blink reverse conceal
------------------------------------------------------------------------
xterm yes yes no yes bold yes yes
linux yes yes yes bold yes yes no
Mac Terminal yes yes no yes yes yes yes
Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation under
-Cygwin on Windows NT, and Mac Terminal is the Terminal application in Mac OS
-X. Where the entry is other than yes or no, that emulator displays the
+Cygwin on Windows NT, and Mac Terminal is the Terminal application in Mac
+OS X. Where the entry is other than yes or no, that emulator displays the
given attribute as something else instead. Note that on an aixterm, clear
-doesn't reset colors; you have to explicitly set the colors back to what you
-want. More entries in this table are welcome.
-
-Note that codes 3 (italic), 6 (rapid blink), and 9 (strikethrough) are
-specified in ANSI X3.64 and ECMA-048 but are not commonly supported by most
-displays and emulators and therefore aren't supported by this module at the
-present time. ECMA-048 also specifies a large number of other attributes,
-including a sequence of attributes for font changes, Fraktur characters,
-double-underlining, framing, circling, and overlining. As none of these
-attributes are widely supported or useful, they also aren't currently
-supported by this module.
+doesn't reset colors; you have to explicitly set the colors back to what
+you want. More entries in this table are welcome.
+
+Note that codes 3 (italic), 6 (rapid blink), and 9 (strike-through) are
+specified in ANSI X3.64 and ECMA-048 but are not commonly supported by
+most displays and emulators and therefore aren't supported by this module
+at the present time. ECMA-048 also specifies a large number of other
+attributes, including a sequence of attributes for font changes, Fraktur
+characters, double-underlining, framing, circling, and overlining. As
+none of these attributes are widely supported or useful, they also aren't
+currently supported by this module.
=head1 SEE ALSO
ECMA-048 is available on-line (at least at the time of this writing) at
L<http://www.ecma-international.org/publications/standards/ECMA-048.HTM>.
-ISO 6429 is available from ISO for a charge; the author of this module does
-not own a copy of it. Since the source material for ISO 6429 was ECMA-048
-and the latter is available for free, there seems little reason to obtain
-the ISO standard.
+ISO 6429 is available from ISO for a charge; the author of this module
+does not own a copy of it. Since the source material for ISO 6429 was
+ECMA-048 and the latter is available for free, there seems little reason
+to obtain the ISO standard.
-The current version of this module is always available from its web site at
-L<http://www.eyrie.org/~eagle/software/ansicolor/>. It is also part of the
-Perl core distribution as of 5.6.0.
+The current version of this module is always available from its web site
+at L<http://www.eyrie.org/~eagle/software/ansicolor/>. It is also part of
+the Perl core distribution as of 5.6.0.
=head1 AUTHORS
Original idea (using constants) by Zenin, reimplemented using subs by Russ
-Allbery <rra@stanford.edu>, and then combined with the original idea by Russ
-with input from Zenin. Russ Allbery now maintains this module.
+Allbery <rra@stanford.edu>, and then combined with the original idea by
+Russ with input from Zenin. Russ Allbery now maintains this module.
=head1 COPYRIGHT AND LICENSE
-Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006 Russ Allbery
-<rra@stanford.edu> and Zenin. This program is free software; you may
-redistribute it and/or modify it under the same terms as Perl itself.
+Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009 Russ
+Allbery <rra@stanford.edu> and Zenin. This program is free software; you
+may redistribute it and/or modify it under the same terms as Perl itself.
+
+PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
+voice solutions.
=cut
#!/usr/bin/perl
-# $Id: basic.t 55 2006-06-22 17:56:02Z eagle $
#
# t/basic.t -- Test suite for the Term::ANSIColor Perl module.
+#
+# Copyright 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2009
+# Russ Allbery <rra@stanford.edu>
+#
+# This program is free software; you may redistribute it and/or modify it
+# under the same terms as Perl itself.
-##############################################################################
-# Ensure module can be loaded
-##############################################################################
-
-BEGIN { $| = 1; print "1..16\n" }
-END { print "not ok 1\n" unless $loaded }
-delete $ENV{ANSI_COLORS_DISABLED};
-use Term::ANSIColor qw(:constants color colored uncolor);
-$loaded = 1;
-print "ok 1\n";
-
-##############################################################################
-# Test suite
-##############################################################################
-
-# Test simple color attributes.
-if (color ('blue on_green', 'bold') eq "\e[34;42;1m") {
- print "ok 2\n";
-} else {
- print "not ok 2\n";
-}
-
-# Test colored.
-if (colored ("testing", 'blue', 'bold') eq "\e[34;1mtesting\e[0m") {
- print "ok 3\n";
-} else {
- print "not ok 3\n";
-}
+use strict;
+use Test::More tests => 29;
-# Test the constants.
-if (BLUE BOLD "testing" eq "\e[34m\e[1mtesting") {
- print "ok 4\n";
-} else {
- print "not ok 4\n";
+BEGIN {
+ delete $ENV{ANSI_COLORS_DISABLED};
+ use_ok ('Term::ANSIColor', qw/:pushpop color colored uncolor/);
}
-# Test AUTORESET.
+# Various basic tests.
+is (color ('blue on_green', 'bold'), "\e[34;42;1m", 'Simple attributes');
+is (colored ('testing', 'blue', 'bold'), "\e[34;1mtesting\e[0m", 'colored');
+is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting", 'Constants');
$Term::ANSIColor::AUTORESET = 1;
-if (BLUE BOLD "testing" eq "\e[34m\e[1mtesting\e[0m\e[0m") {
- print "ok 5\n";
-} else {
- print "not ok 5\n";
-}
-
-# Test EACHLINE.
+is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting\e[0m\e[0m", 'AUTORESET');
$Term::ANSIColor::EACHLINE = "\n";
-if (colored ("test\n\ntest", 'bold')
- eq "\e[1mtest\e[0m\n\n\e[1mtest\e[0m") {
- print "ok 6\n";
-} else {
- print colored ("test\n\ntest", 'bold'), "\n";
- print "not ok 6\n";
-}
-
-# Test EACHLINE with multiple trailing delimiters.
+is (colored ("test\n\ntest", 'bold'), "\e[1mtest\e[0m\n\n\e[1mtest\e[0m",
+ 'EACHLINE');
$Term::ANSIColor::EACHLINE = "\r\n";
-if (colored ("test\ntest\r\r\n\r\n", 'bold')
- eq "\e[1mtest\ntest\r\e[0m\r\n\r\n") {
- print "ok 7\n";
-} else {
- print "not ok 7\n";
-}
-
-# Test the array ref form.
+is (colored ("test\ntest\r\r\n\r\n", 'bold'),
+ "\e[1mtest\ntest\r\e[0m\r\n\r\n",
+ 'EACHLINE with multiple delimiters');
$Term::ANSIColor::EACHLINE = "\n";
-if (colored (['bold', 'on_green'], "test\n", "\n", "test")
- eq "\e[1;42mtest\e[0m\n\n\e[1;42mtest\e[0m") {
- print "ok 8\n";
-} else {
- print colored (['bold', 'on_green'], "test\n", "\n", "test");
- print "not ok 8\n";
-}
-
-# Test uncolor.
-my @names = uncolor ('1;42', "\e[m", '', "\e[0m");
-if (join ('|', @names) eq 'bold|on_green|clear') {
- print "ok 9\n";
-} else {
- print join ('|', @names), "\n";
- print "not ok 9\n";
-}
+is (colored (['bold', 'on_green'], "test\n", "\n", "test"),
+ "\e[1;42mtest\e[0m\n\n\e[1;42mtest\e[0m",
+ 'colored with reference to array');
+is_deeply ([ uncolor ('1;42', "\e[m", '', "\e[0m") ],
+ [ qw/bold on_green clear/ ], 'uncolor');
-# Test ANSI_COLORS_DISABLED.
+# Several tests for ANSI_COLORS_DISABLED.
$ENV{ANSI_COLORS_DISABLED} = 1;
-if (color ('blue') eq '') {
- print "ok 10\n";
-} else {
- print "not ok 10\n";
-}
-if (colored ('testing', 'blue', 'on_red') eq 'testing') {
- print "ok 11\n";
-} else {
- print "not ok 11\n";
-}
-if (GREEN 'testing' eq 'testing') {
- print "ok 12\n";
-} else {
- print "not ok 12\n";
-}
+is (color ('blue'), '', 'color support for ANSI_COLORS_DISABLED');
+is (colored ('testing', 'blue', 'on_red'), 'testing',
+ 'colored support for ANSI_COLORS_DISABLED');
+is ((GREEN 'testing'), 'testing', 'Constant support for ANSI_COLORS_DISABLED');
delete $ENV{ANSI_COLORS_DISABLED};
# Make sure DARK is exported. This was omitted in versions prior to 1.07.
-if (DARK "testing" eq "\e[2mtesting\e[0m") {
- print "ok 13\n";
-} else {
- print "not ok 13\n";
-}
+is ((DARK "testing"), "\e[2mtesting\e[0m", 'DARK');
# Test colored with 0 and EACHLINE.
$Term::ANSIColor::EACHLINE = "\n";
-if (colored ('0', 'blue', 'bold') eq "\e[34;1m0\e[0m") {
- print "ok 14\n";
-} else {
- print "not ok 14\n";
-}
-if (colored ("0\n0\n\n", 'blue', 'bold')
- eq "\e[34;1m0\e[0m\n\e[34;1m0\e[0m\n\n") {
- print "ok 15\n";
-} else {
- print "not ok 15\n";
-}
+is (colored ('0', 'blue', 'bold'), "\e[34;1m0\e[0m",
+ 'colored with 0 and EACHLINE');
+is (colored ("0\n0\n\n", 'blue', 'bold'), "\e[34;1m0\e[0m\n\e[34;1m0\e[0m\n\n",
+ 'colored with 0, EACHLINE, and multiple lines');
# Test colored with the empty string and EACHLINE.
-if (colored ('', 'blue', 'bold') eq '') {
- print "ok 16\n";
-} else {
- print "not ok 16\n";
-}
+is (colored ('', 'blue', 'bold'), '',
+ 'colored with an empty string and EACHLINE');
+
+# Test push and pop support.
+$Term::ANSIColor::AUTORESET = 0;
+is ((PUSHCOLOR RED ON_GREEN "text"), "\e[31m\e[42mtext",
+ 'PUSHCOLOR does not break constants');
+is ((PUSHCOLOR BLUE "text"), "\e[34mtext", '...and adding another level');
+is ((RESET BLUE "text"), "\e[0m\e[34mtext", '...and using reset');
+is ((POPCOLOR "text"), "\e[31m\e[42mtext", '...and POPCOLOR works');
+is ((LOCALCOLOR GREEN ON_BLUE "text"), "\e[32m\e[44mtext\e[31m\e[42m",
+ 'LOCALCOLOR');
+$Term::ANSIColor::AUTOLOCAL = 1;
+is ((ON_BLUE "text"), "\e[44mtext\e[31m\e[42m", 'AUTOLOCAL');
+$Term::ANSIColor::AUTOLOCAL = 0;
+is ((POPCOLOR "text"), "\e[0mtext", 'POPCOLOR with empty stack');
+
+# Test push and pop support with the syntax from the original openmethods.com
+# submission, which uses a different coding style.
+is (PUSHCOLOR (RED ON_GREEN), "\e[31m\e[42m",
+ 'PUSHCOLOR with explict argument');
+is (PUSHCOLOR (BLUE), "\e[34m", '...and another explicit argument');
+is (RESET . BLUE . "text", "\e[0m\e[34mtext",
+ '...and constants with concatenation');
+is (POPCOLOR . "text", "\e[31m\e[42mtext",
+ '...and POPCOLOR works without an argument');
+is (LOCALCOLOR(GREEN . ON_BLUE . "text"), "\e[32m\e[44mtext\e[31m\e[42m",
+ 'LOCALCOLOR with two arguments');
+is (POPCOLOR . "text", "\e[0mtext", 'POPCOLOR with no arguments');