Skip to content

Commit d4c7cc5

Browse files
committed
fix/improve color handling for MSWin32 platforms
+ rationale: using Win32::Console::ANSI simplifies the code and improves Win32 outputs - simplified usage ~ Win32::Console::ANSI is fire-and-forget - once loaded, use Term::ANSIColor without regard for platform - fixes incorrect color 'reset' behavior - Win32::Color::ANSI resets correctly to the prior default color attribute set - bug due to incorrect assumption that LIGHTGRAY on BLACK are always the default color attributes - fixes [GH#26](#26) - Win32::Console::ANSI works for all output (no tie to a specific output handle) - [PR#34](#34) is no longer needed
1 parent c6381af commit d4c7cc5

File tree

2 files changed

+13
-45
lines changed

2 files changed

+13
-45
lines changed

bin/prove

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ if output is not to a terminal, color is disabled. You can override this by
113113
adding the C<--color> switch.
114114

115115
Color support requires L<Term::ANSIColor> on Unix-like platforms and
116-
L<Win32::Console> on windows. If the necessary module is not installed
116+
L<Win32::Console::ANSI> on windows. If the necessary module is not installed
117117
colored output will not be available.
118118

119119
=head2 Exit Code

lib/TAP/Formatter/Color.pm

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,24 @@ my $NO_COLOR;
1212
BEGIN {
1313
$NO_COLOR = 0;
1414

15+
eval 'use Term::ANSIColor';
16+
if ($@) {
17+
$NO_COLOR = $@;
18+
};
1519
if (IS_WIN32) {
16-
eval 'use Win32::Console';
20+
eval 'use Win32::Console::ANSI';
1721
if ($@) {
1822
$NO_COLOR = $@;
1923
}
20-
else {
21-
my $console = Win32::Console->new( STD_OUTPUT_HANDLE() );
22-
23-
# eval here because we might not know about these variables
24-
my $fg = eval '$FG_LIGHTGRAY';
25-
my $bg = eval '$BG_BLACK';
26-
27-
*set_color = sub {
28-
my ( $self, $output, $color ) = @_;
29-
30-
my $var;
31-
if ( $color eq 'reset' ) {
32-
$fg = eval '$FG_LIGHTGRAY';
33-
$bg = eval '$BG_BLACK';
34-
}
35-
elsif ( $color =~ /^on_(.+)$/ ) {
36-
$bg = eval '$BG_' . uc($1);
37-
}
38-
else {
39-
$fg = eval '$FG_' . uc($color);
40-
}
41-
42-
# In case of colors that aren't defined
43-
$self->set_color('reset')
44-
unless defined $bg && defined $fg;
45-
46-
$console->Attr( $bg | $fg );
47-
};
48-
}
49-
}
50-
else {
51-
eval 'use Term::ANSIColor';
52-
if ($@) {
53-
$NO_COLOR = $@;
54-
}
55-
else {
56-
*set_color = sub {
57-
my ( $self, $output, $color ) = @_;
58-
$output->( color($color) );
59-
};
60-
}
61-
}
24+
};
6225

6326
if ($NO_COLOR) {
6427
*set_color = sub { };
28+
} else {
29+
*set_color = sub {
30+
my ( $self, $output, $color ) = @_;
31+
$output->( color($color) );
32+
};
6533
}
6634
}
6735

@@ -87,7 +55,7 @@ in color. Passing tests are printed in green. Failing tests are in red.
8755
Skipped tests are blue on a white background and TODO tests are printed in
8856
white.
8957
90-
If L<Term::ANSIColor> cannot be found (or L<Win32::Console> if running
58+
If L<Term::ANSIColor> cannot be found (or L<Win32::Console::ANSI> if running
9159
under Windows) tests will be run without color.
9260
9361
=head1 SYNOPSIS

0 commit comments

Comments
 (0)