10

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.

Community
  • 1
  • 1
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242

2 Answers2

21

It is possible thanks to ctypes and SetConsoleTextAttribute

Here is an example

from ctypes import *
STD_OUTPUT_HANDLE_ID = c_ulong(0xfffffff5)
windll.Kernel32.GetStdHandle.restype = c_ulong
std_output_hdl = windll.Kernel32.GetStdHandle(STD_OUTPUT_HANDLE_ID)
for color in xrange(16):
    windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, color)
    print "hello"
SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25
luc
  • 41,928
  • 25
  • 127
  • 172
  • 3
    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 Aug 25 '09 at 15:39
  • I would encourage the more modern searcher to look at how this is done by iPython, which does things in a generally very smart and portable way. – meawoppl Feb 11 '14 at 21:27
3

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
  • 854,459
  • 170
  • 1,222
  • 1,395