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

Color is non-functional (Win32) #26

Open
garfieldnate opened this issue Oct 26, 2013 · 5 comments
Open

Color is non-functional (Win32) #26

garfieldnate opened this issue Oct 26, 2013 · 5 comments

Comments

@garfieldnate
Copy link
Contributor

I have installed Win32::Console (and Term::ANSIColor just in case), but do not see color output. Googling around I got a more detailed SO question. I know that color output otherwise works because ack now has colored output, and it also uses Win32::Console to achieve this.
Using Windows 7, Strawberry 5.18.1.1, Test::Harness 3.29.

@Leont
Copy link
Member

Leont commented Oct 26, 2013

Color is not enabled by default on Windows, see here. Does it work if you explicitly enable it?

@garfieldnate
Copy link
Contributor Author

Oh, that should be put in the documentation, too. But no, it's still just white on black when I use -c or --color, just like the guy in the SO question.

@garfieldnate
Copy link
Contributor Author

I gave a shot at fixing this and completely failed, but maybe I can get the obvious ones out of the way.
I added the following lines in Tap/Formatter/Color.pm at the end of the *set_color sub:

$console->Attr($Win32::Console::FG_RED);
print "hello!\n";
$console->Flush; 

This printed hello!, but the console color was not changed to red (which it does in a separate standalone test).
If you remove line 46, which sets the background and foreground colors on the Win32::Console object, then it kills Perl during test running: Could not execute ("C:\...\perl.exe" t\my_test.t): open3: Can't close: Bad file descriptor at C:/.../perl/site/lib/TAP/Parser/Iterator/Process.pm line 153.

At this point I have absolutely no idea what to try. It's like the console is correctly being colored, but then we print to a different console or something. Wish I could help more :(

@garfieldnate
Copy link
Contributor Author

A little more debugging: in the above code, if you replace print "hello!\n"; with $console->Write( "hello!\n" );, then nothing is printed out, but it should be.
If you move the my $console = Win32::Console->new( STD_OUTPUT_HANDLE() ); line inside of the *set_color block, then the color is set correctly! But now perl hangs, after warning with:

Use of uninitialized value $fd in pattern match (m//) at C:/.../perl/lib/IO/Handle.pm line 374.
Use of uninitialized value $fd in concatenation (.) or string at C:/.../per/lib/IO/Handle.pm line 379.

I traced the call stack back to IPC::Open3 line 410, which calls IO::Handle::fdopen, but $fd->{tmp_copy} and $fd->{mode} have become undefined, I guess because I called Win32::Console::new.

It seems to me that Win32::Console and IPC::Open3 do not play nicely, or at least they are not being made to play nicely.

@mjcarman
Copy link

The problem (as far as I can tell) is that it's applying color settings to the wrong handle (STDOUT instead of STDERR). With that trivial change I see colorized output. I've submitted a pull request.

That said, the output isn't quite what I expected. Test failures are red, but passed tests and diagnostics aren't colorized. The only time I see green is for an "all tests passed" summary.

Another option to fix the issue is to drop use of Win32::Console altogether. Term::ANSIColor works on Windows if you also load Win32::Console::ANSI. That would simplify the code in TAP::Formatter::Color. I don't know if there's a reason to prefer one module over the other.

rivy added a commit to rivy/perl.Test-Harness that referenced this issue Sep 11, 2016
+ 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](Perl-Toolchain-Gang#26)
  - Win32::Console::ANSI works for all output (no tie to a specific output handle)
  - [PR#34](Perl-Toolchain-Gang#34) is no longer needed
Leont pushed a commit that referenced this issue Jul 23, 2017
+ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants