Perlito5

 view release on metacpan or  search on metacpan

lib/Perlito5X/Term/ANSIColor.pm  view on Meta::CPAN

    'bright_cyan'    => 96,   'on_bright_cyan'    => 106,
    'bright_white'   => 97,   'on_bright_white'   => 107,
);
#>>>

# Generating the 256-color codes involves a lot of codes and offsets that are
# not helped by turning them into constants.

# The first 16 256-color codes are duplicates of the 16 ANSI colors.  The rest
# are RBG and greyscale values.
for my $code (0 .. 15) {
    $ATTRIBUTES{"ansi$code"}    = "38;5;$code";
    $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
}

# 256-color RGB colors.  Red, green, and blue can each be values 0 through 5,
# and the resulting 216 colors start with color 16.
for my $r (0 .. 5) {
    for my $g (0 .. 5) {
        for my $b (0 .. 5) {
            my $code = 16 + (6 * 6 * $r) + (6 * $g) + $b;
            $ATTRIBUTES{"rgb$r$g$b"}    = "38;5;$code";
            $ATTRIBUTES{"on_rgb$r$g$b"} = "48;5;$code";
        }
    }
}

# The last 256-color codes are 24 shades of grey.
for my $n (0 .. 23) {
    my $code = $n + 232;
    $ATTRIBUTES{"grey$n"}    = "38;5;$code";
    $ATTRIBUTES{"on_grey$n"} = "48;5;$code";
}

# Reverse lookup.  Alphabetically first name for a sequence is preferred.
our %ATTRIBUTES_R;
for my $attr (reverse sort keys %ATTRIBUTES) {
    $ATTRIBUTES_R{ $ATTRIBUTES{$attr} } = $attr;
}

# Provide ansiN names for all 256 characters to provide a convenient flat
# namespace if one doesn't want to mess with the RGB and greyscale naming.  Do
# this after creating %ATTRIBUTES_R since we want to use the canonical names
# when reversing a color.
for my $code (16 .. 255) {
    $ATTRIBUTES{"ansi$code"}    = "38;5;$code";
    $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
}

# Import any custom colors set in the environment.
our %ALIASES;
if (exists $ENV{ANSI_COLORS_ALIASES}) {
    my $spec = $ENV{ANSI_COLORS_ALIASES};
    $spec =~ s{\s+}{}xmsg;

    # Error reporting here is an interesting question.  Use warn rather than
    # carp because carp would report the line of the use or require, which
    # doesn't help anyone understand what's going on, whereas seeing this code
    # will be more helpful.
    ## no critic (ErrorHandling::RequireCarping)
    for my $definition (split m{,}xms, $spec) {
        my ($new, $old) = split m{=}xms, $definition, 2;
        if (!$new || !$old) {
            warn qq{Bad color mapping "$definition"};
        } else {
            my $result = eval { coloralias($new, $old) };
            if (!$result) {
                my $error = $@;
                $error =~ s{ [ ] at [ ] .* }{}xms;
                warn qq{$error in "$definition"};
            }
        }
    }
}

# Stores the current color stack maintained by PUSHCOLOR and POPCOLOR.  This
# is global and therefore not threadsafe.
our @COLORSTACK;

##############################################################################
# Helper functions
##############################################################################

# Stub to load the Carp module on demand.
sub croak {
    my (@args) = @_;
    require Carp;
    Carp::croak(@args);
}

##############################################################################
# Implementation (constant form)
##############################################################################

# Time to have fun!  We now want to define the constant subs, which are named
# the same as the attributes above but in all caps.  Each constant sub needs
# to act differently depending on whether $AUTORESET is set.  Without
# autoreset:
#
#     BLUE "text\n"  ==>  "\e[34mtext\n"
#
# If $AUTORESET is set, we should instead get:
#
#     BLUE "text\n"  ==>  "\e[34mtext\n\e[0m"
#
# The sub also needs to handle the case where it has no arguments correctly.
# Maintaining all of this as separate subs would be a major nightmare, as well
# as duplicate the %ATTRIBUTES hash, so instead we define an AUTOLOAD sub to
# 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 to a true value,
# just return the arguments without adding any escape sequences.  This is to
# make it easier to write scripts that also work on systems without any ANSI
# support, like Windows consoles.
#
# Avoid using character classes like [:upper:] and \w here, since they load
# Unicode character tables and consume a ton of memory.  All of our constants
# only use ASCII characters.
#
## no critic (ClassHierarchies::ProhibitAutoloading)



( run in 0.616 second using v1.01-cache-2.11-cpan-71847e10f99 )