This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Term::ANSIColor 1.04.
authorJarkko Hietaniemi <jhi@iki.fi>
Tue, 10 Jul 2001 13:09:34 +0000 (13:09 +0000)
committerJarkko Hietaniemi <jhi@iki.fi>
Tue, 10 Jul 2001 13:09:34 +0000 (13:09 +0000)
p4raw-id: //depot/perl@11254

MANIFEST
lib/Term/ANSIColor.pm
lib/Term/ANSIColor/ChangeLog [new file with mode: 0644]
lib/Term/ANSIColor/README [new file with mode: 0644]
lib/Term/ANSIColor/test.pl

index ba99ec8..80dd5ef 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -15,11 +15,11 @@ Changes5.003                Differences between 5.002 and 5.003
 Changes5.004           Differences between 5.003 and 5.004
 Changes5.005           Differences between 5.004 and 5.005
 Changes5.6             Differences between 5.005 and 5.6
+config_h.SH            Produces config.h
 configpm               Produces lib/Config.pm
 Configure              Portability tool
 configure.com          Configure-equivalent for VMS
 configure.gnu          Crude emulation of GNU configure
-config_h.SH            Produces config.h
 cop.h                  Control operator header
 Copying                        The GNU General Public License
 Cross/README           Cross-compilation
@@ -47,9 +47,9 @@ embedvar.h            C namespace management
 epoc/config.sh         EPOC port config.sh template
 epoc/createpkg.pl      EPOC port generate PKG file
 epoc/epoc.c            EPOC port
+epoc/epoc_stubs.c      EPOC port
 epoc/epocish.c         EPOC port
 epoc/epocish.h         EPOC port
-epoc/epoc_stubs.c      EPOC port
 epoc/link.pl           EPOC port link a exe
 ext/attrs.t                    See if attrs works with C<sub : attrs>
 ext/attrs/attrs.pm             attrs extension Perl module
@@ -114,10 +114,10 @@ ext/Data/Dumper/t/dumper.t        See if Data::Dumper works
 ext/Data/Dumper/t/overload.t   See if Data::Dumper works for overloaded data
 ext/Data/Dumper/Todo           Data pretty printer, futures
 ext/DB_File/Changes    Berkeley DB extension change log
-ext/DB_File/dbinfo     Berkeley DB database version checker
 ext/DB_File/DB_File.pm Berkeley DB extension Perl module
 ext/DB_File/DB_File.xs Berkeley DB extension external subroutines
 ext/DB_File/DB_File_BS Berkeley DB extension mkbootstrap fodder
+ext/DB_File/dbinfo     Berkeley DB database version checker
 ext/DB_File/hints/dynixptx.pl  Hint for DB_File for named architecture
 ext/DB_File/hints/sco.pl       Hint for DB_File for named architecture
 ext/DB_File/Makefile.PL        Berkeley DB extension makefile writer
@@ -146,7 +146,6 @@ ext/Digest/MD5/t/align.t    See if Digest::MD5 extension works
 ext/Digest/MD5/t/badfile.t     See if Digest::MD5 extension works
 ext/Digest/MD5/t/files.t       See if Digest::MD5 extension works
 ext/Digest/MD5/typemap         Digest::MD5 extension
-ext/DynaLoader/dlutils.c       Dynamic loader utilities for dl_*.xs files
 ext/DynaLoader/dl_aix.xs       AIX implementation
 ext/DynaLoader/dl_beos.xs      BeOS implementation
 ext/DynaLoader/dl_dld.xs       GNU dld style implementation
@@ -160,6 +159,7 @@ ext/DynaLoader/dl_next.xs   NeXT implementation
 ext/DynaLoader/dl_none.xs      Stub implementation
 ext/DynaLoader/dl_vmesa.xs     VM/ESA implementation
 ext/DynaLoader/dl_vms.xs       VMS implementation
+ext/DynaLoader/dlutils.c       Dynamic loader utilities for dl_*.xs files
 ext/DynaLoader/DynaLoader_pm.PL        Dynamic Loader perl module
 ext/DynaLoader/hints/aix.pl    Hint for DynaLoader for named architecture
 ext/DynaLoader/hints/linux.pl  Hint for DynaLoader for named architecture
@@ -992,11 +992,11 @@ lib/Memoize/t/normalize.t Memoize
 lib/Memoize/t/prototype.t      Memoize
 lib/Memoize/t/speed.t          Memoize
 lib/Memoize/t/tie.t            Memoize
-lib/Memoize/t/tiefeatures.t    Memoize
 lib/Memoize/t/tie_gdbm.t       Memoize
 lib/Memoize/t/tie_ndbm.t       Memoize
 lib/Memoize/t/tie_sdbm.t       Memoize
 lib/Memoize/t/tie_storable.t   Memoize
+lib/Memoize/t/tiefeatures.t    Memoize
 lib/Memoize/t/unmemoize.t      Memoize
 lib/Memoize/TODO               Memoize
 lib/Net/ChangeLog.libnet       libnet
@@ -1091,6 +1091,8 @@ lib/Symbol.t                      See if Symbol works
 lib/syslog.pl                  Perl library supporting syslogging
 lib/tainted.pl                 Old code for tainting
 lib/Term/ANSIColor.pm          Perl module supporting termcap usage
+lib/Term/ANSIColor/ChangeLog   Term::ANSIColor
+lib/Term/ANSIColor/README      Term::ANSIColor
 lib/Term/ANSIColor/test.pl     See if Term::ANSIColor works
 lib/Term/Cap.pm                        Perl module supporting termcap usage
 lib/Term/Complete.pm           A command completion subroutine
@@ -1589,8 +1591,8 @@ opcode.pl                 Opcode header generatore
 opnames.h                      Automatically generated opcode header
 os2/Changes                    Changelog for OS/2 port
 os2/diff.configure             Patches to Configure
-os2/dlfcn.h                    Addon for dl_open
 os2/dl_os2.c                   Addon for dl_open
+os2/dlfcn.h                    Addon for dl_open
 os2/Makefile.SHs               Shared library generation for OS/2
 os2/os2.c                      Additional code for OS/2
 os2/os2.sym                    Additional symbols to export
@@ -1633,10 +1635,10 @@ os2/OS2/REXX/t/rx_tievar.t      DLL access module
 os2/OS2/REXX/t/rx_tieydb.t     DLL access module
 os2/OS2/REXX/t/rx_varset.t     DLL access module
 os2/OS2/REXX/t/rx_vrexx.t      DLL access module
+os2/os2_base.t                 Additional tests for builtin methods
 os2/os2add.sym                 Overriding symbols to export
 os2/os2ish.h                   Header for OS/2
 os2/os2thread.h                        pthread-like typedefs
-os2/os2_base.t                 Additional tests for builtin methods
 os2/perl2cmd.pl                        Corrects installed binaries under OS/2
 patchlevel.h                   The current patch level of perl
 perl.c                         main()
@@ -1655,8 +1657,8 @@ perly.c                           A byacc'ed perly.y
 perly.fixer                    A program to remove yacc stack limitations
 perly.h                                The header file for perly.c
 perly.y                                Yacc grammar for perl
-perlyline.pl                   Perl code to fix #line directives and gcc warnings in perly.c
 perly_c.diff                   Fixup perly.c to allow recursion
+perlyline.pl                   Perl code to fix #line directives and gcc warnings in perly.c
 plan9/aperl                    Shell to make Perl error messages Acme-friendly
 plan9/arpa/inet.h              Plan9 port: replacement C header file
 plan9/buildinfo                        Plan9 port: configuration information
@@ -2032,6 +2034,7 @@ t/op/pwent.t                      See if getpw*() functions work
 t/op/quotemeta.t               See if quotemeta works
 t/op/rand.t                    See if rand works
 t/op/range.t                   See if .. works
+t/op/re_tests                  Regular expressions for regexp.t
 t/op/read.t                    See if read() works
 t/op/readdir.t                 See if readdir() works
 t/op/recurse.t                 See if deep recursion works
@@ -2041,7 +2044,6 @@ t/op/regexp_noamp.t               See if regular expressions work with optimizations
 t/op/regmesg.t                 See if one can get regular expression errors
 t/op/repeat.t                  See if x operator works
 t/op/reverse.t                 See if reverse operator works
-t/op/re_tests                  Regular expressions for regexp.t
 t/op/runlevel.t                        See if die() works from perl_call_*()
 t/op/sleep.t                   See if sleep works
 t/op/sort.t                    See if sort works
@@ -2050,11 +2052,11 @@ t/op/split.t                    See if split works
 t/op/sprintf.t                 See if sprintf works
 t/op/stat.t                    See if stat works
 t/op/study.t                   See if study works
+t/op/sub_lval.t                        See if lvalue subroutines work
 t/op/subst.t                   See if substitution works
-t/op/substr.t                  See if substr works
 t/op/subst_amp.t               See if $&-related substitution works
 t/op/subst_wamp.t              See if substitution works with $& present
-t/op/sub_lval.t                        See if lvalue subroutines work
+t/op/substr.t                  See if substr works
 t/op/sysio.t                   See if sysread and syswrite work
 t/op/taint.t                   See if tainting works
 t/op/tie.t                     See if tie/untie functions work
@@ -2155,9 +2157,9 @@ vms/ext/Stdio/test.pl             regression tests for VMS::Stdio
 vms/ext/vmsish.pm              Control VMS-specific behavior of Perl core
 vms/ext/vmsish.t               Tests for vmsish.pm
 vms/ext/XSSymSet.pm            manage linker symbols when building extensions
+vms/gen_shrfls.pl              generate options files and glue for shareable image
 vms/genconfig.pl               retcon config.sh from config.h
 vms/genopt.com                 hack to write options files in case of broken makes
-vms/gen_shrfls.pl              generate options files and glue for shareable image
 vms/make_command.com           record MM[SK] command used to build Perl
 vms/mms2make.pl                        convert descrip.mms to make syntax
 vms/munchconfig.c              performs shell $var substitution for VMS
@@ -2169,9 +2171,9 @@ vms/sockadapt.c                   glue for SockshShr socket support
 vms/sockadapt.h                        glue for SockshShr socket support
 vms/test.com                   DCL driver for regression tests
 vms/vms.c                      VMS-specific C code for Perl core
+vms/vms_yfix.pl                        convert Unix perly.[ch] to VMS perly_[ch].vms
 vms/vmsish.h                   VMS-specific C header for Perl core
 vms/vmspipe.com                        VMS-specific piped command helper script
-vms/vms_yfix.pl                        convert Unix perly.[ch] to VMS perly_[ch].vms
 vms/writemain.pl               Generate perlmain.c from miniperlmain.c+extensions
 vos/build.cm                   VOS command macro to build Perl
 vos/Changes                    Changes made to port Perl to the VOS operating system
@@ -2186,8 +2188,8 @@ vos/install_perl.cm               VOS command macro to install perl after building
 vos/Makefile                   A helper for maintaining the config.*.* in UNIX
 vos/perl.bind                  VOS bind control file
 vos/test_vos_dummies.c         Test program for "vos_dummies.c"
-vos/vosish.h                   VOS-specific header file
 vos/vos_dummies.c              Wrappers to soak up undefined functions
+vos/vosish.h                   VOS-specific header file
 warnings.h                     The warning numbers
 warnings.pl                    Program to write warnings.h and lib/warnings.pm
 win32/bin/exetype.pl           Set executable type to CONSOLE or WINDOWS
index 09ca297..52683d4 100644 (file)
@@ -1,10 +1,10 @@
 # Term::ANSIColor -- Color screen output using ANSI escape sequences.
-# $Id: ANSIColor.pm,v 1.3 2000/08/06 18:28:10 eagle Exp $
+# $Id: ANSIColor.pm,v 1.4 2001/07/10 08:52:05 eagle Exp $
 #
-# Copyright 1996, 1997, 1998, 2000
-#   by Russ Allbery <rra@stanford.edu> and Zenin <zenin@best.com>
+# Copyright 1996, 1997, 1998, 2000, 2001
+#   by Russ Allbery <rra@stanford.edu> and Zenin <zenin@bawdycaste.com>
 #
-# This program is free software; you can redistribute it and/or modify it
+# This program is free software; you may redistribute it and/or modify it
 # under the same terms as Perl itself.
 #
 # Ah, September, when the sysadmins turn colors and fall off the trees....
@@ -18,12 +18,13 @@ package Term::ANSIColor;
 require 5.001;
 
 use strict;
-use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION $AUTOLOAD %attributes
-            $AUTORESET $EACHLINE);
+use vars qw($AUTOLOAD $AUTORESET $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 UNDERLINE UNDERSCORE BLINK
                                  REVERSE CONCEALED BLACK RED GREEN YELLOW
                                  BLUE MAGENTA CYAN WHITE ON_BLACK ON_RED
@@ -33,7 +34,7 @@ Exporter::export_ok_tags ('constants');
 
 # Don't use the CVS revision as the version, since this module is also in
 # Perl core and too many things could munge CVS magic revision strings.
-$VERSION = 1.03;
+$VERSION = 1.04;
 
 
 ############################################################################
@@ -50,15 +51,20 @@ $VERSION = 1.03;
                'reverse'    => 7,
                'concealed'  => 8,
 
-               'black'      => 30,   'on_black'   => 40, 
-               'red'        => 31,   'on_red'     => 41, 
-               'green'      => 32,   'on_green'   => 42, 
-               'yellow'     => 33,   'on_yellow'  => 43, 
-               'blue'       => 34,   'on_blue'    => 44, 
-               'magenta'    => 35,   'on_magenta' => 45, 
-               'cyan'       => 36,   'on_cyan'    => 46, 
+               'black'      => 30,   'on_black'   => 40,
+               'red'        => 31,   'on_red'     => 41,
+               'green'      => 32,   'on_green'   => 42,
+               'yellow'     => 33,   'on_yellow'  => 43,
+               'blue'       => 34,   'on_blue'    => 44,
+               'magenta'    => 35,   'on_magenta' => 45,
+               'cyan'       => 36,   'on_cyan'    => 46,
                'white'      => 37,   'on_white'   => 47);
 
+# Reverse lookup.  Alphabetically first name for a sequence is preferred.
+for (reverse sort keys %attributes) {
+    $attributes_r{$attributes{$_}} = $_;
+}
+
 
 ############################################################################
 # Implementation (constant form)
@@ -81,12 +87,18 @@ $VERSION = 1.03;
 # 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.
 sub AUTOLOAD {
+    my $enable_colors = !defined $ENV{ANSI_COLORS_DISABLED};
     my $sub;
     ($sub = $AUTOLOAD) =~ s/^.*:://;
     my $attr = $attributes{lc $sub};
     if ($sub =~ /^[A-Z_]+$/ && defined $attr) {
-        $attr = "\e[" . $attr . 'm';
+        $attr = $enable_colors ? "\e[" . $attr . 'm' : '';
         eval qq {
             sub $AUTOLOAD {
                 if (\$AUTORESET && \@_) {
@@ -110,6 +122,7 @@ sub AUTOLOAD {
 
 # Return the escape code for a given set of color attributes.
 sub color {
+    return '' if defined $ENV{ANSI_COLORS_DISABLED};
     my @codes = map { split } @_;
     my $attribute = '';
     foreach (@codes) {
@@ -124,6 +137,33 @@ sub color {
     ($attribute ne '') ? "\e[${attribute}m" : undef;
 }
 
+# Return a list of named color attributes for a given set of escape codes.
+# Escape sequences can be given with or without enclosing "\e[" and "m".
+# The empty escape sequence '' or "\e[m" gives an empty list of attrs.
+sub uncolor {
+    my (@nums, @result);
+    for (@_) {
+        my $escape = $_;
+        $escape =~ s/^\e\[//;
+        $escape =~ s/m$//;
+        unless ($escape =~ /^((?:\d+;)*\d*)$/) {
+            require Carp;
+            Carp::croak ("Bad escape sequence $_");
+        }
+        push (@nums, split (/;/, $1));
+    }
+    for (@nums) {
+       $_ += 0; # Strip leading zeroes
+       my $name = $attributes_r{$_};
+       if (!defined $name) {
+           require Carp;
+           Carp::croak ("No name for escape sequence $_" );
+       }
+       push (@result, $name);
+    }
+    @result;
+}
+
 # Given a string and a set of attributes, returns the string surrounded by
 # escape codes to set those attributes and then clear them at the end of the
 # string.  The attributes can be given either as an array ref as the first
@@ -141,9 +181,10 @@ sub colored {
         $string = shift;
         @codes = @_;
     }
+    return $string if defined $ENV{ANSI_COLORS_DISABLED};
     if (defined $EACHLINE) {
         my $attr = color (@codes);
-        join '', 
+        join '',
             map { $_ && $_ ne $EACHLINE ? $attr . $_ . "\e[0m" : $_ }
                 split (/(\Q$EACHLINE\E)/, $string);
     } else {
@@ -175,6 +216,9 @@ Term::ANSIColor - Color screen output using ANSI escape sequences
     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;
 
@@ -186,22 +230,26 @@ Term::ANSIColor - Color screen output using ANSI escape sequences
 =head1 DESCRIPTION
 
 This module has two interfaces, one through color() and colored() and the
-other through constants.
+other through constants.  It also offers the utility function uncolor(),
+which has to be explicitly imported to be used (see L<SYNOPSYS>).
 
 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, 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.
+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
@@ -212,28 +260,28 @@ Attributes, once set, last until they are unset (by sending the attribute
 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.
+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 C<"\n"> to use
-this feature.
+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 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, 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, 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\n", RESET;
 
@@ -260,15 +308,32 @@ that only two subroutines are exported into your namespace, versus
 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, polute 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, polute 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.
 
 =head1 DIAGNOSTICS
 
 =over 4
 
+=item Bad escape sequence %s
+
+(F) You passed an invalid ANSI escape sequence to uncolor().
+
+=item Bareword "%s" not allowed while "strict subs" in use
+
+(F) You probably mistyped a constant color name such as:
+
+    $Foobar = FOOBAR . "This line should be blue\n";
+
+or:
+
+    @Foobar = FOOBAR, "This line should be blue\n";
+
+This will only show up under use strict (another good reason to run under
+use strict).
+
 =item Invalid attribute name %s
 
 (F) You passed an invalid attribute name to either color() or colored().
@@ -292,18 +357,28 @@ Generating this fatal compile error is one of the main advantages of using
 the constants interface, since you'll immediately know if you mistype a
 color name.
 
-=item Bareword "%s" not allowed while "strict subs" in use
+=item No name for escape sequence %s
 
-(F) You probably mistyped a constant color name such as:
+(F) The ANSI escape sequence passed to uncolor() contains escapes which
+aren't recognized and can't be translated to names.
 
-    $Foobar = FOOBAR . "This line should be blue\n";
+=back
 
-or:
+=head1 ENVIRONMENT
 
-    @Foobar = FOOBAR, "This line should be blue\n";
+=over 4
 
-This will only show up under use strict (another good reason to run under
-use strict).
+=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
+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.
+
+For it to have its proper effect, this environment variable must be set
+before any color constants are used in the program.
 
 =back
 
@@ -316,15 +391,27 @@ entirely and just say:
 
 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.)
 
 For easier debuging, 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 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.
+
+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 (reportedly) the
+"console" in at least some versions of Windows.  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:
 
@@ -338,14 +425,39 @@ emulators and their support for the various attributes:
  aixterm      kinda   normal     no      yes      no       yes      yes
 
 Where the entry is other than yes or no, that emulator interpret 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.
+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.
+
+=head1 SEE ALSO
+
+ECMA-048 is available on-line (at least at the time of this writing) at
+E<lt>http://www.ecma.ch/ecma1/STAND/ECMA-048.HTME<gt>.
+
+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.
 
 =head1 AUTHORS
 
-Original idea (using constants) by Zenin (zenin@best.com), reimplemented
-using subs by Russ Allbery (rra@stanford.edu), and then combined with the
-original idea by Russ with input from Zenin.
+Original idea (using constants) by Zenin, reimplemented using subs by Russ
+Allbery E<lt>rra@stanford.eduE<gt>, and then combined with the original idea
+by Russ with input from Zenin.  Russ Allbery now maintains this module.
+
+=head1 LICENSE
+
+Copyright 1996, 1997, 1998, 2000, 2001 Russ Allbery <rra@stanford.edu> and
+Zenin <zenin@bawdycaste.org>.  This program is free software; you may
+redistribute it and/or modify it under the same terms as Perl itself.
 
 =cut
diff --git a/lib/Term/ANSIColor/ChangeLog b/lib/Term/ANSIColor/ChangeLog
new file mode 100644 (file)
index 0000000..9df692d
--- /dev/null
@@ -0,0 +1,127 @@
+2001-07-10  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 1.04 released.
+
+       * ANSIColor.pm: Add documentation, examples, and diagnostics for
+       uncolor.  Document ANSI_COLORS_DISABLED.  Add information about
+       the relevant standards for these escape sequences and the
+       additional ones that aren't supported by this module.  Add a
+       pointer to the relevant standards.  Add a LICENSE section.  Update
+       Zenin's e-mail address.
+
+       * ANSIColor.pm (AUTOLOAD): Add support for ANSI_COLORS_DISABLED.
+       (color): Likewise.
+       (colored): Likewise.
+       * test.pl: Add tests for ANSI_COLORS_DISABLED.
+
+       * ANSIColor.pm (uncolor): New function.
+       * test.pl: Add a test for it.
+
+2000-08-06  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 1.03 released.
+
+       * Makefile.PL: Install in the Perl library directory for Perl
+       versions >= 5.6.0.
+
+       * test.pl: Added a new test for the array reference syntax for
+       colored.
+
+       * ANSIColor.pm: Changed $VERSION to a static string.  Added dark
+       to the attributes.  Updated the documentation to include a table
+       of supported attributes on different terminal emulators, to add
+       dark, to document the new optional way to call colored, and to
+       mark the diagnostics as fatal errors or warnings.
+       (colored): Allow the attributes to be passed as an initial array
+       reference as well as a final list, and for that calling syntax
+       take the rest of the arguments as text to be colored.
+
+1998-11-27  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 1.02 released.
+
+       * Makefile.PL: Added a 5.005-only section giving ABSTRACT and
+       AUTHOR settings for generating a PPD to go with a binary
+       distribution or the Perl Resource Kits.
+
+1998-04-14  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: croak() instead of die() on AUTOLOAD failure to
+       get the right error text, fixed a bunch of typos in the
+       documentation, added a quote.
+
+1997-12-10  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 1.01 released.
+
+       * ANSIColor.pm (color): Carp::croak() isn't predeclared, so it
+       needs parens around its argument.  This bug will only show up in
+       versions of Perl >5.004_04 since up until then strict.pm imports
+       Carp which predeclares the function.
+
+1997-11-29  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 1.00 released.
+
+       * Makefile.PL: Now gets version information from the module, has
+       the correct rules to build a distribution.
+
+       * test.pl: Comments trimmed, minor test modifications.
+
+       * ANSIColor.pm: Changed my e-mail address, fixed to deal correctly
+       with trailing delimiters when EACHLINE is being used, die()
+       changed to croak() if the caller uses an invalid attribute name,
+       getting $VERSION from RCS updated to my current method, source
+       detabified.
+
+       * test.pl: Added test for EACHLINE with trailing delimiters.
+
+1997-02-17  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 0.9 released.
+
+       * ANSIColor.pm: Changed the runtime error message to start with an
+       uppercase letter, reworked the documentation considerably
+       including adding more comparison between the two interfaces,
+       fixing some formatting bugs, fixing a typo, adding more
+       diagnostics, and generally being more verbose.
+
+1997-01-08  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: Version 0.8 released.
+
+       * test.pl: Fixed the test numbering in the BEGIN block.
+
+       * test.pl: Reformatted and commented to fit my programming style.
+
+       * ANSIColor.pm: Changed the method by which $VERSION is set so
+       that it will always have two digits after the decimal point.
+
+       * test.pl: New file.
+
+       * ANSIColor.pm: [Revision 0.7] Changed the codes so that reset is
+       always consistantly "\e[0m".
+
+       * ANSIColor.pm: [Revision 0.6] Added $EACHLINE and support to
+       colored() for it so that attributes can be reset around every
+       newline (or other line delimiter -- we're flexible).  Documented
+       this as well.
+
+       * ANSIColor.pm: [Revision 0.5] Changed implementation of the
+       constants to autoloaded subs, added the $AUTORESET variable for
+       use with the constants, and documented this.
+
+1996-12-07  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: [Revision 0.4] Added POD documentation.
+
+       * ANSIColor.pm: [Revision 0.3] Added constant forms, modified to
+       allow a space-separated string of attributes to be passed to
+       color() and colored(), added Zenin to the credits.
+
+1996-12-04  Russ Allbery  <rra@stanford.edu>
+
+       * ANSIColor.pm: [Revision 0.2] Changed return syntax and check for
+       the null attribute string.
+
+       * ANSIColor.pm: New file.
diff --git a/lib/Term/ANSIColor/README b/lib/Term/ANSIColor/README
new file mode 100644 (file)
index 0000000..9c44a61
--- /dev/null
@@ -0,0 +1,77 @@
+                        Term::ANSIColor version 1.04
+               (A simple ANSI text attribute control module)
+
+  Copyright 1996, 1997, 1998, 2000, 2001 Russ Allbery <rra@stanford.edu>
+  and Zenin <zenin@bawdycaste.org>.  This program is free software; you
+  may redistribute it and/or modify it under the same terms as Perl
+  itself.
+
+
+INTRODUCTION
+
+  This module grew out of a thread on comp.lang.perl.misc where several of
+  us were throwing around different ways to print colored text from Perl
+  scripts and Zenin posted his old library to do that.  I (Russ) disagreed
+  with the implementation and offered my own (the color() and colored()
+  functions implemented in this package), Zenin convinced me that the
+  constants had their place as well, and we started figuring out the best
+  ways of implementing both.
+
+  While ANSI color escape codes are fairly simple, it can be hard to
+  remember the codes for all of the attributes and the code resulting from
+  hard-coding them into your script is definitely difficult to read.  This
+  module is designed to fix those problems, as well as provide a convenient
+  interface to do a few things for you automatically (like resetting
+  attributes after the text you print out so that you don't accidentally
+  leave attributes set).
+
+  Despite its name, this module can also handle non-color ANSI text
+  attributes (bold, underline, reverse video, and blink).  It uses either of
+  two interfaces, one of which uses "constants" for each different attribute
+  and the other of which uses two subs which take strings of attributes as
+  arguments.
+
+  See the POD documentation for complete details, features, and usage.
+
+
+INSTALLATION
+
+  Follow the standard installation procedure for Perl modules, which is to
+  type the following commands:
+
+          perl Makefile.PL
+          make
+          make test
+          make install
+
+  You'll probably need to do the last as root.  If instead you wish to
+  install the module by hand, simply copy it into a directory named Term in
+  your Perl library directory.
+
+  Note that make install, for Perl 5.6.0 or later, will replace the
+  Term::ANSIColor that came with Perl.  You may wan to save a backup copy
+  of the standard version first.
+
+
+THANKS
+
+  To Jon Lennox for looking at early versions of this module, providing
+  feedback, and offering suggestions for improvement.
+
+  To Jesse Taylor for writing the first significant script to use this
+  module (colorized calsplit), thus offering innumerable opportunities to
+  test and debug.
+
+  To Jean Delvare for providing documentation of what the various
+  attributes do on various different terminal emulators, and for noting
+  that attribute 2 is dark.
+
+  To Edward Avis for the implementation of uncolor.
+
+  To Rani Pinchuk for the idea of ANSI_COLORS_DISABLED and an initial
+  implementation.
+
+  To Larry Wall, as always, for Perl.
+
+                                                Russ Allbery
+                                                rra@stanford.edu
index f38e905..d97bde1 100755 (executable)
@@ -1,8 +1,3 @@
-BEGIN {
-    chdir 't' if -d 't';
-    @INC = '../lib';
-}
-
 # Test suite for the Term::ANSIColor Perl module.  Before `make install' is
 # performed this script should be runnable with `make test'.  After `make
 # install' it should work as `perl test.pl'.
@@ -11,9 +6,10 @@ BEGIN {
 # Ensure module can be loaded
 ############################################################################
 
-BEGIN { $| = 1; print "1..8\n" }
+BEGIN { $| = 1; print "1..12\n" }
 END   { print "not ok 1\n" unless $loaded }
-use Term::ANSIColor qw(:constants color colored);
+delete $ENV{ANSI_COLORS_DISABLED};
+use Term::ANSIColor qw(:constants color colored uncolor);
 $loaded = 1;
 print "ok 1\n";
 
@@ -79,3 +75,30 @@ if (colored (['bold', 'on_green'], "test\n", "\n", "test")
     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";
+}
+
+# Test ANSI_COLORS_DISABLED.
+$ENV{ANSI_COLORS_DISABLED} = 1;
+if (color ('blue') == '') {
+    print "ok 10\n";
+} else {
+    print "not ok 10\n";
+}
+if (colored ('testing', 'blue', 'on_red') == '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";
+}