2012-09-20 21:55:37 +00:00
|
|
|
"""
|
|
|
|
Colors text in console mode application (win32).
|
|
|
|
Uses ctypes and Win32 methods SetConsoleTextAttribute and
|
|
|
|
GetConsoleScreenBufferInfo.
|
|
|
|
|
|
|
|
$Id: color_console.py 534 2009-05-10 04:00:59Z andre $
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ctypes import windll, Structure, c_short as SHORT, c_ushort as WORD, byref
|
|
|
|
|
|
|
|
class COORD(Structure):
|
|
|
|
"""struct in wincon.h."""
|
2012-09-24 17:11:20 +00:00
|
|
|
_fields_ = [("X", SHORT),("Y", SHORT)]
|
2012-09-20 21:55:37 +00:00
|
|
|
|
|
|
|
class SMALL_RECT(Structure):
|
|
|
|
"""struct in wincon.h."""
|
2012-09-24 17:11:20 +00:00
|
|
|
_fields_ = [("Left", SHORT),("Top", SHORT),("Right", SHORT),("Bottom", SHORT)]
|
2012-09-20 21:55:37 +00:00
|
|
|
|
|
|
|
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
|
|
|
|
"""struct in wincon.h."""
|
2012-09-24 17:11:20 +00:00
|
|
|
_fields_ = [("dwSize", COORD),("dwCursorPosition", COORD),("wAttributes", WORD),("srWindow", SMALL_RECT),("dwMaximumWindowSize", COORD)]
|
2012-09-20 21:55:37 +00:00
|
|
|
|
|
|
|
# winbase.h
|
2012-09-24 17:11:20 +00:00
|
|
|
STD_INPUT_HANDLE = -10
|
2012-09-20 21:55:37 +00:00
|
|
|
STD_OUTPUT_HANDLE = -11
|
2012-09-24 17:11:20 +00:00
|
|
|
STD_ERROR_HANDLE = -12
|
2012-09-20 21:55:37 +00:00
|
|
|
|
|
|
|
stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
|
|
|
|
SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
|
|
|
|
GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
|
|
|
|
|
|
|
|
def get_text_attr():
|
|
|
|
"""Returns the character attributes (colors) of the console screen
|
|
|
|
buffer."""
|
|
|
|
csbi = CONSOLE_SCREEN_BUFFER_INFO()
|
|
|
|
GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
|
|
|
|
return csbi.wAttributes
|
|
|
|
|
|
|
|
def set_text_attr(color):
|
|
|
|
"""Sets the character attributes (colors) of the console screen
|
|
|
|
buffer. Color is a combination of foreground and background color,
|
|
|
|
foreground and background intensity."""
|
|
|
|
SetConsoleTextAttribute(stdout_handle, color)
|