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

fix and simplify MSWin32 colorization #54

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 4 additions & 4 deletions bin/prove
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ matching the pattern C<t/*.t>.

=head2 Colored Test Output

Colored test output using L<TAP::Formatter::Color> is the default, but
if output is not to a terminal, color is disabled. You can override this by
Colored test output using L<TAP::Formatter::Color> is the default, but
if output is not to a terminal, color is disabled. You can override this by
adding the C<--color> switch.

Color support requires L<Term::ANSIColor> on Unix-like platforms and
L<Win32::Console> on windows. If the necessary module is not installed
Color support requires L<Term::ANSIColor> and, on windows platforms, also
L<Win32::Console::ANSI>. If the necessary module(s) are not installed
colored output will not be available.

=head2 Exit Code
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Prove.pm
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ sub _help {
sub _color_default {
my $self = shift;

return -t STDOUT && !$ENV{HARNESS_NOTTY} && !IS_WIN32;
return -t STDOUT && !$ENV{HARNESS_NOTTY};
}

sub _get_args {
Expand Down
56 changes: 12 additions & 44 deletions lib/TAP/Formatter/Color.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,24 @@ my $NO_COLOR;
BEGIN {
$NO_COLOR = 0;

eval 'require Term::ANSIColor';
if ($@) {
$NO_COLOR = $@;
};
if (IS_WIN32) {
eval 'use Win32::Console';
eval 'use Win32::Console::ANSI';
if ($@) {
$NO_COLOR = $@;
}
else {
my $console = Win32::Console->new( STD_OUTPUT_HANDLE() );

# eval here because we might not know about these variables
my $fg = eval '$FG_LIGHTGRAY';
my $bg = eval '$BG_BLACK';

*set_color = sub {
my ( $self, $output, $color ) = @_;

my $var;
if ( $color eq 'reset' ) {
$fg = eval '$FG_LIGHTGRAY';
$bg = eval '$BG_BLACK';
}
elsif ( $color =~ /^on_(.+)$/ ) {
$bg = eval '$BG_' . uc($1);
}
else {
$fg = eval '$FG_' . uc($color);
}

# In case of colors that aren't defined
$self->set_color('reset')
unless defined $bg && defined $fg;

$console->Attr( $bg | $fg );
};
}
}
else {
eval 'use Term::ANSIColor';
if ($@) {
$NO_COLOR = $@;
}
else {
*set_color = sub {
my ( $self, $output, $color ) = @_;
$output->( color($color) );
};
}
}
};

if ($NO_COLOR) {
*set_color = sub { };
} else {
*set_color = sub {
my ( $self, $output, $color ) = @_;
$output->( Term::ANSIColor::color($color) );
};
}
}

Expand All @@ -87,7 +55,7 @@ in color. Passing tests are printed in green. Failing tests are in red.
Skipped tests are blue on a white background and TODO tests are printed in
white.

If L<Term::ANSIColor> cannot be found (or L<Win32::Console> if running
If L<Term::ANSIColor> cannot be found (and L<Win32::Console::ANSI> if running
under Windows) tests will be run without color.

=head1 SYNOPSIS
Expand Down