--- /usr/local/lib/perl5/5.8.8/Term/ANSIColor.pm 2007-03-23 12:58:18.000000000 -0500 +++ Term/ANSIColor.pm 2007-09-20 12:58:17.000000000 -0500 @@ -4,6 +4,8 @@ # Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006 # by Russ Allbery <rra@stanford.edu> and Zenin # +# Autopop patch 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. # @@ -19,20 +21,30 @@ use strict; use vars qw($AUTOLOAD $AUTORESET $EACHLINE @ISA @EXPORT @EXPORT_OK + @COLORLIST @COLORSTACK $AUTOLOCAL %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'); +BEGIN{ +@COLORLIST = qw(CLEAR 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); +eval "sub $_(;\$)" for @COLORLIST; +push @COLORLIST, 'RESET'; +}; +%EXPORT_TAGS = (constants => \@COLORLIST, + pushpop => [@COLORLIST, qw(PUSHCOLOR POPCOLOR LOCALCOLOR)] +); +Exporter::export_ok_tags ('constants','pushpop'); + +$VERSION = '1.13'; -$VERSION = '1.12'; +sub RESET(){"\e[0m"}; # a.k.a "use constant RESET => "\e[0m"; ############################################################################## # Internal data structures @@ -57,7 +69,8 @@ 'cyan' => 36, 'on_cyan' => 46, 'white' => 37, 'on_white' => 47); -# Reverse lookup. Alphabetically first name for a sequence is preferred. +# Reverse lookup. Alphabetically first name for a sequence is preferred, +# so alpha-earlier names clobber alpha-later ones. for (reverse sort keys %attributes) { $attributes_r{$attributes{$_}} = $_; } @@ -95,15 +108,17 @@ my $attr = $attributes{lc $sub}; if ($sub =~ /^[A-Z_]+$/ && defined $attr) { $attr = $enable_colors ? "\e[" . $attr . 'm' : ''; - eval qq { - sub $AUTOLOAD { + eval <<"AUTOBLOCK"; + sub $AUTOLOAD(;\$) { if (\$AUTORESET && \@_) { '$attr' . "\@_" . "\e[0m"; + } elsif (\$AUTOLOCAL && \@_) { + PUSHCOLOR('$attr')."\@_".POPCOLOR(); } else { ('$attr' . "\@_"); } } - }; +AUTOBLOCK goto &$AUTOLOAD; } else { require Carp; @@ -111,6 +126,30 @@ } } + +############################################################################## +# Implementation of PUSHCOLOR and POPCOLOR and LOCALCOLOR +############################################################################## + +# append new color to top of colorstack if any and return top of stack +sub PUSHCOLOR($){ + my $newcolor = $COLORSTACK[-1].shift; + push @COLORSTACK, $newcolor; + $newcolor +}; + +# pop stack and return new top // reset +sub POPCOLOR(){ + pop @COLORSTACK; + @COLORSTACK or @COLORSTACK = (RESET); + $COLORSTACK[-1]; +}; + +# push, pass, pop +sub LOCALCOLOR($$){ + PUSHCOLOR($_[0]).$_[1].POPCOLOR +}; + ############################################################################## # Implementation (attribute string form) ############################################################################## @@ -223,6 +262,22 @@ 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 will be green on blue\n"); + print "but this is red on green.\n"; + { + local $Term::ANSIColor::AUTOLOCAL = 1; + print ON_BLUE "this and only this is red on blue\n"; + print "but this, again, is red on green.\n"; + } + print POPCOLOR."back to plain, or whatever we were a dozen lines ago.\n"; + + =head1 DESCRIPTION This module has two interfaces, one through color() and colored() and the @@ -316,6 +371,14 @@ 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 late 2007, it is now possible to import :pushpop instead of :constants +and stack ANSIColor attributes using the three additional points of pollution +PUSHCOLOR, POPCOLOR, and LOCALCOLOR and $Term::ANSIColor::AUTOLOCAL. +See the synopsis section for working examples. + + local @Term::ANSIColor::COLORSTACK = (RESET); # start fresh + + =head1 DIAGNOSTICS =over 4 @@ -467,7 +530,8 @@ 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. +with input from Zenin. Russ Allbery now maintains this module. Pushpop +contributed by openmethods.com voice solutions. =head1 COPYRIGHT AND LICENSE