| OLD | NEW |
| (Empty) |
| 1 | |
| 2 from . import win32 | |
| 3 | |
| 4 | |
| 5 # from wincon.h | |
| 6 class WinColor(object): | |
| 7 BLACK = 0 | |
| 8 BLUE = 1 | |
| 9 GREEN = 2 | |
| 10 CYAN = 3 | |
| 11 RED = 4 | |
| 12 MAGENTA = 5 | |
| 13 YELLOW = 6 | |
| 14 GREY = 7 | |
| 15 | |
| 16 # from wincon.h | |
| 17 class WinStyle(object): | |
| 18 NORMAL = 0x00 # dim text, dim background | |
| 19 BRIGHT = 0x08 # bright text, dim background | |
| 20 | |
| 21 | |
| 22 class WinTerm(object): | |
| 23 | |
| 24 def __init__(self): | |
| 25 self._default = win32.GetConsoleScreenBufferInfo(win32.STDOUT).wAttribut
es | |
| 26 self.set_attrs(self._default) | |
| 27 self._default_fore = self._fore | |
| 28 self._default_back = self._back | |
| 29 self._default_style = self._style | |
| 30 | |
| 31 def get_attrs(self): | |
| 32 return self._fore + self._back * 16 + self._style | |
| 33 | |
| 34 def set_attrs(self, value): | |
| 35 self._fore = value & 7 | |
| 36 self._back = (value >> 4) & 7 | |
| 37 self._style = value & WinStyle.BRIGHT | |
| 38 | |
| 39 def reset_all(self, on_stderr=None): | |
| 40 self.set_attrs(self._default) | |
| 41 self.set_console(attrs=self._default) | |
| 42 | |
| 43 def fore(self, fore=None, on_stderr=False): | |
| 44 if fore is None: | |
| 45 fore = self._default_fore | |
| 46 self._fore = fore | |
| 47 self.set_console(on_stderr=on_stderr) | |
| 48 | |
| 49 def back(self, back=None, on_stderr=False): | |
| 50 if back is None: | |
| 51 back = self._default_back | |
| 52 self._back = back | |
| 53 self.set_console(on_stderr=on_stderr) | |
| 54 | |
| 55 def style(self, style=None, on_stderr=False): | |
| 56 if style is None: | |
| 57 style = self._default_style | |
| 58 self._style = style | |
| 59 self.set_console(on_stderr=on_stderr) | |
| 60 | |
| 61 def set_console(self, attrs=None, on_stderr=False): | |
| 62 if attrs is None: | |
| 63 attrs = self.get_attrs() | |
| 64 handle = win32.STDOUT | |
| 65 if on_stderr: | |
| 66 handle = win32.STDERR | |
| 67 win32.SetConsoleTextAttribute(handle, attrs) | |
| 68 | |
| 69 def get_position(self, handle): | |
| 70 position = win32.GetConsoleScreenBufferInfo(handle).dwCursorPosition | |
| 71 # Because Windows coordinates are 0-based, | |
| 72 # and win32.SetConsoleCursorPosition expects 1-based. | |
| 73 position.X += 1 | |
| 74 position.Y += 1 | |
| 75 return position | |
| 76 | |
| 77 def set_cursor_position(self, position=None, on_stderr=False): | |
| 78 if position is None: | |
| 79 #I'm not currently tracking the position, so there is no default. | |
| 80 #position = self.get_position() | |
| 81 return | |
| 82 handle = win32.STDOUT | |
| 83 if on_stderr: | |
| 84 handle = win32.STDERR | |
| 85 win32.SetConsoleCursorPosition(handle, position) | |
| 86 | |
| 87 def cursor_up(self, num_rows=0, on_stderr=False): | |
| 88 if num_rows == 0: | |
| 89 return | |
| 90 handle = win32.STDOUT | |
| 91 if on_stderr: | |
| 92 handle = win32.STDERR | |
| 93 position = self.get_position(handle) | |
| 94 adjusted_position = (position.Y - num_rows, position.X) | |
| 95 self.set_cursor_position(adjusted_position, on_stderr) | |
| 96 | |
| 97 def erase_data(self, mode=0, on_stderr=False): | |
| 98 # 0 (or None) should clear from the cursor to the end of the screen. | |
| 99 # 1 should clear from the cursor to the beginning of the screen. | |
| 100 # 2 should clear the entire screen. (And maybe move cursor to (1,1)?) | |
| 101 # | |
| 102 # At the moment, I only support mode 2. From looking at the API, it | |
| 103 # should be possible to calculate a different number of bytes to clea
r, | |
| 104 # and to do so relative to the cursor position. | |
| 105 if mode[0] not in (2,): | |
| 106 return | |
| 107 handle = win32.STDOUT | |
| 108 if on_stderr: | |
| 109 handle = win32.STDERR | |
| 110 # here's where we'll home the cursor | |
| 111 coord_screen = win32.COORD(0,0) | |
| 112 csbi = win32.GetConsoleScreenBufferInfo(handle) | |
| 113 # get the number of character cells in the current buffer | |
| 114 dw_con_size = csbi.dwSize.X * csbi.dwSize.Y | |
| 115 # fill the entire screen with blanks | |
| 116 win32.FillConsoleOutputCharacter(handle, ord(' '), dw_con_size, coord_sc
reen) | |
| 117 # now set the buffer's attributes accordingly | |
| 118 win32.FillConsoleOutputAttribute(handle, self.get_attrs(), dw_con_size,
coord_screen ); | |
| 119 # put the cursor at (0, 0) | |
| 120 win32.SetConsoleCursorPosition(handle, (coord_screen.X, coord_screen.Y)) | |
| OLD | NEW |