Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to use Term::ANSIColor module?.

by perlsen (Chaplain)
on Feb 10, 2005 at 07:29 UTC ( [id://429607]=perlquestion: print w/replies, xml ) Need Help??

perlsen has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have started the Term::ANSIColor module. But it cannot responds what i expect.
How to format the code to view screen visual output(what i expected).
I have tried this in command prompt.

use Term::ANSIColor qw(:constants); print BLACK, ON_WHITE, "black on white\n"; print WHITE, ON_BLACK, "white on black\n"; print GREEN, ON_CYAN, BLINK, "garish!\n"; print RESET; output in screen: D:\sample>tes1.pl ?[30m?[47mblack on white ?[37m?[40mwhite on black ?[32m?[46m?[5mgarish! ?[0m

Thanks in advance for your suggestions.

Replies are listed 'Best First'.
Re: How to use Term::ANSIColor module?.
by BrowserUk (Patriarch) on Feb 10, 2005 at 08:10 UTC

    cmd.exe does not support ANSI escape sequences.

    Loading ansi.sys will not make cmd.exe support them, only command.com--the old, 16-bit, totally braindead (as opposed to only half braindead) command line processoer.

    However, CPAN does have a module that will allow you to use ansi escape sequences to control the cmd.exe consol session colour attributes: Win32::Console::ANSI. If you load it before you load Term::ANSIColor, you will get (mostly) the effect you require. Try this:

    #! perl -slw use strict; use Win32::Console::ANSI; use Term::ANSIColor qw(:constants); print BLACK, ON_WHITE, "black on white\n"; print WHITE, ON_BLACK, "white on black\n"; print GREEN, ON_CYAN, BLINK, "garish!\n"; print RESET;

    One caveat. It makes the assumption that the default colors (RESET) are white on black. If you use black on white by default as I do, it is annoying for it to reset these to white on black. It should query the current settings on start up and reset to those.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

      Thanks, BrowserUk and all.
      Now the code is working fine.

      other than Win32::Console::ANSI, anywhere to execute Perl script with colour in Windows?

        Yes. You could use Win32::Console directly.

        At a pinch, you could even do

        perl -E"say 'hi'; system 'color fc'; <STDIN>; say 'bye'; system 'color + 79'; <STDIN>; say 'gone'"

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: How to use Term::ANSIColor module?.
by cowboy (Friar) on Feb 10, 2005 at 07:33 UTC
    It seems to be printing the ANSI control codes properly. You need a terminal that supports them.

    On windows, try loading an ANSI driver. (hint, find a file called ansi.sys, rename it to ansi.com, and execute it, and the shell should be ansi capable)

    Update: Even better, see microsofts help file on the subject.
    How to enable ANSI.SYS in a command window

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://429607]
Approved by cog
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found