I wanted to add a little color to a command-line program, to highlight a few things. Windows NT/2k/XP doesn't have ANSI X3.64 control codes in the console window.

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

    You might want to take a look at Win32::Console::ANSI


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: color text output in Windows
by jand (Friar) on Sep 05, 2003 at 16:46 UTC
    I believe you can enable ANSI control characters in Windows consoles by adding:
    device=%SystemRoot%\system32\ansi.sys
    
    to your %SystemRoot%\system32\CONFIG.NT file.

      No, unfortunately you can't. ansi.sys is a 16-but windows drivers. I only works in command.com sessions not cmd.exe sessions. Using command.com as your console shell is pointless.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.