Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 """
2 From Andre Burgaud's Blog, from the CTypes Wiki:
3 http://www.burgaud.com/bring-colors-to-the-windows-console-with-python/
5 Colors text in console mode application (win32).
6 Uses ctypes and Win32 methods SetConsoleTextAttribute and
7 GetConsoleScreenBufferInfo.
9 $Id: color_console.py 534 2009-05-10 04:00:59Z andre $
10 """
12 from ctypes import windll, Structure, c_short, c_ushort, byref
14 SHORT = c_short
15 WORD = c_ushort
17 class COORD(Structure):
18 """struct in wincon.h."""
19 _fields_ = [
20 ("X", SHORT),
21 ("Y", SHORT)]
23 class SMALL_RECT(Structure):
24 """struct in wincon.h."""
25 _fields_ = [
26 ("Left", SHORT),
27 ("Top", SHORT),
28 ("Right", SHORT),
29 ("Bottom", SHORT)]
31 class CONSOLE_SCREEN_BUFFER_INFO(Structure):
32 """struct in wincon.h."""
33 _fields_ = [
34 ("dwSize", COORD),
35 ("dwCursorPosition", COORD),
36 ("wAttributes", WORD),
37 ("srWindow", SMALL_RECT),
38 ("dwMaximumWindowSize", COORD)]
40 # winbase.h
41 STD_INPUT_HANDLE = -10
42 STD_OUTPUT_HANDLE = -11
43 STD_ERROR_HANDLE = -12
45 # wincon.h
46 FOREGROUND_BLACK = 0x0000
47 FOREGROUND_BLUE = 0x0001
48 FOREGROUND_GREEN = 0x0002
49 FOREGROUND_CYAN = 0x0003
50 FOREGROUND_RED = 0x0004
51 FOREGROUND_MAGENTA = 0x0005
52 FOREGROUND_YELLOW = 0x0006
53 FOREGROUND_GREY = 0x0007
54 FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified.
56 BACKGROUND_BLACK = 0x0000
57 BACKGROUND_BLUE = 0x0010
58 BACKGROUND_GREEN = 0x0020
59 BACKGROUND_CYAN = 0x0030
60 BACKGROUND_RED = 0x0040
61 BACKGROUND_MAGENTA = 0x0050
62 BACKGROUND_YELLOW = 0x0060
63 BACKGROUND_GREY = 0x0070
64 BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
66 stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
67 SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
68 GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
70 def get_text_attr():
71 csbi = CONSOLE_SCREEN_BUFFER_INFO()
72 GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
73 return csbi.wAttributes
75 DEFAULT_COLORS = get_text_attr()
77 class Terminal(object):
78 COLOR = {
79 'black': 0x0000,
80 'blue': 0x0001,
81 'green': 0x0002,
82 'cyan': 0x0003,
83 'red': 0x0004,
84 'magenta': 0x0005,
85 'yellow': 0x0006,
86 'gray': 0x0007
87 }
88 BRIGHT_INTENSITY = 0x0008
89 BACKGROUND_SHIFT = 4
91 @classmethod
92 def set_color(cls, color):
93 """
94 color: str - color definition string
95 """
96 color_code = 0
97 if color.startswith('bright'):
98 color_code |= cls.BRIGHT_INTENSITY
99 color = color[len('bright'):]
100 color_code |= Terminal.COLOR[color]
101 SetConsoleTextAttribute(stdout_handle, color_code)
103 @classmethod
104 def reset_color(cls):
105 SetConsoleTextAttribute(stdout_handle, DEFAULT_COLORS)
107 @classmethod
108 def clear_right(cls):
109 pass