| Win32::Console::ANSI - Perl extension to emulate ANSI console on Win32 system. |
Win32::Console::ANSI - Perl extension to emulate ANSI console on Win32 system.
use Win32::Console::ANSI;
print "\e[1;34mThis text is bold blue.\e[0m\n"; print "This text is normal.\n"; print "\e[33;45;1mBold yellow on magenta.\e[0m\n"; print "This text is normal.\n";
With the Term::ANSIColor module one increases the readability:
use Win32::Console::ANSI; use Term::ANSIColor;
print color 'bold blue';
print "This text is bold blue.\n";
print color 'reset';
print "This text is normal.\n";
print colored ("Bold yellow on magenta.\n", 'bold yellow on_magenta');
print "This text is normal.\n";
And even more with Term::ANSIScreen:
use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/;
locate 1, 1; print "@ This is (1,1)", savepos;
print locate(24,60), "@ This is (24,60)"; loadpos;
print down(2), clline, "@ This is (3,16)\n";
color 'black on white'; clline;
print "This line is black on white.\n";
print color 'reset'; print "This text is normal.\n";
print colored ("This text is bold blue.\n", 'bold blue');
print "This text is normal.\n";
print colored ['bold blue'], "This text is bold blue.\n";
print "This text is normal.\n";
Windows NT/2000/XP does not support ANSI escape sequences in Win32 Console applications. This module emulates an ANSI console for the script that uses it and also converts the characters from Windows code page to DOS code page (the so-called ANSI to OEM conversion). This conversion permits the display of the accented characters in the console like in the Windows- based editor used to type the script.
\e[J is equivalent to \e[0J. (Some terminal/emulators respond to \e[J as if it were \e[2J. Here, the default is 0; it is the norm)
\e[K is equivalent to \e[0K. (Some terminal/emulators respond to \e[K as if it were \e[2K. Here, the default is 0; it is the norm)
0 All attributes off
1 Bold on
4 Underscore on
7 Reverse video on
8 Concealed on
21 Bold off
24 Underscore off
27 Reverse video off
28 Concealed off
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
\e[m is equivalent to \e[0m.
The conversion is done by the Windows internal functions. If a character cannot be represented in the console code page it is replaced by a question mark character.
If (and only if) the console uses a Unicode police, it is possible to change its codepage with this escape sequence. No effect with an ordinary ``Raster Font''. (For Windows NT/2000/XP the currently available Unicode console font is the Lucida Console TrueType font.) # is the number of the codepage needed, 855 for cp855 for instance.
The two following escape sequences are not in the ANSI standard. These are private escape sequences introduced by DEC for the VT-300 series of video terminals.
Because the module exports no symbols into the callers namespace, it is necessary to import the names of the functions before using them.
use Win32::Console::ANSI qw/ Title /;
for (reverse 0..5) {
Title "Count down ... $_";
sleep 1;
}
use Win32::Console::ANSI qw/ Cursor /; ($x, $y) = Cursor(); # reads cursor position Cursor(5, 8); # puts the cursor at column 5, line 8 Cursor(5, 0); # puts the cursor at column 5, line doesn't change Cursor(0, 8); # puts the cursor at line 8, column doesn't change Cursor(0, 0); # the cursor doesn't change a position (useless!) ($x, $y) = Cursor(5, 8); # reads cursor position AND puts cursor at (5, 8)
($x, $y) = Cursor() we have always 1 <= $x <= $Xmax and
1 <= $y <= $Ymax.
$width and $height cannot be less than the width and height
of the screen buffer's window. The specified dimensions also cannot be less
than the minimum size allowed by the system.
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero and the extended error
message is in $^E.
The parameter $state can be one of the following values:
If the console window was previously visible, the return value is nonzero.
If the console window was previously hidden, the return value is zero.
Example:
#!/usr/bin/perl -w
use strict;
use Win32::Console::ANSI qw/ :SW_ /;
MinimizeAll();
sleep 2;
ShowConsoleWindow(SW_SHOWMAXIMIZED);
sleep 2;
ShowConsoleWindow(SW_HIDE);
sleep 2;
ShowConsoleWindow(SW_RESTORE);
SetCloseButton( 0 ) disables the close button [x] of the console
window and deletes the CLOSE menu item from the console menu system.
SetCloseButton( 1 ) enables the close button [x] of the console window
and restores the CLOSE menu item from the console menu system.
If the function succeeds, the return value is nonzero else, the return value is zero.
For obvious reasons, the button is re-established and the menu restored at the end of the script.
Example:
#!/usr/bin/perl -w
use strict;
use Win32::Console::ANSI qw/ SetCloseButton /;
$SIG{INT}='IGNORE'; # no Ctrl-C interrupt
SetCloseButton(0); # no close button
print "No close button, no Ctrl-C interrupt\n",
" Press [Enter]...\n";
do { $_ = <STDIN> } until defined;
$SIG{INT}=''; # Ctrl-C interrupt
print "No close button, Ctrl-C interrupt enabled\n",
" Press [Enter]...\n";
do { $_ = <STDIN> } until defined;
SetCloseButton(1); # restore close button
print "Close button available\n Press [Enter]...\n";
do { $_ = <STDIN> } until defined;
SetConsoleFullScreen( 1 ) sets the console in full-screen mode.
SetConsoleFullScreen( 0 ) sets the console in windowed mode.
If the function succeeds, the return value is nonzero. If the function
fails, the return value is zero and the extended error message is in
$^E).
The parameter $state can be one of the following constants:
Example:
#!/usr/bin/perl -w
use strict;
use Win32::Console::ANSI qw/ :MS_ /;
SetMonitorState(MS_STANDBY);
sleep 10; # standby for 10 sec
SetMonitorState(MS_ON);
Nothing by default; the function names and constants must be explicitly exported.
SetMonitorState and the MS_* constants.
MinimizeAll, ShowConsoleWindow and the SW_* constants
Due to DOS-console limitations, the blink mode (text attributes 5 and 25) is not implemented.
If you use an integrated environment for developing your program you can see strange results on the console controlled by your IDE. The IDE catches and processes the output of your program so your program does not see a ``real'' console. In this case test your program in a separate console.
the Win32::Console manpage, the Term::ANSIColor manpage, the Term::ANSIScreen manpage.
J-L Morel <jl_morel@bribes.org>
Home page: http://www.bribes.org/perl/wANSIConsole.html
Report bug: http://rt.cpan.org/Public/Dist/Display.html?Name=Win32-Console-ANSI
Copyright (c) 2003-2013 J-L Morel. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Win32::Console::ANSI - Perl extension to emulate ANSI console on Win32 system. |