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