Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO_COLOR: the empty string should not disable color #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/Term/ANSIColor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ sub AUTOLOAD {

# If colors are disabled, just return the input. Do this without
# installing a sub for (marginal, unbenchmarked) speed.
if ($ENV{ANSI_COLORS_DISABLED} || defined($ENV{NO_COLOR})) {
if ($ENV{ANSI_COLORS_DISABLED} || length($ENV{NO_COLOR})) {
return join(q{}, @_);
}

Expand All @@ -290,7 +290,7 @@ sub AUTOLOAD {
## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
my $eval_result = eval qq{
sub $AUTOLOAD {
if (\$ENV{ANSI_COLORS_DISABLED} || defined(\$ENV{NO_COLOR})) {
if (\$ENV{ANSI_COLORS_DISABLED} || length(\$ENV{NO_COLOR})) {
return join(q{}, \@_);
} elsif (\$AUTOLOCAL && \@_) {
return PUSHCOLOR('$escape') . join(q{}, \@_) . POPCOLOR;
Expand Down Expand Up @@ -388,7 +388,7 @@ sub color {
my (@codes) = @_;

# Return the empty string if colors are disabled.
if ($ENV{ANSI_COLORS_DISABLED} || defined($ENV{NO_COLOR})) {
if ($ENV{ANSI_COLORS_DISABLED} || length($ENV{NO_COLOR})) {
return q{};
}

Expand Down Expand Up @@ -513,7 +513,7 @@ sub colored {
}

# Return the string unmolested if colors are disabled.
if ($ENV{ANSI_COLORS_DISABLED} || defined($ENV{NO_COLOR})) {
if ($ENV{ANSI_COLORS_DISABLED} || length($ENV{NO_COLOR})) {
return $string;
}

Expand Down Expand Up @@ -1201,12 +1201,12 @@ escape sequences.

=item NO_COLOR

If this environment variable is set to any value, it suppresses generation of
escape sequences the same as if ANSI_COLORS_DISABLED is set to a true value.
This implements the L<https://no-color.org/> informal standard. Programs that
want to enable color despite NO_COLOR being set will need to unset that
environment variable before any constant or function provided by this module
is used.
If this environment variable is set to any value (other than the empty string),
it suppresses generation of escape sequences the same as if
ANSI_COLORS_DISABLED is set to a true value. This implements the
L<https://no-color.org/> informal standard. Programs that want to enable color
despite NO_COLOR being set will need to unset that environment variable before
any constant or function provided by this module is used.

=back

Expand Down
4 changes: 2 additions & 2 deletions t/module/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ is(
);
is((BLUE 'testing'), 'testing', 'Constant support for NO_COLOR');
local $ENV{NO_COLOR} = q{};
is(color('blue'), q{}, 'color support for NO_COLOR with empty string');
is(color('blue'), qq{\e[34m}, 'color support for NO_COLOR with empty string');
is(
(RED 'testing'),
'testing',
"\e[31mtesting",
'Constant support for NO_COLOR with empty string',
);
delete $ENV{NO_COLOR};
Expand Down