How to colour text in a Windows cmd.exe shell

A

alex

I'd like to colour my output. Term::ANSIColor looks great,
but unfortunately i'm on windows using cmd.exe and it doesn't want
to interpret the colour codes. Other than changing shell,
is there anyway i can get a coloured output?

The best idea i've come up with is to pipe my ouput thro' "more":

# code borrowed from Term::ANSIColor docs
use Term::ANSIColor;
open STDOUT, "|more" or die "Can't redirect STDOUT: $!"; # !!!
print color 'bold blue';
print "This text is bold blue.\n";
print color 'reset';
print "This text is normal.\n";
print colored ("Yellow on magenta.\n", 'yellow on_magenta');
print "This text is normal.\n";
print colored ['yellow on_magenta'], "Yellow on magenta.\n";


which works, but i'm wondering if i can do what "more" is
doing in Perl without resorting to an external process.

Many thanks!
 
A

A. Sinan Unur

I'd like to colour my output. Term::ANSIColor looks great,
but unfortunately i'm on windows using cmd.exe and it doesn't want
to interpret the colour codes. Other than changing shell,
is there anyway i can get a coloured output?

The best idea i've come up with is to pipe my ouput thro' "more":

I don't know how that works. Maybe you have an external 'more' installed.

See
<URL:http://cpan.uwinnipeg.ca/htdocs/ANSIColor/Term/ANSIColor.html#notes>

Sinan
 
T

Thomas Kratz

alex said:
I'd like to colour my output. Term::ANSIColor looks great,
but unfortunately i'm on windows using cmd.exe and it doesn't want
to interpret the colour codes. Other than changing shell,
is there anyway i can get a coloured output?

You could try Win32::Console.
The best idea i've come up with is to pipe my ouput thro' "more":

# code borrowed from Term::ANSIColor docs
use Term::ANSIColor;
open STDOUT, "|more" or die "Can't redirect STDOUT: $!"; # !!!
print color 'bold blue';
print "This text is bold blue.\n";
print color 'reset';
print "This text is normal.\n";
print colored ("Yellow on magenta.\n", 'yellow on_magenta');
print "This text is normal.\n";
print colored ['yellow on_magenta'], "Yellow on magenta.\n";


which works, but i'm wondering if i can do what "more" is
doing in Perl without resorting to an external process.

IIRC you have to load ansi.sys for cmd.exe. Try putting a line like
'device=%systemroot%\system32\ansi.sys' into the file 'config.nt' in
%systemroot%\system32. It should work, but I haven't tried it

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
G

Gerhard Sprenger

(e-mail address removed) wrote
I'd like to colour my output. Term::ANSIColor looks great,
but unfortunately i'm on windows using cmd.exe and it doesn't want
to interpret the colour codes. Other than changing shell,
is there anyway i can get a coloured output?

As someone mentioned earlier, I also suggest

Win32::Console::ANSI

located at http://www.bribes.org/perl/ppm/
as Win32-Console-ANSI-0.07-PPM58.tar.gz
or Win32-Console-ANSI-0.07-PPM56.tar.gz
(PPD is Win32-Console-ANSI.ppd)

Be sure to use version 0.06 or later. Otherwise redirections
are broken.

Just include the following code snippet

if ($^O eq "MSWin32") {
require Win32::Console::ANSI;
}

and you may use the ansi escape sequences
described in "ISO 6429 colors"
(e.g. http://www.perpetualpc.net/6429_colors.html)

Gerhard.
 
A

alex

Thanks Guys!

Win32::Console::ANSI did the trick. Now to get playing with my new
crayon set!

alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top