[Haskell-cafe] Windows console

Andrew Coppin andrewcoppin at btinternet.com
Tue Sep 9 15:15:08 EDT 2008


When coding in a POSIX-compliant environment, you can usually write 
special escape codes to the console to change the text colour and so 
forth. However, this does not work on Windows.

(Ignore all references you see to enabling ANSI.SYS in your config.sys 
file; this applies only to 16-bit MS-DOS programs, *not* to 32-bit 
Windows programs.)

However - to my surprise - apparently the Win32 API provides a set of 
function calls that do in fact allow 32-bit applications to change the 
colours of the console on a character-by-character basis. However - wait 
for it - those *particular* functions aren't in Haskell's 
System.Win32.Console module. :-(

Obviously, I know nothing about C. However, after much hunting around, 
it turns out that for some reason, GHC appears to ship with a complete 
set of C header files for the Win32 API. (In other words, the necessary 
header file for accessing the functions I want is there.) After 
staggering through the (very unhelpful) FFI language specification, I 
was able to make it so that I can apparently call these functions from 
Haskell. I was not, however, able to find any way at all to import the 
symbolic constants necessary, so I was forced to reading through the 
source code of the raw C header files to find out what the numeric 
values of these are (!!!)

The long and short of it is, I now have a small Haskell library that 
enables me to print things in trippy colours on the screen from a normal 
Haskell CLI program. Yay for me! (Actually, you can also change the 
title on the window - so you can make your program say "My Fantastic 
Tool" instead of "C:\Documents and 
Settings\foo\Haskell\MyTool\MyTool.exe" in the titlebar. Again, this is 
for a normal Haskell console application with nothing done to it.)

Would anybody else be interested in having this code? (Obviously, it's 
pretty tiny.) For that matter, would the maintainer of 
System.Win32.Console be interested in adding the necessary dozen lines 
of code to that module?

Actually, now that I think about it, it would be kind of nice to have a 
magic package that writes out escape codes or calls the Win32 API 
depending on which platform your program is compiled on - in the style 
of System.FilePath. I don't know how to do that though... A nice idea, guys?



More information about the Haskell-Cafe mailing list