So, I played around with the Win32::Console module. Setting the current attribute will affect subsequent output, even if they come through layers of file stuff first. The secret is to make sure you flush before changing it again.
Rather than using the Console::Write function, I use ordinary print so that it still works if the output is redirected to a file. The changing attribute of the console window is simply ignored, and the same text is printed anyway.
use strict; use warnings; use IO::Handle; use Win32::Console; my $CONSOLE = new Win32::Console(STD_OUTPUT_HANDLE); my $save_attr= $CONSOLE->Attr; my $caption_attr= 10; # bright green on black sub print_color ($@) { my $color= shift; STDOUT->flush(); $CONSOLE->Attr ($color); print @_; STDOUT->flush(); $CONSOLE->Attr ($save_attr); } print_color ($caption_attr, "Hello ", 42, " world.\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: color text output in Windows
by BrowserUk (Patriarch) on Sep 04, 2003 at 19:21 UTC | |
|
Re: color text output in Windows
by jand (Friar) on Sep 05, 2003 at 16:46 UTC | |
by BrowserUk (Patriarch) on Sep 05, 2003 at 17:27 UTC |