michael@0: """ michael@0: From Andre Burgaud's Blog, from the CTypes Wiki: michael@0: http://www.burgaud.com/bring-colors-to-the-windows-console-with-python/ michael@0: michael@0: Colors text in console mode application (win32). michael@0: Uses ctypes and Win32 methods SetConsoleTextAttribute and michael@0: GetConsoleScreenBufferInfo. michael@0: michael@0: $Id: color_console.py 534 2009-05-10 04:00:59Z andre $ michael@0: """ michael@0: michael@0: from ctypes import windll, Structure, c_short, c_ushort, byref michael@0: michael@0: SHORT = c_short michael@0: WORD = c_ushort michael@0: michael@0: class COORD(Structure): michael@0: """struct in wincon.h.""" michael@0: _fields_ = [ michael@0: ("X", SHORT), michael@0: ("Y", SHORT)] michael@0: michael@0: class SMALL_RECT(Structure): michael@0: """struct in wincon.h.""" michael@0: _fields_ = [ michael@0: ("Left", SHORT), michael@0: ("Top", SHORT), michael@0: ("Right", SHORT), michael@0: ("Bottom", SHORT)] michael@0: michael@0: class CONSOLE_SCREEN_BUFFER_INFO(Structure): michael@0: """struct in wincon.h.""" michael@0: _fields_ = [ michael@0: ("dwSize", COORD), michael@0: ("dwCursorPosition", COORD), michael@0: ("wAttributes", WORD), michael@0: ("srWindow", SMALL_RECT), michael@0: ("dwMaximumWindowSize", COORD)] michael@0: michael@0: # winbase.h michael@0: STD_INPUT_HANDLE = -10 michael@0: STD_OUTPUT_HANDLE = -11 michael@0: STD_ERROR_HANDLE = -12 michael@0: michael@0: # wincon.h michael@0: FOREGROUND_BLACK = 0x0000 michael@0: FOREGROUND_BLUE = 0x0001 michael@0: FOREGROUND_GREEN = 0x0002 michael@0: FOREGROUND_CYAN = 0x0003 michael@0: FOREGROUND_RED = 0x0004 michael@0: FOREGROUND_MAGENTA = 0x0005 michael@0: FOREGROUND_YELLOW = 0x0006 michael@0: FOREGROUND_GREY = 0x0007 michael@0: FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified. michael@0: michael@0: BACKGROUND_BLACK = 0x0000 michael@0: BACKGROUND_BLUE = 0x0010 michael@0: BACKGROUND_GREEN = 0x0020 michael@0: BACKGROUND_CYAN = 0x0030 michael@0: BACKGROUND_RED = 0x0040 michael@0: BACKGROUND_MAGENTA = 0x0050 michael@0: BACKGROUND_YELLOW = 0x0060 michael@0: BACKGROUND_GREY = 0x0070 michael@0: BACKGROUND_INTENSITY = 0x0080 # background color is intensified. michael@0: michael@0: stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) michael@0: SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute michael@0: GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo michael@0: michael@0: def get_text_attr(): michael@0: csbi = CONSOLE_SCREEN_BUFFER_INFO() michael@0: GetConsoleScreenBufferInfo(stdout_handle, byref(csbi)) michael@0: return csbi.wAttributes michael@0: michael@0: DEFAULT_COLORS = get_text_attr() michael@0: michael@0: class Terminal(object): michael@0: COLOR = { michael@0: 'black': 0x0000, michael@0: 'blue': 0x0001, michael@0: 'green': 0x0002, michael@0: 'cyan': 0x0003, michael@0: 'red': 0x0004, michael@0: 'magenta': 0x0005, michael@0: 'yellow': 0x0006, michael@0: 'gray': 0x0007 michael@0: } michael@0: BRIGHT_INTENSITY = 0x0008 michael@0: BACKGROUND_SHIFT = 4 michael@0: michael@0: @classmethod michael@0: def set_color(cls, color): michael@0: """ michael@0: color: str - color definition string michael@0: """ michael@0: color_code = 0 michael@0: if color.startswith('bright'): michael@0: color_code |= cls.BRIGHT_INTENSITY michael@0: color = color[len('bright'):] michael@0: color_code |= Terminal.COLOR[color] michael@0: SetConsoleTextAttribute(stdout_handle, color_code) michael@0: michael@0: @classmethod michael@0: def reset_color(cls): michael@0: SetConsoleTextAttribute(stdout_handle, DEFAULT_COLORS) michael@0: michael@0: @classmethod michael@0: def clear_right(cls): michael@0: pass