Blame lib/Term/ANSIColor.pm

Packit e6c7a3
# Color screen output using ANSI escape sequences.
Packit e6c7a3
#
Packit e6c7a3
# Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009, 2010,
Packit e6c7a3
#     2011, 2012, 2013, 2014, 2015, 2016 Russ Allbery <rra@cpan.org>
Packit e6c7a3
# Copyright 1996 Zenin
Packit e6c7a3
# Copyright 2012 Kurt Starsinic <kstarsinic@gmail.com>
Packit e6c7a3
#
Packit e6c7a3
# This program is free software; you may redistribute it and/or modify it
Packit e6c7a3
# under the same terms as Perl itself.
Packit e6c7a3
#
Packit e6c7a3
# PUSH/POP support submitted 2007 by openmethods.com voice solutions
Packit e6c7a3
#
Packit e6c7a3
# Ah, September, when the sysadmins turn colors and fall off the trees....
Packit e6c7a3
#                               -- Dave Van Domelen
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Modules and declarations
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
package Term::ANSIColor;
Packit e6c7a3
Packit e6c7a3
use 5.006;
Packit e6c7a3
use strict;
Packit e6c7a3
use warnings;
Packit e6c7a3
Packit e6c7a3
# Also uses Carp but loads it on demand to reduce memory usage.
Packit e6c7a3
Packit e6c7a3
use Exporter ();
Packit e6c7a3
Packit e6c7a3
# use Exporter plus @ISA instead of use base for 5.6 compatibility.
Packit e6c7a3
## no critic (ClassHierarchies::ProhibitExplicitISA)
Packit e6c7a3
Packit e6c7a3
# Declare variables that should be set in BEGIN for robustness.
Packit e6c7a3
## no critic (Modules::ProhibitAutomaticExportation)
Packit e6c7a3
our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS, @ISA, $VERSION);
Packit e6c7a3
Packit e6c7a3
# We use autoloading, which sets this variable to the name of the called sub.
Packit e6c7a3
our $AUTOLOAD;
Packit e6c7a3
Packit e6c7a3
# Set $VERSION and everything export-related in a BEGIN block for robustness
Packit e6c7a3
# against circular module loading (not that we load any modules, but
Packit e6c7a3
# consistency is good).
Packit e6c7a3
BEGIN {
Packit e6c7a3
    $VERSION = '4.06';
Packit e6c7a3
Packit e6c7a3
    # All of the basic supported constants, used in %EXPORT_TAGS.
Packit e6c7a3
    my @colorlist = qw(
Packit e6c7a3
      CLEAR           RESET             BOLD            DARK
Packit e6c7a3
      FAINT           ITALIC            UNDERLINE       UNDERSCORE
Packit e6c7a3
      BLINK           REVERSE           CONCEALED
Packit e6c7a3
Packit e6c7a3
      BLACK           RED               GREEN           YELLOW
Packit e6c7a3
      BLUE            MAGENTA           CYAN            WHITE
Packit e6c7a3
      ON_BLACK        ON_RED            ON_GREEN        ON_YELLOW
Packit e6c7a3
      ON_BLUE         ON_MAGENTA        ON_CYAN         ON_WHITE
Packit e6c7a3
Packit e6c7a3
      BRIGHT_BLACK    BRIGHT_RED        BRIGHT_GREEN    BRIGHT_YELLOW
Packit e6c7a3
      BRIGHT_BLUE     BRIGHT_MAGENTA    BRIGHT_CYAN     BRIGHT_WHITE
Packit e6c7a3
      ON_BRIGHT_BLACK ON_BRIGHT_RED     ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
Packit e6c7a3
      ON_BRIGHT_BLUE  ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN  ON_BRIGHT_WHITE
Packit e6c7a3
    );
Packit e6c7a3
Packit e6c7a3
    # 256-color constants, used in %EXPORT_TAGS.
Packit e6c7a3
    my @colorlist256 = (
Packit e6c7a3
        (map { ("ANSI$_", "ON_ANSI$_") } 0 .. 255),
Packit e6c7a3
        (map { ("GREY$_", "ON_GREY$_") } 0 .. 23),
Packit e6c7a3
    );
Packit e6c7a3
    for my $r (0 .. 5) {
Packit e6c7a3
        for my $g (0 .. 5) {
Packit e6c7a3
            push(@colorlist256, map { ("RGB$r$g$_", "ON_RGB$r$g$_") } 0 .. 5);
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Exported symbol configuration.
Packit e6c7a3
    @ISA         = qw(Exporter);
Packit e6c7a3
    @EXPORT      = qw(color colored);
Packit e6c7a3
    @EXPORT_OK   = qw(uncolor colorstrip colorvalid coloralias);
Packit e6c7a3
    %EXPORT_TAGS = (
Packit e6c7a3
        constants    => \@colorlist,
Packit e6c7a3
        constants256 => \@colorlist256,
Packit e6c7a3
        pushpop      => [@colorlist, qw(PUSHCOLOR POPCOLOR LOCALCOLOR)],
Packit e6c7a3
    );
Packit e6c7a3
    Exporter::export_ok_tags('pushpop', 'constants256');
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Package variables
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# If this is set, any color changes will implicitly push the current color
Packit e6c7a3
# onto the stack and then pop it at the end of the constant sequence, just as
Packit e6c7a3
# if LOCALCOLOR were used.
Packit e6c7a3
our $AUTOLOCAL;
Packit e6c7a3
Packit e6c7a3
# Caller sets this to force a reset at the end of each constant sequence.
Packit e6c7a3
our $AUTORESET;
Packit e6c7a3
Packit e6c7a3
# Caller sets this to force colors to be reset at the end of each line.
Packit e6c7a3
our $EACHLINE;
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Internal data structures
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# This module does quite a bit of initialization at the time it is first
Packit e6c7a3
# loaded, primarily to set up the package-global %ATTRIBUTES hash.  The
Packit e6c7a3
# entries for 256-color names are easier to handle programmatically, and
Packit e6c7a3
# custom colors are also imported from the environment if any are set.
Packit e6c7a3
Packit e6c7a3
# All basic supported attributes, including aliases.
Packit e6c7a3
#<<<
Packit e6c7a3
our %ATTRIBUTES = (
Packit e6c7a3
    'clear'          => 0,
Packit e6c7a3
    'reset'          => 0,
Packit e6c7a3
    'bold'           => 1,
Packit e6c7a3
    'dark'           => 2,
Packit e6c7a3
    'faint'          => 2,
Packit e6c7a3
    'italic'         => 3,
Packit e6c7a3
    'underline'      => 4,
Packit e6c7a3
    'underscore'     => 4,
Packit e6c7a3
    'blink'          => 5,
Packit e6c7a3
    'reverse'        => 7,
Packit e6c7a3
    'concealed'      => 8,
Packit e6c7a3
Packit e6c7a3
    'black'          => 30,   'on_black'          => 40,
Packit e6c7a3
    'red'            => 31,   'on_red'            => 41,
Packit e6c7a3
    'green'          => 32,   'on_green'          => 42,
Packit e6c7a3
    'yellow'         => 33,   'on_yellow'         => 43,
Packit e6c7a3
    'blue'           => 34,   'on_blue'           => 44,
Packit e6c7a3
    'magenta'        => 35,   'on_magenta'        => 45,
Packit e6c7a3
    'cyan'           => 36,   'on_cyan'           => 46,
Packit e6c7a3
    'white'          => 37,   'on_white'          => 47,
Packit e6c7a3
Packit e6c7a3
    'bright_black'   => 90,   'on_bright_black'   => 100,
Packit e6c7a3
    'bright_red'     => 91,   'on_bright_red'     => 101,
Packit e6c7a3
    'bright_green'   => 92,   'on_bright_green'   => 102,
Packit e6c7a3
    'bright_yellow'  => 93,   'on_bright_yellow'  => 103,
Packit e6c7a3
    'bright_blue'    => 94,   'on_bright_blue'    => 104,
Packit e6c7a3
    'bright_magenta' => 95,   'on_bright_magenta' => 105,
Packit e6c7a3
    'bright_cyan'    => 96,   'on_bright_cyan'    => 106,
Packit e6c7a3
    'bright_white'   => 97,   'on_bright_white'   => 107,
Packit e6c7a3
);
Packit e6c7a3
#>>>
Packit e6c7a3
Packit e6c7a3
# Generating the 256-color codes involves a lot of codes and offsets that are
Packit e6c7a3
# not helped by turning them into constants.
Packit e6c7a3
Packit e6c7a3
# The first 16 256-color codes are duplicates of the 16 ANSI colors.  The rest
Packit e6c7a3
# are RBG and greyscale values.
Packit e6c7a3
for my $code (0 .. 15) {
Packit e6c7a3
    $ATTRIBUTES{"ansi$code"}    = "38;5;$code";
Packit e6c7a3
    $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# 256-color RGB colors.  Red, green, and blue can each be values 0 through 5,
Packit e6c7a3
# and the resulting 216 colors start with color 16.
Packit e6c7a3
for my $r (0 .. 5) {
Packit e6c7a3
    for my $g (0 .. 5) {
Packit e6c7a3
        for my $b (0 .. 5) {
Packit e6c7a3
            my $code = 16 + (6 * 6 * $r) + (6 * $g) + $b;
Packit e6c7a3
            $ATTRIBUTES{"rgb$r$g$b"}    = "38;5;$code";
Packit e6c7a3
            $ATTRIBUTES{"on_rgb$r$g$b"} = "48;5;$code";
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# The last 256-color codes are 24 shades of grey.
Packit e6c7a3
for my $n (0 .. 23) {
Packit e6c7a3
    my $code = $n + 232;
Packit e6c7a3
    $ATTRIBUTES{"grey$n"}    = "38;5;$code";
Packit e6c7a3
    $ATTRIBUTES{"on_grey$n"} = "48;5;$code";
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Reverse lookup.  Alphabetically first name for a sequence is preferred.
Packit e6c7a3
our %ATTRIBUTES_R;
Packit e6c7a3
for my $attr (reverse sort keys %ATTRIBUTES) {
Packit e6c7a3
    $ATTRIBUTES_R{ $ATTRIBUTES{$attr} } = $attr;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Provide ansiN names for all 256 characters to provide a convenient flat
Packit e6c7a3
# namespace if one doesn't want to mess with the RGB and greyscale naming.  Do
Packit e6c7a3
# this after creating %ATTRIBUTES_R since we want to use the canonical names
Packit e6c7a3
# when reversing a color.
Packit e6c7a3
for my $code (16 .. 255) {
Packit e6c7a3
    $ATTRIBUTES{"ansi$code"}    = "38;5;$code";
Packit e6c7a3
    $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Import any custom colors set in the environment.
Packit e6c7a3
our %ALIASES;
Packit e6c7a3
if (exists $ENV{ANSI_COLORS_ALIASES}) {
Packit e6c7a3
    my $spec = $ENV{ANSI_COLORS_ALIASES};
Packit e6c7a3
    $spec =~ s{\s+}{}xmsg;
Packit e6c7a3
Packit e6c7a3
    # Error reporting here is an interesting question.  Use warn rather than
Packit e6c7a3
    # carp because carp would report the line of the use or require, which
Packit e6c7a3
    # doesn't help anyone understand what's going on, whereas seeing this code
Packit e6c7a3
    # will be more helpful.
Packit e6c7a3
    ## no critic (ErrorHandling::RequireCarping)
Packit e6c7a3
    for my $definition (split m{,}xms, $spec) {
Packit e6c7a3
        my ($new, $old) = split m{=}xms, $definition, 2;
Packit e6c7a3
        if (!$new || !$old) {
Packit e6c7a3
            warn qq{Bad color mapping "$definition"};
Packit e6c7a3
        } else {
Packit e6c7a3
            my $result = eval { coloralias($new, $old) };
Packit e6c7a3
            if (!$result) {
Packit e6c7a3
                my $error = $@;
Packit e6c7a3
                $error =~ s{ [ ] at [ ] .* }{}xms;
Packit e6c7a3
                warn qq{$error in "$definition"};
Packit e6c7a3
            }
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Stores the current color stack maintained by PUSHCOLOR and POPCOLOR.  This
Packit e6c7a3
# is global and therefore not threadsafe.
Packit e6c7a3
our @COLORSTACK;
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Helper functions
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# Stub to load the Carp module on demand.
Packit e6c7a3
sub croak {
Packit e6c7a3
    my (@args) = @_;
Packit e6c7a3
    require Carp;
Packit e6c7a3
    Carp::croak(@args);
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Implementation (constant form)
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# Time to have fun!  We now want to define the constant subs, which are named
Packit e6c7a3
# the same as the attributes above but in all caps.  Each constant sub needs
Packit e6c7a3
# to act differently depending on whether $AUTORESET is set.  Without
Packit e6c7a3
# autoreset:
Packit e6c7a3
#
Packit e6c7a3
#     BLUE "text\n"  ==>  "\e[34mtext\n"
Packit e6c7a3
#
Packit e6c7a3
# If $AUTORESET is set, we should instead get:
Packit e6c7a3
#
Packit e6c7a3
#     BLUE "text\n"  ==>  "\e[34mtext\n\e[0m"
Packit e6c7a3
#
Packit e6c7a3
# The sub also needs to handle the case where it has no arguments correctly.
Packit e6c7a3
# Maintaining all of this as separate subs would be a major nightmare, as well
Packit e6c7a3
# as duplicate the %ATTRIBUTES hash, so instead we define an AUTOLOAD sub to
Packit e6c7a3
# define the constant subs on demand.  To do that, we check the name of the
Packit e6c7a3
# called sub against the list of attributes, and if it's an all-caps version
Packit e6c7a3
# of one of them, we define the sub on the fly and then run it.
Packit e6c7a3
#
Packit e6c7a3
# If the environment variable ANSI_COLORS_DISABLED is set to a true value,
Packit e6c7a3
# just return the arguments without adding any escape sequences.  This is to
Packit e6c7a3
# make it easier to write scripts that also work on systems without any ANSI
Packit e6c7a3
# support, like Windows consoles.
Packit e6c7a3
#
Packit e6c7a3
# Avoid using character classes like [:upper:] and \w here, since they load
Packit e6c7a3
# Unicode character tables and consume a ton of memory.  All of our constants
Packit e6c7a3
# only use ASCII characters.
Packit e6c7a3
#
Packit e6c7a3
## no critic (ClassHierarchies::ProhibitAutoloading)
Packit e6c7a3
## no critic (Subroutines::RequireArgUnpacking)
Packit e6c7a3
## no critic (RegularExpressions::ProhibitEnumeratedClasses)
Packit e6c7a3
sub AUTOLOAD {
Packit e6c7a3
    my ($sub, $attr) = $AUTOLOAD =~ m{
Packit e6c7a3
        \A ( [a-zA-Z0-9:]* :: ([A-Z0-9_]+) ) \z
Packit e6c7a3
    }xms;
Packit e6c7a3
Packit e6c7a3
    # Check if we were called with something that doesn't look like an
Packit e6c7a3
    # attribute.
Packit e6c7a3
    if (!($attr && defined($ATTRIBUTES{ lc $attr }))) {
Packit e6c7a3
        croak("undefined subroutine &$AUTOLOAD called");
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # If colors are disabled, just return the input.  Do this without
Packit e6c7a3
    # installing a sub for (marginal, unbenchmarked) speed.
Packit e6c7a3
    if ($ENV{ANSI_COLORS_DISABLED}) {
Packit e6c7a3
        return join(q{}, @_);
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # We've untainted the name of the sub.
Packit e6c7a3
    $AUTOLOAD = $sub;
Packit e6c7a3
Packit e6c7a3
    # Figure out the ANSI string to set the desired attribute.
Packit e6c7a3
    my $escape = "\e[" . $ATTRIBUTES{ lc $attr } . 'm';
Packit e6c7a3
Packit e6c7a3
    # Save the current value of $@.  We can't just use local since we want to
Packit e6c7a3
    # restore it before dispatching to the newly-created sub.  (The caller may
Packit e6c7a3
    # be colorizing output that includes $@.)
Packit e6c7a3
    my $eval_err = $@;
Packit e6c7a3
Packit e6c7a3
    # Generate the constant sub, which should still recognize some of our
Packit e6c7a3
    # package variables.  Use string eval to avoid a dependency on
Packit e6c7a3
    # Sub::Install, even though it makes it somewhat less readable.
Packit e6c7a3
    ## no critic (BuiltinFunctions::ProhibitStringyEval)
Packit e6c7a3
    ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
Packit e6c7a3
    my $eval_result = eval qq{
Packit e6c7a3
        sub $AUTOLOAD {
Packit e6c7a3
            if (\$ENV{ANSI_COLORS_DISABLED}) {
Packit e6c7a3
                return join(q{}, \@_);
Packit e6c7a3
            } elsif (\$AUTOLOCAL && \@_) {
Packit e6c7a3
                return PUSHCOLOR('$escape') . join(q{}, \@_) . POPCOLOR;
Packit e6c7a3
            } elsif (\$AUTORESET && \@_) {
Packit e6c7a3
                return '$escape' . join(q{}, \@_) . "\e[0m";
Packit e6c7a3
            } else {
Packit e6c7a3
                return '$escape' . join(q{}, \@_);
Packit e6c7a3
            }
Packit e6c7a3
        }
Packit e6c7a3
        1;
Packit e6c7a3
    };
Packit e6c7a3
Packit e6c7a3
    # Failure is an internal error, not a problem with the caller.
Packit e6c7a3
    ## no critic (ErrorHandling::RequireCarping)
Packit e6c7a3
    if (!$eval_result) {
Packit e6c7a3
        die "failed to generate constant $attr: $@";
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Restore $@.
Packit e6c7a3
    ## no critic (Variables::RequireLocalizedPunctuationVars)
Packit e6c7a3
    $@ = $eval_err;
Packit e6c7a3
Packit e6c7a3
    # Dispatch to the newly-created sub.
Packit e6c7a3
    ## no critic (References::ProhibitDoubleSigils)
Packit e6c7a3
    goto &$AUTOLOAD;
Packit e6c7a3
}
Packit e6c7a3
## use critic
Packit e6c7a3
Packit e6c7a3
# Append a new color to the top of the color stack and return the top of
Packit e6c7a3
# the stack.
Packit e6c7a3
#
Packit e6c7a3
# $text - Any text we're applying colors to, with color escapes prepended
Packit e6c7a3
#
Packit e6c7a3
# Returns: The text passed in
Packit e6c7a3
sub PUSHCOLOR {
Packit e6c7a3
    my (@text) = @_;
Packit e6c7a3
    my $text = join(q{}, @text);
Packit e6c7a3
Packit e6c7a3
    # Extract any number of color-setting escape sequences from the start of
Packit e6c7a3
    # the string.
Packit e6c7a3
    my ($color) = $text =~ m{ \A ( (?:\e\[ [\d;]+ m)+ ) }xms;
Packit e6c7a3
Packit e6c7a3
    # If we already have a stack, append these escapes to the set from the top
Packit e6c7a3
    # of the stack.  This way, each position in the stack stores the complete
Packit e6c7a3
    # enabled colors for that stage, at the cost of some potential
Packit e6c7a3
    # inefficiency.
Packit e6c7a3
    if (@COLORSTACK) {
Packit e6c7a3
        $color = $COLORSTACK[-1] . $color;
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Push the color onto the stack.
Packit e6c7a3
    push(@COLORSTACK, $color);
Packit e6c7a3
    return $text;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Pop the color stack and return the new top of the stack (or reset, if
Packit e6c7a3
# the stack is empty).
Packit e6c7a3
#
Packit e6c7a3
# @text - Any text we're applying colors to
Packit e6c7a3
#
Packit e6c7a3
# Returns: The concatenation of @text prepended with the new stack color
Packit e6c7a3
sub POPCOLOR {
Packit e6c7a3
    my (@text) = @_;
Packit e6c7a3
    pop(@COLORSTACK);
Packit e6c7a3
    if (@COLORSTACK) {
Packit e6c7a3
        return $COLORSTACK[-1] . join(q{}, @text);
Packit e6c7a3
    } else {
Packit e6c7a3
        return RESET(@text);
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Surround arguments with a push and a pop.  The effect will be to reset the
Packit e6c7a3
# colors to whatever was on the color stack before this sequence of colors was
Packit e6c7a3
# applied.
Packit e6c7a3
#
Packit e6c7a3
# @text - Any text we're applying colors to
Packit e6c7a3
#
Packit e6c7a3
# Returns: The concatenation of the text and the proper color reset sequence.
Packit e6c7a3
sub LOCALCOLOR {
Packit e6c7a3
    my (@text) = @_;
Packit e6c7a3
    return PUSHCOLOR(join(q{}, @text)) . POPCOLOR();
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Implementation (attribute string form)
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# Return the escape code for a given set of color attributes.
Packit e6c7a3
#
Packit e6c7a3
# @codes - A list of possibly space-separated color attributes
Packit e6c7a3
#
Packit e6c7a3
# Returns: The escape sequence setting those color attributes
Packit e6c7a3
#          undef if no escape sequences were given
Packit e6c7a3
#  Throws: Text exception for any invalid attribute
Packit e6c7a3
sub color {
Packit e6c7a3
    my (@codes) = @_;
Packit e6c7a3
    @codes = map { split } @codes;
Packit e6c7a3
Packit e6c7a3
    # Return the empty string if colors are disabled.
Packit e6c7a3
    if ($ENV{ANSI_COLORS_DISABLED}) {
Packit e6c7a3
        return q{};
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Build the attribute string from semicolon-separated numbers.
Packit e6c7a3
    my $attribute = q{};
Packit e6c7a3
    for my $code (@codes) {
Packit e6c7a3
        $code = lc($code);
Packit e6c7a3
        if (defined($ATTRIBUTES{$code})) {
Packit e6c7a3
            $attribute .= $ATTRIBUTES{$code} . q{;};
Packit e6c7a3
        } elsif (defined($ALIASES{$code})) {
Packit e6c7a3
            $attribute .= $ALIASES{$code} . q{;};
Packit e6c7a3
        } else {
Packit e6c7a3
            croak("Invalid attribute name $code");
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # We added one too many semicolons for simplicity.  Remove the last one.
Packit e6c7a3
    chop($attribute);
Packit e6c7a3
Packit e6c7a3
    # Return undef if there were no attributes.
Packit e6c7a3
    return ($attribute ne q{}) ? "\e[${attribute}m" : undef;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Return a list of named color attributes for a given set of escape codes.
Packit e6c7a3
# Escape sequences can be given with or without enclosing "\e[" and "m".  The
Packit e6c7a3
# empty escape sequence '' or "\e[m" gives an empty list of attrs.
Packit e6c7a3
#
Packit e6c7a3
# There is one special case.  256-color codes start with 38 or 48, followed by
Packit e6c7a3
# a 5 and then the 256-color code.
Packit e6c7a3
#
Packit e6c7a3
# @escapes - A list of escape sequences or escape sequence numbers
Packit e6c7a3
#
Packit e6c7a3
# Returns: An array of attribute names corresponding to those sequences
Packit e6c7a3
#  Throws: Text exceptions on invalid escape sequences or unknown colors
Packit e6c7a3
sub uncolor {
Packit e6c7a3
    my (@escapes) = @_;
Packit e6c7a3
    my (@nums, @result);
Packit e6c7a3
Packit e6c7a3
    # Walk the list of escapes and build a list of attribute numbers.
Packit e6c7a3
    for my $escape (@escapes) {
Packit e6c7a3
        $escape =~ s{ \A \e\[ }{}xms;
Packit e6c7a3
        $escape =~ s{ m \z }   {}xms;
Packit e6c7a3
        my ($attrs) = $escape =~ m{ \A ((?:\d+;)* \d*) \z }xms;
Packit e6c7a3
        if (!defined($attrs)) {
Packit e6c7a3
            croak("Bad escape sequence $escape");
Packit e6c7a3
        }
Packit e6c7a3
Packit e6c7a3
        # Pull off 256-color codes (38;5;n or 48;5;n) as a unit.
Packit e6c7a3
        push(@nums, $attrs =~ m{ ( 0*[34]8;0*5;\d+ | \d+ ) (?: ; | \z ) }xmsg);
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Now, walk the list of numbers and convert them to attribute names.
Packit e6c7a3
    # Strip leading zeroes from any of the numbers.  (xterm, at least, allows
Packit e6c7a3
    # leading zeroes to be added to any number in an escape sequence.)
Packit e6c7a3
    for my $num (@nums) {
Packit e6c7a3
        $num =~ s{ ( \A | ; ) 0+ (\d) }{$1$2}xmsg;
Packit e6c7a3
        my $name = $ATTRIBUTES_R{$num};
Packit e6c7a3
        if (!defined($name)) {
Packit e6c7a3
            croak("No name for escape sequence $num");
Packit e6c7a3
        }
Packit e6c7a3
        push(@result, $name);
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Return the attribute names.
Packit e6c7a3
    return @result;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Given a string and a set of attributes, returns the string surrounded by
Packit e6c7a3
# escape codes to set those attributes and then clear them at the end of the
Packit e6c7a3
# string.  The attributes can be given either as an array ref as the first
Packit e6c7a3
# argument or as a list as the second and subsequent arguments.
Packit e6c7a3
#
Packit e6c7a3
# If $EACHLINE is set, insert a reset before each occurrence of the string
Packit e6c7a3
# $EACHLINE and the starting attribute code after the string $EACHLINE, so
Packit e6c7a3
# that no attribute crosses line delimiters (this is often desirable if the
Packit e6c7a3
# output is to be piped to a pager or some other program).
Packit e6c7a3
#
Packit e6c7a3
# $first - An anonymous array of attributes or the text to color
Packit e6c7a3
# @rest  - The text to color or the list of attributes
Packit e6c7a3
#
Packit e6c7a3
# Returns: The text, concatenated if necessary, surrounded by escapes to set
Packit e6c7a3
#          the desired colors and reset them afterwards
Packit e6c7a3
#  Throws: Text exception on invalid attributes
Packit e6c7a3
sub colored {
Packit e6c7a3
    my ($first, @rest) = @_;
Packit e6c7a3
    my ($string, @codes);
Packit e6c7a3
    if (ref($first) && ref($first) eq 'ARRAY') {
Packit e6c7a3
        @codes = @{$first};
Packit e6c7a3
        $string = join(q{}, @rest);
Packit e6c7a3
    } else {
Packit e6c7a3
        $string = $first;
Packit e6c7a3
        @codes  = @rest;
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Return the string unmolested if colors are disabled.
Packit e6c7a3
    if ($ENV{ANSI_COLORS_DISABLED}) {
Packit e6c7a3
        return $string;
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Find the attribute string for our colors.
Packit e6c7a3
    my $attr = color(@codes);
Packit e6c7a3
Packit e6c7a3
    # If $EACHLINE is defined, split the string on line boundaries, suppress
Packit e6c7a3
    # empty segments, and then colorize each of the line sections.
Packit e6c7a3
    if (defined($EACHLINE)) {
Packit e6c7a3
        my @text = map { ($_ ne $EACHLINE) ? $attr . $_ . "\e[0m" : $_ }
Packit e6c7a3
          grep { length > 0 }
Packit e6c7a3
          split(m{ (\Q$EACHLINE\E) }xms, $string);
Packit e6c7a3
        return join(q{}, @text);
Packit e6c7a3
    } else {
Packit e6c7a3
        return $attr . $string . "\e[0m";
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Define a new color alias, or return the value of an existing alias.
Packit e6c7a3
#
Packit e6c7a3
# $alias - The color alias to define
Packit e6c7a3
# $color - The standard color the alias will correspond to (optional)
Packit e6c7a3
#
Packit e6c7a3
# Returns: The standard color value of the alias
Packit e6c7a3
#          undef if one argument was given and the alias was not recognized
Packit e6c7a3
#  Throws: Text exceptions for invalid alias names, attempts to use a
Packit e6c7a3
#          standard color name as an alias, or an unknown standard color name
Packit e6c7a3
sub coloralias {
Packit e6c7a3
    my ($alias, $color) = @_;
Packit e6c7a3
    if (!defined($color)) {
Packit e6c7a3
        if (!exists $ALIASES{$alias}) {
Packit e6c7a3
            return;
Packit e6c7a3
        } else {
Packit e6c7a3
            return $ATTRIBUTES_R{ $ALIASES{$alias} };
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    # Avoid \w here to not load Unicode character tables, which increases the
Packit e6c7a3
    # memory footprint of this module considerably.
Packit e6c7a3
    #
Packit e6c7a3
    ## no critic (RegularExpressions::ProhibitEnumeratedClasses)
Packit e6c7a3
    if ($alias !~ m{ \A [a-zA-Z0-9._-]+ \z }xms) {
Packit e6c7a3
        croak(qq{Invalid alias name "$alias"});
Packit e6c7a3
    } elsif ($ATTRIBUTES{$alias}) {
Packit e6c7a3
        croak(qq{Cannot alias standard color "$alias"});
Packit e6c7a3
    } elsif (!exists $ATTRIBUTES{$color}) {
Packit e6c7a3
        croak(qq{Invalid attribute name "$color"});
Packit e6c7a3
    }
Packit e6c7a3
    ## use critic
Packit e6c7a3
Packit e6c7a3
    # Set the alias and return.
Packit e6c7a3
    $ALIASES{$alias} = $ATTRIBUTES{$color};
Packit e6c7a3
    return $color;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Given a string, strip the ANSI color codes out of that string and return the
Packit e6c7a3
# result.  This removes only ANSI color codes, not movement codes and other
Packit e6c7a3
# escape sequences.
Packit e6c7a3
#
Packit e6c7a3
# @string - The list of strings to sanitize
Packit e6c7a3
#
Packit e6c7a3
# Returns: (array)  The strings stripped of ANSI color escape sequences
Packit e6c7a3
#          (scalar) The same, concatenated
Packit e6c7a3
sub colorstrip {
Packit e6c7a3
    my (@string) = @_;
Packit e6c7a3
    for my $string (@string) {
Packit e6c7a3
        $string =~ s{ \e\[ [\d;]* m }{}xmsg;
Packit e6c7a3
    }
Packit e6c7a3
    return wantarray ? @string : join(q{}, @string);
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Given a list of color attributes (arguments for color, for instance), return
Packit e6c7a3
# true if they're all valid or false if any of them are invalid.
Packit e6c7a3
#
Packit e6c7a3
# @codes - A list of color attributes, possibly space-separated
Packit e6c7a3
#
Packit e6c7a3
# Returns: True if all the attributes are valid, false otherwise.
Packit e6c7a3
sub colorvalid {
Packit e6c7a3
    my (@codes) = @_;
Packit e6c7a3
    @codes = map { split(q{ }, lc) } @codes;
Packit e6c7a3
    for my $code (@codes) {
Packit e6c7a3
        if (!(defined($ATTRIBUTES{$code}) || defined($ALIASES{$code}))) {
Packit e6c7a3
            return;
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
    return 1;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
##############################################################################
Packit e6c7a3
# Module return value and documentation
Packit e6c7a3
##############################################################################
Packit e6c7a3
Packit e6c7a3
# Ensure we evaluate to true.
Packit e6c7a3
1;
Packit e6c7a3
__END__
Packit e6c7a3
Packit e6c7a3
=head1 NAME
Packit e6c7a3
Packit e6c7a3
Term::ANSIColor - Color screen output using ANSI escape sequences
Packit e6c7a3
Packit e6c7a3
=for stopwords
Packit e6c7a3
cyan colorize namespace runtime TMTOWTDI cmd.exe cmd.exe. 4nt.exe. 4nt.exe
Packit e6c7a3
command.com NT ESC Delvare SSH OpenSSH aixterm ECMA-048 Fraktur overlining
Packit e6c7a3
Zenin reimplemented Allbery PUSHCOLOR POPCOLOR LOCALCOLOR openmethods.com
Packit e6c7a3
openmethods.com. grey ATTR urxvt mistyped prepending Bareword filehandle
Packit e6c7a3
Cygwin Starsinic aterm rxvt CPAN RGB Solarized Whitespace alphanumerics
Packit e6c7a3
undef
Packit e6c7a3
Packit e6c7a3
=head1 SYNOPSIS
Packit e6c7a3
Packit e6c7a3
    use Term::ANSIColor;
Packit e6c7a3
    print color('bold blue');
Packit e6c7a3
    print "This text is bold blue.\n";
Packit e6c7a3
    print color('reset');
Packit e6c7a3
    print "This text is normal.\n";
Packit e6c7a3
    print colored("Yellow on magenta.", 'yellow on_magenta'), "\n";
Packit e6c7a3
    print "This text is normal.\n";
Packit e6c7a3
    print colored(['yellow on_magenta'], 'Yellow on magenta.', "\n");
Packit e6c7a3
    print colored(['red on_bright_yellow'], 'Red on bright yellow.', "\n");
Packit e6c7a3
    print colored(['bright_red on_black'], 'Bright red on black.', "\n");
Packit e6c7a3
    print "\n";
Packit e6c7a3
Packit e6c7a3
    # Map escape sequences back to color names.
Packit e6c7a3
    use Term::ANSIColor 1.04 qw(uncolor);
Packit e6c7a3
    my $names = uncolor('01;31');
Packit e6c7a3
    print join(q{ }, @{$names}), "\n";
Packit e6c7a3
Packit e6c7a3
    # Strip all color escape sequences.
Packit e6c7a3
    use Term::ANSIColor 2.01 qw(colorstrip);
Packit e6c7a3
    print colorstrip("\e[1mThis is bold\e[0m"), "\n";
Packit e6c7a3
Packit e6c7a3
    # Determine whether a color is valid.
Packit e6c7a3
    use Term::ANSIColor 2.02 qw(colorvalid);
Packit e6c7a3
    my $valid = colorvalid('blue bold', 'on_magenta');
Packit e6c7a3
    print "Color string is ", $valid ? "valid\n" : "invalid\n";
Packit e6c7a3
Packit e6c7a3
    # Create new aliases for colors.
Packit e6c7a3
    use Term::ANSIColor 4.00 qw(coloralias);
Packit e6c7a3
    coloralias('alert', 'red');
Packit e6c7a3
    print "Alert is ", coloralias('alert'), "\n";
Packit e6c7a3
    print colored("This is in red.", 'alert'), "\n";
Packit e6c7a3
Packit e6c7a3
    use Term::ANSIColor qw(:constants);
Packit e6c7a3
    print BOLD, BLUE, "This text is in bold blue.\n", RESET;
Packit e6c7a3
Packit e6c7a3
    use Term::ANSIColor qw(:constants);
Packit e6c7a3
    {
Packit e6c7a3
        local $Term::ANSIColor::AUTORESET = 1;
Packit e6c7a3
        print BOLD BLUE "This text is in bold blue.\n";
Packit e6c7a3
        print "This text is normal.\n";
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
    use Term::ANSIColor 2.00 qw(:pushpop);
Packit e6c7a3
    print PUSHCOLOR RED ON_GREEN "This text is red on green.\n";
Packit e6c7a3
    print PUSHCOLOR BRIGHT_BLUE "This text is bright blue on green.\n";
Packit e6c7a3
    print RESET BRIGHT_BLUE "This text is just bright blue.\n";
Packit e6c7a3
    print POPCOLOR "Back to red on green.\n";
Packit e6c7a3
    print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\n";
Packit e6c7a3
    print "This text is red on green.\n";
Packit e6c7a3
    {
Packit e6c7a3
        local $Term::ANSIColor::AUTOLOCAL = 1;
Packit e6c7a3
        print ON_BLUE "This text is red on blue.\n";
Packit e6c7a3
        print "This text is red on green.\n";
Packit e6c7a3
    }
Packit e6c7a3
    print POPCOLOR "Back to whatever we started as.\n";
Packit e6c7a3
Packit e6c7a3
=head1 DESCRIPTION
Packit e6c7a3
Packit e6c7a3
This module has two interfaces, one through color() and colored() and the
Packit e6c7a3
other through constants.  It also offers the utility functions uncolor(),
Packit e6c7a3
colorstrip(), colorvalid(), and coloralias(), which have to be explicitly
Packit e6c7a3
imported to be used (see L</SYNOPSIS>).
Packit e6c7a3
Packit e6c7a3
See L</COMPATIBILITY> for the versions of Term::ANSIColor that introduced
Packit e6c7a3
particular features and the versions of Perl that included them.
Packit e6c7a3
Packit e6c7a3
=head2 Supported Colors
Packit e6c7a3
Packit e6c7a3
Terminal emulators that support color divide into three types: ones that
Packit e6c7a3
support only eight colors, ones that support sixteen, and ones that
Packit e6c7a3
support 256.  This module provides the ANSI escape codes for all of them.
Packit e6c7a3
These colors are referred to as ANSI colors 0 through 7 (normal), 8
Packit e6c7a3
through 15 (16-color), and 16 through 255 (256-color).
Packit e6c7a3
Packit e6c7a3
Unfortunately, interpretation of colors 0 through 7 often depends on
Packit e6c7a3
whether the emulator supports eight colors or sixteen colors.  Emulators
Packit e6c7a3
that only support eight colors (such as the Linux console) will display
Packit e6c7a3
colors 0 through 7 with normal brightness and ignore colors 8 through 15,
Packit e6c7a3
treating them the same as white.  Emulators that support 16 colors, such
Packit e6c7a3
as gnome-terminal, normally display colors 0 through 7 as dim or darker
Packit e6c7a3
versions and colors 8 through 15 as normal brightness.  On such emulators,
Packit e6c7a3
the "normal" white (color 7) usually is shown as pale grey, requiring
Packit e6c7a3
bright white (15) to be used to get a real white color.  Bright black
Packit e6c7a3
usually is a dark grey color, although some terminals display it as pure
Packit e6c7a3
black.  Some sixteen-color terminal emulators also treat normal yellow
Packit e6c7a3
(color 3) as orange or brown, and bright yellow (color 11) as yellow.
Packit e6c7a3
Packit e6c7a3
Following the normal convention of sixteen-color emulators, this module
Packit e6c7a3
provides a pair of attributes for each color.  For every normal color (0
Packit e6c7a3
through 7), the corresponding bright color (8 through 15) is obtained by
Packit e6c7a3
prepending the string C<bright_> to the normal color name.  For example,
Packit e6c7a3
C<red> is color 1 and C<bright_red> is color 9.  The same applies for
Packit e6c7a3
background colors: C<on_red> is the normal color and C<on_bright_red> is
Packit e6c7a3
the bright color.  Capitalize these strings for the constant interface.
Packit e6c7a3
Packit e6c7a3
For 256-color emulators, this module additionally provides C<ansi0>
Packit e6c7a3
through C<ansi15>, which are the same as colors 0 through 15 in
Packit e6c7a3
sixteen-color emulators but use the 256-color escape syntax, C<grey0>
Packit e6c7a3
through C<grey23> ranging from nearly black to nearly white, and a set of
Packit e6c7a3
RGB colors.  The RGB colors are of the form C<rgbI<RGB>> where I<R>, I<G>,
Packit e6c7a3
and I are numbers from 0 to 5 giving the intensity of red, green, and
Packit e6c7a3
blue.  The grey and RGB colors are also available as C<ansi16> through
Packit e6c7a3
C<ansi255> if you want simple names for all 256 colors.  C<on_> variants
Packit e6c7a3
of all of these colors are also provided.  These colors may be ignored
Packit e6c7a3
completely on non-256-color terminals or may be misinterpreted and produce
Packit e6c7a3
random behavior.  Additional attributes such as blink, italic, or bold may
Packit e6c7a3
not work with the 256-color palette.
Packit e6c7a3
Packit e6c7a3
There is unfortunately no way to know whether the current emulator
Packit e6c7a3
supports more than eight colors, which makes the choice of colors
Packit e6c7a3
difficult.  The most conservative choice is to use only the regular
Packit e6c7a3
colors, which are at least displayed on all emulators.  However, they will
Packit e6c7a3
appear dark in sixteen-color terminal emulators, including most common
Packit e6c7a3
emulators in UNIX X environments.  If you know the display is one of those
Packit e6c7a3
emulators, you may wish to use the bright variants instead.  Even better,
Packit e6c7a3
offer the user a way to configure the colors for a given application to
Packit e6c7a3
fit their terminal emulator.
Packit e6c7a3
Packit e6c7a3
=head2 Function Interface
Packit e6c7a3
Packit e6c7a3
The function interface uses attribute strings to describe the colors and
Packit e6c7a3
text attributes to assign to text.  The recognized non-color attributes
Packit e6c7a3
are clear, reset, bold, dark, faint, italic, underline, underscore, blink,
Packit e6c7a3
reverse, and concealed.  Clear and reset (reset to default attributes),
Packit e6c7a3
dark and faint (dim and saturated), and underline and underscore are
Packit e6c7a3
equivalent, so use whichever is the most intuitive to you.
Packit e6c7a3
Packit e6c7a3
Note that not all attributes are supported by all terminal types, and some
Packit e6c7a3
terminals may not support any of these sequences.  Dark and faint, italic,
Packit e6c7a3
blink, and concealed in particular are frequently not implemented.
Packit e6c7a3
Packit e6c7a3
The recognized normal foreground color attributes (colors 0 to 7) are:
Packit e6c7a3
Packit e6c7a3
  black  red  green  yellow  blue  magenta  cyan  white
Packit e6c7a3
Packit e6c7a3
The corresponding bright foreground color attributes (colors 8 to 15) are:
Packit e6c7a3
Packit e6c7a3
  bright_black  bright_red      bright_green  bright_yellow
Packit e6c7a3
  bright_blue   bright_magenta  bright_cyan   bright_white
Packit e6c7a3
Packit e6c7a3
The recognized normal background color attributes (colors 0 to 7) are:
Packit e6c7a3
Packit e6c7a3
  on_black  on_red      on_green  on yellow
Packit e6c7a3
  on_blue   on_magenta  on_cyan   on_white
Packit e6c7a3
Packit e6c7a3
The recognized bright background color attributes (colors 8 to 15) are:
Packit e6c7a3
Packit e6c7a3
  on_bright_black  on_bright_red      on_bright_green  on_bright_yellow
Packit e6c7a3
  on_bright_blue   on_bright_magenta  on_bright_cyan   on_bright_white
Packit e6c7a3
Packit e6c7a3
For 256-color terminals, the recognized foreground colors are:
Packit e6c7a3
Packit e6c7a3
  ansi0 .. ansi255
Packit e6c7a3
  grey0 .. grey23
Packit e6c7a3
Packit e6c7a3
plus C<rgbI<RGB>> for I<R>, I<G>, and I values from 0 to 5, such as
Packit e6c7a3
C<rgb000> or C<rgb515>.  Similarly, the recognized background colors are:
Packit e6c7a3
Packit e6c7a3
  on_ansi0 .. on_ansi255
Packit e6c7a3
  on_grey0 .. on_grey23
Packit e6c7a3
Packit e6c7a3
plus C<on_rgbI<RGB>> for I<R>, I<G>, and I values from 0 to 5.
Packit e6c7a3
Packit e6c7a3
For any of the above listed attributes, case is not significant.
Packit e6c7a3
Packit e6c7a3
Attributes, once set, last until they are unset (by printing the attribute
Packit e6c7a3
C<clear> or C<reset>).  Be careful to do this, or otherwise your attribute
Packit e6c7a3
will last after your script is done running, and people get very annoyed
Packit e6c7a3
at having their prompt and typing changed to weird colors.
Packit e6c7a3
Packit e6c7a3
=over 4
Packit e6c7a3
Packit e6c7a3
=item color(ATTR[, ATTR ...])
Packit e6c7a3
Packit e6c7a3
color() takes any number of strings as arguments and considers them to be
Packit e6c7a3
space-separated lists of attributes.  It then forms and returns the escape
Packit e6c7a3
sequence to set those attributes.  It doesn't print it out, just returns
Packit e6c7a3
it, so you'll have to print it yourself if you want to.  This is so that
Packit e6c7a3
you can save it as a string, pass it to something else, send it to a file
Packit e6c7a3
handle, or do anything else with it that you might care to.  color()
Packit e6c7a3
throws an exception if given an invalid attribute.
Packit e6c7a3
Packit e6c7a3
=item colored(STRING, ATTR[, ATTR ...])
Packit e6c7a3
Packit e6c7a3
=item colored(ATTR-REF, STRING[, STRING...])
Packit e6c7a3
Packit e6c7a3
As an aid in resetting colors, colored() takes a scalar as the first
Packit e6c7a3
argument and any number of attribute strings as the second argument and
Packit e6c7a3
returns the scalar wrapped in escape codes so that the attributes will be
Packit e6c7a3
set as requested before the string and reset to normal after the string.
Packit e6c7a3
Alternately, you can pass a reference to an array as the first argument,
Packit e6c7a3
and then the contents of that array will be taken as attributes and color
Packit e6c7a3
codes and the remainder of the arguments as text to colorize.
Packit e6c7a3
Packit e6c7a3
Normally, colored() just puts attribute codes at the beginning and end of
Packit e6c7a3
the string, but if you set $Term::ANSIColor::EACHLINE to some string, that
Packit e6c7a3
string will be considered the line delimiter and the attribute will be set
Packit e6c7a3
at the beginning of each line of the passed string and reset at the end of
Packit e6c7a3
each line.  This is often desirable if the output contains newlines and
Packit e6c7a3
you're using background colors, since a background color that persists
Packit e6c7a3
across a newline is often interpreted by the terminal as providing the
Packit e6c7a3
default background color for the next line.  Programs like pagers can also
Packit e6c7a3
be confused by attributes that span lines.  Normally you'll want to set
Packit e6c7a3
$Term::ANSIColor::EACHLINE to C<"\n"> to use this feature.
Packit e6c7a3
Packit e6c7a3
=item uncolor(ESCAPE)
Packit e6c7a3
Packit e6c7a3
uncolor() performs the opposite translation as color(), turning escape
Packit e6c7a3
sequences into a list of strings corresponding to the attributes being set
Packit e6c7a3
by those sequences.  uncolor() will never return C<ansi16> through
Packit e6c7a3
C<ansi255>, instead preferring the C<grey> and C<rgb> names (and likewise
Packit e6c7a3
for C<on_ansi16> through C<on_ansi255>).
Packit e6c7a3
Packit e6c7a3
=item colorstrip(STRING[, STRING ...])
Packit e6c7a3
Packit e6c7a3
colorstrip() removes all color escape sequences from the provided strings,
Packit e6c7a3
returning the modified strings separately in array context or joined
Packit e6c7a3
together in scalar context.  Its arguments are not modified.
Packit e6c7a3
Packit e6c7a3
=item colorvalid(ATTR[, ATTR ...])
Packit e6c7a3
Packit e6c7a3
colorvalid() takes attribute strings the same as color() and returns true
Packit e6c7a3
if all attributes are known and false otherwise.
Packit e6c7a3
Packit e6c7a3
=item coloralias(ALIAS[, ATTR])
Packit e6c7a3
Packit e6c7a3
If ATTR is specified, coloralias() sets up an alias of ALIAS for the
Packit e6c7a3
standard color ATTR.  From that point forward, ALIAS can be passed into
Packit e6c7a3
color(), colored(), and colorvalid() and will have the same meaning as
Packit e6c7a3
ATTR.  One possible use of this facility is to give more meaningful names
Packit e6c7a3
to the 256-color RGB colors.  Only ASCII alphanumerics, C<.>, C<_>, and
Packit e6c7a3
C<-> are allowed in alias names.
Packit e6c7a3
Packit e6c7a3
If ATTR is not specified, coloralias() returns the standard color name to
Packit e6c7a3
which ALIAS is aliased, if any, or undef if ALIAS does not exist.
Packit e6c7a3
Packit e6c7a3
This is the same facility used by the ANSI_COLORS_ALIASES environment
Packit e6c7a3
variable (see L</ENVIRONMENT> below) but can be used at runtime, not just
Packit e6c7a3
when the module is loaded.
Packit e6c7a3
Packit e6c7a3
Later invocations of coloralias() with the same ALIAS will override
Packit e6c7a3
earlier aliases.  There is no way to remove an alias.
Packit e6c7a3
Packit e6c7a3
Aliases have no effect on the return value of uncolor().
Packit e6c7a3
Packit e6c7a3
B<WARNING>: Aliases are global and affect all callers in the same process.
Packit e6c7a3
There is no way to set an alias limited to a particular block of code or a
Packit e6c7a3
particular object.
Packit e6c7a3
Packit e6c7a3
=back
Packit e6c7a3
Packit e6c7a3
=head2 Constant Interface
Packit e6c7a3
Packit e6c7a3
Alternately, if you import C<:constants>, you can use the following
Packit e6c7a3
constants directly:
Packit e6c7a3
Packit e6c7a3
  CLEAR           RESET             BOLD            DARK
Packit e6c7a3
  FAINT           ITALIC            UNDERLINE       UNDERSCORE
Packit e6c7a3
  BLINK           REVERSE           CONCEALED
Packit e6c7a3
Packit e6c7a3
  BLACK           RED               GREEN           YELLOW
Packit e6c7a3
  BLUE            MAGENTA           CYAN            WHITE
Packit e6c7a3
  BRIGHT_BLACK    BRIGHT_RED        BRIGHT_GREEN    BRIGHT_YELLOW
Packit e6c7a3
  BRIGHT_BLUE     BRIGHT_MAGENTA    BRIGHT_CYAN     BRIGHT_WHITE
Packit e6c7a3
Packit e6c7a3
  ON_BLACK        ON_RED            ON_GREEN        ON_YELLOW
Packit e6c7a3
  ON_BLUE         ON_MAGENTA        ON_CYAN         ON_WHITE
Packit e6c7a3
  ON_BRIGHT_BLACK ON_BRIGHT_RED     ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
Packit e6c7a3
  ON_BRIGHT_BLUE  ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN  ON_BRIGHT_WHITE
Packit e6c7a3
Packit e6c7a3
These are the same as color('attribute') and can be used if you prefer
Packit e6c7a3
typing:
Packit e6c7a3
Packit e6c7a3
    print BOLD BLUE ON_WHITE "Text", RESET, "\n";
Packit e6c7a3
Packit e6c7a3
to
Packit e6c7a3
Packit e6c7a3
    print colored ("Text", 'bold blue on_white'), "\n";
Packit e6c7a3
Packit e6c7a3
(Note that the newline is kept separate to avoid confusing the terminal as
Packit e6c7a3
described above since a background color is being used.)
Packit e6c7a3
Packit e6c7a3
If you import C<:constants256>, you can use the following constants
Packit e6c7a3
directly:
Packit e6c7a3
Packit e6c7a3
  ANSI0 .. ANSI255
Packit e6c7a3
  GREY0 .. GREY23
Packit e6c7a3
Packit e6c7a3
  RGBXYZ (for X, Y, and Z values from 0 to 5, like RGB000 or RGB515)
Packit e6c7a3
Packit e6c7a3
  ON_ANSI0 .. ON_ANSI255
Packit e6c7a3
  ON_GREY0 .. ON_GREY23
Packit e6c7a3
Packit e6c7a3
  ON_RGBXYZ (for X, Y, and Z values from 0 to 5)
Packit e6c7a3
Packit e6c7a3
Note that C<:constants256> does not include the other constants, so if you
Packit e6c7a3
want to mix both, you need to include C<:constants> as well.  You may want
Packit e6c7a3
to explicitly import at least C<RESET>, as in:
Packit e6c7a3
Packit e6c7a3
    use Term::ANSIColor 4.00 qw(RESET :constants256);
Packit e6c7a3
Packit e6c7a3
When using the constants, if you don't want to have to remember to add the
Packit e6c7a3
C<, RESET> at the end of each print line, you can set
Packit e6c7a3
$Term::ANSIColor::AUTORESET to a true value.  Then, the display mode will
Packit e6c7a3
automatically be reset if there is no comma after the constant.  In other
Packit e6c7a3
words, with that variable set:
Packit e6c7a3
Packit e6c7a3
    print BOLD BLUE "Text\n";
Packit e6c7a3
Packit e6c7a3
will reset the display mode afterward, whereas:
Packit e6c7a3
Packit e6c7a3
    print BOLD, BLUE, "Text\n";
Packit e6c7a3
Packit e6c7a3
will not.  If you are using background colors, you will probably want to
Packit e6c7a3
either use say() (in newer versions of Perl) or print the newline with a
Packit e6c7a3
separate print statement to avoid confusing the terminal.
Packit e6c7a3
Packit e6c7a3
If $Term::ANSIColor::AUTOLOCAL is set (see below), it takes precedence
Packit e6c7a3
over $Term::ANSIColor::AUTORESET, and the latter is ignored.
Packit e6c7a3
Packit e6c7a3
The subroutine interface has the advantage over the constants interface in
Packit e6c7a3
that only two subroutines are exported into your namespace, versus
Packit e6c7a3
thirty-eight in the constants interface.  On the flip side, the constants
Packit e6c7a3
interface has the advantage of better compile time error checking, since
Packit e6c7a3
misspelled names of colors or attributes in calls to color() and colored()
Packit e6c7a3
won't be caught until runtime whereas misspelled names of constants will
Packit e6c7a3
be caught at compile time.  So, pollute your namespace with almost two
Packit e6c7a3
dozen subroutines that you may not even use that often, or risk a silly
Packit e6c7a3
bug by mistyping an attribute.  Your choice, TMTOWTDI after all.
Packit e6c7a3
Packit e6c7a3
=head2 The Color Stack
Packit e6c7a3
Packit e6c7a3
You can import C<:pushpop> and maintain a stack of colors using PUSHCOLOR,
Packit e6c7a3
POPCOLOR, and LOCALCOLOR.  PUSHCOLOR takes the attribute string that
Packit e6c7a3
starts its argument and pushes it onto a stack of attributes.  POPCOLOR
Packit e6c7a3
removes the top of the stack and restores the previous attributes set by
Packit e6c7a3
the argument of a prior PUSHCOLOR.  LOCALCOLOR surrounds its argument in a
Packit e6c7a3
PUSHCOLOR and POPCOLOR so that the color resets afterward.
Packit e6c7a3
Packit e6c7a3
If $Term::ANSIColor::AUTOLOCAL is set, each sequence of color constants
Packit e6c7a3
will be implicitly preceded by LOCALCOLOR.  In other words, the following:
Packit e6c7a3
Packit e6c7a3
    {
Packit e6c7a3
        local $Term::ANSIColor::AUTOLOCAL = 1;
Packit e6c7a3
        print BLUE "Text\n";
Packit e6c7a3
    }
Packit e6c7a3
Packit e6c7a3
is equivalent to:
Packit e6c7a3
Packit e6c7a3
    print LOCALCOLOR BLUE "Text\n";
Packit e6c7a3
Packit e6c7a3
If $Term::ANSIColor::AUTOLOCAL is set, it takes precedence over
Packit e6c7a3
$Term::ANSIColor::AUTORESET, and the latter is ignored.
Packit e6c7a3
Packit e6c7a3
When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it's particularly
Packit e6c7a3
important to not put commas between the constants.
Packit e6c7a3
Packit e6c7a3
    print PUSHCOLOR BLUE "Text\n";
Packit e6c7a3
Packit e6c7a3
will correctly push BLUE onto the top of the stack.
Packit e6c7a3
Packit e6c7a3
    print PUSHCOLOR, BLUE, "Text\n";    # wrong!
Packit e6c7a3
Packit e6c7a3
will not, and a subsequent pop won't restore the correct attributes.
Packit e6c7a3
PUSHCOLOR pushes the attributes set by its argument, which is normally a
Packit e6c7a3
string of color constants.  It can't ask the terminal what the current
Packit e6c7a3
attributes are.
Packit e6c7a3
Packit e6c7a3
=head1 DIAGNOSTICS
Packit e6c7a3
Packit e6c7a3
=over 4
Packit e6c7a3
Packit e6c7a3
=item Bad color mapping %s
Packit e6c7a3
Packit e6c7a3
(W) The specified color mapping from ANSI_COLORS_ALIASES is not valid and
Packit e6c7a3
could not be parsed.  It was ignored.
Packit e6c7a3
Packit e6c7a3
=item Bad escape sequence %s
Packit e6c7a3
Packit e6c7a3
(F) You passed an invalid ANSI escape sequence to uncolor().
Packit e6c7a3
Packit e6c7a3
=item Bareword "%s" not allowed while "strict subs" in use
Packit e6c7a3
Packit e6c7a3
(F) You probably mistyped a constant color name such as:
Packit e6c7a3
Packit e6c7a3
    $Foobar = FOOBAR . "This line should be blue\n";
Packit e6c7a3
Packit e6c7a3
or:
Packit e6c7a3
Packit e6c7a3
    @Foobar = FOOBAR, "This line should be blue\n";
Packit e6c7a3
Packit e6c7a3
This will only show up under use strict (another good reason to run under
Packit e6c7a3
use strict).
Packit e6c7a3
Packit e6c7a3
=item Cannot alias standard color %s
Packit e6c7a3
Packit e6c7a3
(F) The alias name passed to coloralias() matches a standard color name.
Packit e6c7a3
Standard color names cannot be aliased.
Packit e6c7a3
Packit e6c7a3
=item Cannot alias standard color %s in %s
Packit e6c7a3
Packit e6c7a3
(W) The same, but in ANSI_COLORS_ALIASES.  The color mapping was ignored.
Packit e6c7a3
Packit e6c7a3
=item Invalid alias name %s
Packit e6c7a3
Packit e6c7a3
(F) You passed an invalid alias name to coloralias().  Alias names must
Packit e6c7a3
consist only of alphanumerics, C<.>, C<->, and C<_>.
Packit e6c7a3
Packit e6c7a3
=item Invalid alias name %s in %s
Packit e6c7a3
Packit e6c7a3
(W) You specified an invalid alias name on the left hand of the equal sign
Packit e6c7a3
in a color mapping in ANSI_COLORS_ALIASES.  The color mapping was ignored.
Packit e6c7a3
Packit e6c7a3
=item Invalid attribute name %s
Packit e6c7a3
Packit e6c7a3
(F) You passed an invalid attribute name to color(), colored(), or
Packit e6c7a3
coloralias().
Packit e6c7a3
Packit e6c7a3
=item Invalid attribute name %s in %s
Packit e6c7a3
Packit e6c7a3
(W) You specified an invalid attribute name on the right hand of the equal
Packit e6c7a3
sign in a color mapping in ANSI_COLORS_ALIASES.  The color mapping was
Packit e6c7a3
ignored.
Packit e6c7a3
Packit e6c7a3
=item Name "%s" used only once: possible typo
Packit e6c7a3
Packit e6c7a3
(W) You probably mistyped a constant color name such as:
Packit e6c7a3
Packit e6c7a3
    print FOOBAR "This text is color FOOBAR\n";
Packit e6c7a3
Packit e6c7a3
It's probably better to always use commas after constant names in order to
Packit e6c7a3
force the next error.
Packit e6c7a3
Packit e6c7a3
=item No comma allowed after filehandle
Packit e6c7a3
Packit e6c7a3
(F) You probably mistyped a constant color name such as:
Packit e6c7a3
Packit e6c7a3
    print FOOBAR, "This text is color FOOBAR\n";
Packit e6c7a3
Packit e6c7a3
Generating this fatal compile error is one of the main advantages of using
Packit e6c7a3
the constants interface, since you'll immediately know if you mistype a
Packit e6c7a3
color name.
Packit e6c7a3
Packit e6c7a3
=item No name for escape sequence %s
Packit e6c7a3
Packit e6c7a3
(F) The ANSI escape sequence passed to uncolor() contains escapes which
Packit e6c7a3
aren't recognized and can't be translated to names.
Packit e6c7a3
Packit e6c7a3
=back
Packit e6c7a3
Packit e6c7a3
=head1 ENVIRONMENT
Packit e6c7a3
Packit e6c7a3
=over 4
Packit e6c7a3
Packit e6c7a3
=item ANSI_COLORS_ALIASES
Packit e6c7a3
Packit e6c7a3
This environment variable allows the user to specify custom color aliases
Packit e6c7a3
that will be understood by color(), colored(), and colorvalid().  None of
Packit e6c7a3
the other functions will be affected, and no new color constants will be
Packit e6c7a3
created.  The custom colors are aliases for existing color names; no new
Packit e6c7a3
escape sequences can be introduced.  Only alphanumerics, C<.>, C<_>, and
Packit e6c7a3
C<-> are allowed in alias names.
Packit e6c7a3
Packit e6c7a3
The format is:
Packit e6c7a3
Packit e6c7a3
    ANSI_COLORS_ALIASES='newcolor1=oldcolor1,newcolor2=oldcolor2'
Packit e6c7a3
Packit e6c7a3
Whitespace is ignored.
Packit e6c7a3
Packit e6c7a3
For example the L<Solarized|http://ethanschoonover.com/solarized> colors
Packit e6c7a3
can be mapped with:
Packit e6c7a3
Packit e6c7a3
    ANSI_COLORS_ALIASES='\
Packit e6c7a3
        base00=bright_yellow, on_base00=on_bright_yellow,\
Packit e6c7a3
        base01=bright_green,  on_base01=on_bright_green, \
Packit e6c7a3
        base02=black,         on_base02=on_black,        \
Packit e6c7a3
        base03=bright_black,  on_base03=on_bright_black, \
Packit e6c7a3
        base0=bright_blue,    on_base0=on_bright_blue,   \
Packit e6c7a3
        base1=bright_cyan,    on_base1=on_bright_cyan,   \
Packit e6c7a3
        base2=white,          on_base2=on_white,         \
Packit e6c7a3
        base3=bright_white,   on_base3=on_bright_white,  \
Packit e6c7a3
        orange=bright_red,    on_orange=on_bright_red,   \
Packit e6c7a3
        violet=bright_magenta,on_violet=on_bright_magenta'
Packit e6c7a3
Packit e6c7a3
This environment variable is read and applied when the Term::ANSIColor
Packit e6c7a3
module is loaded and is then subsequently ignored.  Changes to
Packit e6c7a3
ANSI_COLORS_ALIASES after the module is loaded will have no effect.  See
Packit e6c7a3
coloralias() for an equivalent facility that can be used at runtime.
Packit e6c7a3
Packit e6c7a3
=item ANSI_COLORS_DISABLED
Packit e6c7a3
Packit e6c7a3
If this environment variable is set to a true value, all of the functions
Packit e6c7a3
defined by this module (color(), colored(), and all of the constants not
Packit e6c7a3
previously used in the program) will not output any escape sequences and
Packit e6c7a3
instead will just return the empty string or pass through the original
Packit e6c7a3
text as appropriate.  This is intended to support easy use of scripts
Packit e6c7a3
using this module on platforms that don't support ANSI escape sequences.
Packit e6c7a3
Packit e6c7a3
=back
Packit e6c7a3
Packit e6c7a3
=head1 COMPATIBILITY
Packit e6c7a3
Packit e6c7a3
Term::ANSIColor was first included with Perl in Perl 5.6.0.
Packit e6c7a3
Packit e6c7a3
The uncolor() function and support for ANSI_COLORS_DISABLED were added in
Packit e6c7a3
Term::ANSIColor 1.04, included in Perl 5.8.0.
Packit e6c7a3
Packit e6c7a3
Support for dark was added in Term::ANSIColor 1.08, included in Perl
Packit e6c7a3
5.8.4.
Packit e6c7a3
Packit e6c7a3
The color stack, including the C<:pushpop> import tag, PUSHCOLOR,
Packit e6c7a3
POPCOLOR, LOCALCOLOR, and the $Term::ANSIColor::AUTOLOCAL variable, was
Packit e6c7a3
added in Term::ANSIColor 2.00, included in Perl 5.10.1.
Packit e6c7a3
Packit e6c7a3
colorstrip() was added in Term::ANSIColor 2.01 and colorvalid() was added
Packit e6c7a3
in Term::ANSIColor 2.02, both included in Perl 5.11.0.
Packit e6c7a3
Packit e6c7a3
Support for colors 8 through 15 (the C<bright_> variants) was added in
Packit e6c7a3
Term::ANSIColor 3.00, included in Perl 5.13.3.
Packit e6c7a3
Packit e6c7a3
Support for italic was added in Term::ANSIColor 3.02, included in Perl
Packit e6c7a3
5.17.1.
Packit e6c7a3
Packit e6c7a3
Support for colors 16 through 256 (the C<ansi>, C<rgb>, and C<grey>
Packit e6c7a3
colors), the C<:constants256> import tag, the coloralias() function, and
Packit e6c7a3
support for the ANSI_COLORS_ALIASES environment variable were added in
Packit e6c7a3
Term::ANSIColor 4.00, included in Perl 5.17.8.
Packit e6c7a3
Packit e6c7a3
$Term::ANSIColor::AUTOLOCAL was changed to take precedence over
Packit e6c7a3
$Term::ANSIColor::AUTORESET, rather than the other way around, in
Packit e6c7a3
Term::ANSIColor 4.00, included in Perl 5.17.8.
Packit e6c7a3
Packit e6c7a3
C<ansi16> through C<ansi255>, as aliases for the C<rgb> and C<grey>
Packit e6c7a3
colors, and the corresponding C<on_ansi> names and C<ANSI> and C<ON_ANSI>
Packit e6c7a3
constants, were added in Term::ANSIColor 4.06.
Packit e6c7a3
Packit e6c7a3
=head1 RESTRICTIONS
Packit e6c7a3
Packit e6c7a3
It would be nice if one could leave off the commas around the constants
Packit e6c7a3
entirely and just say:
Packit e6c7a3
Packit e6c7a3
    print BOLD BLUE ON_WHITE "Text\n" RESET;
Packit e6c7a3
Packit e6c7a3
but the syntax of Perl doesn't allow this.  You need a comma after the
Packit e6c7a3
string.  (Of course, you may consider it a bug that commas between all the
Packit e6c7a3
constants aren't required, in which case you may feel free to insert
Packit e6c7a3
commas unless you're using $Term::ANSIColor::AUTORESET or
Packit e6c7a3
PUSHCOLOR/POPCOLOR.)
Packit e6c7a3
Packit e6c7a3
For easier debugging, you may prefer to always use the commas when not
Packit e6c7a3
setting $Term::ANSIColor::AUTORESET or PUSHCOLOR/POPCOLOR so that you'll
Packit e6c7a3
get a fatal compile error rather than a warning.
Packit e6c7a3
Packit e6c7a3
It's not possible to use this module to embed formatting and color
Packit e6c7a3
attributes using Perl formats.  They replace the escape character with a
Packit e6c7a3
space (as documented in L<perlform(1)>), resulting in garbled output from
Packit e6c7a3
the unrecognized attribute.  Even if there were a way around that problem,
Packit e6c7a3
the format doesn't know that the non-printing escape sequence is
Packit e6c7a3
zero-length and would incorrectly format the output.  For formatted output
Packit e6c7a3
using color or other attributes, either use sprintf() instead or use
Packit e6c7a3
formline() and then add the color or other attributes after formatting and
Packit e6c7a3
before output.
Packit e6c7a3
Packit e6c7a3
=head1 NOTES
Packit e6c7a3
Packit e6c7a3
The codes generated by this module are standard terminal control codes,
Packit e6c7a3
complying with ECMA-048 and ISO 6429 (generally referred to as "ANSI
Packit e6c7a3
color" for the color codes).  The non-color control codes (bold, dark,
Packit e6c7a3
italic, underline, and reverse) are part of the earlier ANSI X3.64
Packit e6c7a3
standard for control sequences for video terminals and peripherals.
Packit e6c7a3
Packit e6c7a3
Note that not all displays are ISO 6429-compliant, or even X3.64-compliant
Packit e6c7a3
(or are even attempting to be so).  This module will not work as expected
Packit e6c7a3
on displays that do not honor these escape sequences, such as cmd.exe,
Packit e6c7a3
4nt.exe, and command.com under either Windows NT or Windows 2000.  They
Packit e6c7a3
may just be ignored, or they may display as an ESC character followed by
Packit e6c7a3
some apparent garbage.
Packit e6c7a3
Packit e6c7a3
Jean Delvare provided the following table of different common terminal
Packit e6c7a3
emulators and their support for the various attributes and others have
Packit e6c7a3
helped me flesh it out:
Packit e6c7a3
Packit e6c7a3
              clear    bold     faint   under    blink   reverse  conceal
Packit e6c7a3
 ------------------------------------------------------------------------
Packit e6c7a3
 xterm         yes      yes      no      yes      yes      yes      yes
Packit e6c7a3
 linux         yes      yes      yes    bold      yes      yes      no
Packit e6c7a3
 rxvt          yes      yes      no      yes  bold/black   yes      no
Packit e6c7a3
 dtterm        yes      yes      yes     yes    reverse    yes      yes
Packit e6c7a3
 teraterm      yes    reverse    no      yes    rev/red    yes      no
Packit e6c7a3
 aixterm      kinda   normal     no      yes      no       yes      yes
Packit e6c7a3
 PuTTY         yes     color     no      yes      no       yes      no
Packit e6c7a3
 Windows       yes      no       no      no       no       yes      no
Packit e6c7a3
 Cygwin SSH    yes      yes      no     color    color    color     yes
Packit e6c7a3
 Terminal.app  yes      yes      no      yes      yes      yes      yes
Packit e6c7a3
Packit e6c7a3
Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation under
Packit e6c7a3
Cygwin on Windows NT, and Mac Terminal is the Terminal application in Mac
Packit e6c7a3
OS X.  Where the entry is other than yes or no, that emulator displays the
Packit e6c7a3
given attribute as something else instead.  Note that on an aixterm, clear
Packit e6c7a3
doesn't reset colors; you have to explicitly set the colors back to what
Packit e6c7a3
you want.  More entries in this table are welcome.
Packit e6c7a3
Packit e6c7a3
Support for code 3 (italic) is rare and therefore not mentioned in that
Packit e6c7a3
table.  It is not believed to be fully supported by any of the terminals
Packit e6c7a3
listed, although it's displayed as green in the Linux console, but it is
Packit e6c7a3
reportedly supported by urxvt.
Packit e6c7a3
Packit e6c7a3
Note that codes 6 (rapid blink) and 9 (strike-through) are specified in
Packit e6c7a3
ANSI X3.64 and ECMA-048 but are not commonly supported by most displays
Packit e6c7a3
and emulators and therefore aren't supported by this module at the present
Packit e6c7a3
time.  ECMA-048 also specifies a large number of other attributes,
Packit e6c7a3
including a sequence of attributes for font changes, Fraktur characters,
Packit e6c7a3
double-underlining, framing, circling, and overlining.  As none of these
Packit e6c7a3
attributes are widely supported or useful, they also aren't currently
Packit e6c7a3
supported by this module.
Packit e6c7a3
Packit e6c7a3
Most modern X terminal emulators support 256 colors.  Known to not support
Packit e6c7a3
those colors are aterm, rxvt, Terminal.app, and TTY/VC.
Packit e6c7a3
Packit e6c7a3
=head1 AUTHORS
Packit e6c7a3
Packit e6c7a3
Original idea (using constants) by Zenin, reimplemented using subs by Russ
Packit e6c7a3
Allbery <rra@cpan.org>, and then combined with the original idea by
Packit e6c7a3
Russ with input from Zenin.  256-color support is based on work by Kurt
Packit e6c7a3
Starsinic.  Russ Allbery now maintains this module.
Packit e6c7a3
Packit e6c7a3
PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
Packit e6c7a3
voice solutions.
Packit e6c7a3
Packit e6c7a3
=head1 COPYRIGHT AND LICENSE
Packit e6c7a3
Packit e6c7a3
Copyright 1996 Zenin
Packit e6c7a3
Packit e6c7a3
Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009, 2010,
Packit e6c7a3
2011, 2012, 2013, 2014, 2015, 2016 Russ Allbery <rra@cpan.org>
Packit e6c7a3
Packit e6c7a3
Copyright 2012 Kurt Starsinic <kstarsinic@gmail.com>
Packit e6c7a3
Packit e6c7a3
This program is free software; you may redistribute it and/or modify it
Packit e6c7a3
under the same terms as Perl itself.
Packit e6c7a3
Packit e6c7a3
=head1 SEE ALSO
Packit e6c7a3
Packit e6c7a3
The CPAN module L<Term::ExtendedColor> provides a different and more
Packit e6c7a3
comprehensive interface for 256-color emulators that may be more
Packit e6c7a3
convenient.  The CPAN module L<Win32::Console::ANSI> provides ANSI color
Packit e6c7a3
(and other escape sequence) support in the Win32 Console environment.
Packit e6c7a3
The CPAN module L<Term::Chrome> provides a different interface using
Packit e6c7a3
objects and operator overloading.
Packit e6c7a3
Packit e6c7a3
ECMA-048 is available on-line (at least at the time of this writing) at
Packit e6c7a3
L<http://www.ecma-international.org/publications/standards/Ecma-048.htm>.
Packit e6c7a3
Packit e6c7a3
ISO 6429 is available from ISO for a charge; the author of this module
Packit e6c7a3
does not own a copy of it.  Since the source material for ISO 6429 was
Packit e6c7a3
ECMA-048 and the latter is available for free, there seems little reason
Packit e6c7a3
to obtain the ISO standard.
Packit e6c7a3
Packit e6c7a3
The 256-color control sequences are documented at
Packit e6c7a3
L<http://invisible-island.net/xterm/ctlseqs/ctlseqs.html> (search for
Packit e6c7a3
256-color).
Packit e6c7a3
Packit e6c7a3
The current version of this module is always available from its web site
Packit e6c7a3
at L<https://www.eyrie.org/~eagle/software/ansicolor/>.  It is also part
Packit e6c7a3
of the Perl core distribution as of 5.6.0.
Packit e6c7a3
Packit e6c7a3
=cut