Win32::Console::ANSI
La perfection est atteinte non quand il n'y a plus rien à ajouter,
mais quand il n'y a plus rien à retrancher.
[Perfection is achieved, not when there is nothing left to add,
but when there is nothing left to take away.]
Antoine de Saint-Exupéry
Introduction
Windows NT/2000/XP does not support ANSI escape sequences in Win32 Console
applications. One can use the ansi.sys driver but only in the MS-DOS subsystem
with the old COMMAND.COM, not with CMD.EXE (See the
Microsoft Knowledge Base).
The module Win32::Console::ANSI (former Win32::ANSIConsole) emulate an ANSI
console for the script which uses it.
See the doc for the supported escape sequences:
Console::ANSI doc
Caution: this module is still in beta stage;
don't use it in a production script.
Back to Top
Colors rendition
Back to Top
Characters conversion
By default, this module convert the characters from the Windows code page
to the DOS code page (the so-called ANSI to OEM conversion). The conversion
permits the display of the accented characters in the console like in
a Windows-based editor. For example, on my machine, Windows use the codepage
cp1252 and DOS use the codepage the cp850. Here, the conversion is made by
the Encode module :
For comparison and additional information, see :
Microsoft GlobalDev site.
Back to Top
Extra escape sequence
The previous conversion is necessary because the code page of Windows and
the code page of the DOS are different. Another solution would be to change the
code page of the console: it is possible, but only if the console uses an
Unicode police (See the
Microsoft Knowledge Base).
In this case, you can change the code page with the escape sequence
\e(#X where # is the code page needed. (It's not a
standard escape sequence!) It is then possible to change the code page when
one wants :
(Lucida Console 14 pt)
Note: you can change the console code page with the DOS command
chcp. (Don't forget: this command is efficient only if the
police in use is an Unicode police.)
Type help chcp in the console for more info.
The available code pages are under the Register key :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
Back to Top
Download
Current version is 1.04
The module was rewritten in C.
By hooking the WriteFile API function in all the modules loaded by the
script, it is possible to interpret the escape sequences just before they
are written in the console. There is no perturbance with Perl or your script.
This suppresses several previous versions limitations:
- No tied handles (we can duplicate STDOUT or STDERR as usual and the
module works now with IPC::Open2 and IPC::Open3)
- The bug with the join method is fixed. We can use threads.
- The conversion of the Windows codepage to the console codepage (ANSI to
OEM conversion) is done by the internal Windows functions. The
Encode module is not used anymore (the exe made with PAR are smaller).
- The module is smaller and 20 times faster on average.
Successfully tested on Win98, Win98SE, Win2000, WinXP, Windows Server 2003,
Vista (32 and 64 bits), Windows 7-beta (32 and 64 bits).
The module is in my ppm repository.
If you are using ActiveState's Perl distribution (Perl 5.6, Perl 5.8 and Perl 5.10), the easiest way to install
this module is to use ppm. Enter (or cut & paste) this command in a DOS console:
ppm install http://www.bribes.org/perl/ppm/Win32-Console-ANSI.ppd
The documentation, in html format, is at its usual location.
If you want, you can download the source file here:
Win32-Console-ANSI-1.04.tar.gz
or from the
CPAN
To install the module, read the README file.
Back to Top
Links
From the PerlMonks site, some JAPH using Win32::Console::ANSI:
Back to Top

BðP © 2003-2009 J-L Morel - Contact : jl_morel@bribes.org