views:

405

answers:

3

I'm trying to patch a waf issue, where the Windows command prompt output isn't coloured when it's supposed to be. I'm trying to figure out how to actually implement this patch, but I'm having trouble finding sufficient resources - could someone point me in right direction?

Update 1

Please don't suggest anything that requires Cygwin.

A: 

Look into CURSES for python : http://www.amk.ca/python/howto/curses/

(Using curses)

stdscr.addstr( "Pretty text", curses.color_pair(1) )
stdscr.refresh()
Bob Breznak
Does it require Cygwin?
nbolton
I do not believe it does. I used it in the past under Linux but it should be a stand alone thing.
Bob Breznak
I thought curses was Linux only, unless I'm mistaken... Do you know of any Windows specific solutions?
nbolton
Take a look at http://adamv.com/dev/python/curses/ There is a Windows solution using curses and python.
Bob Breznak
wcurses does not require cygwin, but does use its own class of windows, NOT the regular cmd.exe, as "pseudo-consoles" for Python interpreters.
Alex Martelli
A: 

If you're keen on using normal cmd.exe consoles for the Python interactive interpreter, see this recipe. If you're OK with using special windows simulating a console, for example because you also need more advanced curses functionality anyway, then @TheLobster's suggestion of wcurses is just fine.

Alex Martelli
+7  A: 

It is possible thanks to ctypes and SetConsoleTextAttribute

Here is an example

from ctypes import *

windll.Kernel32.GetStdHandle.restype = c_ulong
h = windll.Kernel32.GetStdHandle(c_ulong(0xfffffff5))
for color in xrange(16):
    windll.Kernel32.SetConsoleTextAttribute(h, color)
    print "hello"
luc
A quick note for other people trying this--it won't work in IDLE, use python.exe or run it as a script in cmd.exe
tgray
Excellent! :) Thanks
nbolton