From d4c7cc5069d7be48e0d3bfec362a41e2e357ecaa Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 11 Sep 2016 02:18:38 -0500 Subject: [PATCH 1/5] 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](https://github.com/Perl-Toolchain-Gang/Test-Harness/issues/26) - Win32::Console::ANSI works for all output (no tie to a specific output handle) - [PR#34](https://github.com/Perl-Toolchain-Gang/Test-Harness/pull/34) is no longer needed --- bin/prove | 2 +- lib/TAP/Formatter/Color.pm | 56 ++++++++------------------------------ 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/bin/prove b/bin/prove index 6637cc44..448507b6 100755 --- a/bin/prove +++ b/bin/prove @@ -113,7 +113,7 @@ if output is not to a terminal, color is disabled. You can override this by adding the C<--color> switch. Color support requires L on Unix-like platforms and -L on windows. If the necessary module is not installed +L on windows. If the necessary module is not installed colored output will not be available. =head2 Exit Code diff --git a/lib/TAP/Formatter/Color.pm b/lib/TAP/Formatter/Color.pm index 43084d14..eee6e9f7 100644 --- a/lib/TAP/Formatter/Color.pm +++ b/lib/TAP/Formatter/Color.pm @@ -12,56 +12,24 @@ my $NO_COLOR; BEGIN { $NO_COLOR = 0; + eval 'use 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->( color($color) ); + }; } } @@ -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 cannot be found (or L if running +If L cannot be found (or L if running under Windows) tests will be run without color. =head1 SYNOPSIS From bd6af7d4344e405e2ade88cc00735951dd646a8a Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 11 Sep 2016 13:58:53 -0500 Subject: [PATCH 2/5] remove IS_WIN32 platform test for color from `prove` + rationale: platform dependencies are handled elsewhere/elsewhen by the test harness - enables default use of color on 'MSWin32' systems --- lib/App/Prove.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/App/Prove.pm b/lib/App/Prove.pm index c3216592..f522832d 100644 --- a/lib/App/Prove.pm +++ b/lib/App/Prove.pm @@ -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 { From 1a1bd5a12a10362b4335cd72882846ef3dd2d0ee Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 11 Sep 2016 14:12:05 -0500 Subject: [PATCH 3/5] minor documentation correction for TAP::Formatter::Color --- lib/TAP/Formatter/Color.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/TAP/Formatter/Color.pm b/lib/TAP/Formatter/Color.pm index eee6e9f7..4723ea9c 100644 --- a/lib/TAP/Formatter/Color.pm +++ b/lib/TAP/Formatter/Color.pm @@ -55,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 cannot be found (or L if running +If L cannot be found (and L if running under Windows) tests will be run without color. =head1 SYNOPSIS From 4789cca3f71f4ee21577742bd855d844ae997281 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 11 Sep 2016 15:28:00 -0500 Subject: [PATCH 4/5] minor documentation correction for `prove` --- bin/prove | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/prove b/bin/prove index 448507b6..13286472 100755 --- a/bin/prove +++ b/bin/prove @@ -108,12 +108,12 @@ matching the pattern C. =head2 Colored Test Output -Colored test output using L is the default, but -if output is not to a terminal, color is disabled. You can override this by +Colored test output using L 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 on Unix-like platforms and -L on windows. If the necessary module is not installed +Color support requires L and, on windows platforms, also +L. If the necessary module(s) are not installed colored output will not be available. =head2 Exit Code From 279e44f087ba65aa696b7c03cbf0821094a9ea08 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 2 Oct 2016 12:24:28 -0500 Subject: [PATCH 5/5] clarify use of Term::ANSIColor::color() in TAP::Formatter::Color --- lib/TAP/Formatter/Color.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/TAP/Formatter/Color.pm b/lib/TAP/Formatter/Color.pm index 4723ea9c..7ed477a3 100644 --- a/lib/TAP/Formatter/Color.pm +++ b/lib/TAP/Formatter/Color.pm @@ -12,7 +12,7 @@ my $NO_COLOR; BEGIN { $NO_COLOR = 0; - eval 'use Term::ANSIColor'; + eval 'require Term::ANSIColor'; if ($@) { $NO_COLOR = $@; }; @@ -28,7 +28,7 @@ BEGIN { } else { *set_color = sub { my ( $self, $output, $color ) = @_; - $output->( color($color) ); + $output->( Term::ANSIColor::color($color) ); }; } }