1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
6 class WinColor(object):
17 class WinStyle(object):
18 NORMAL = 0x00 # dim text, dim background
19 BRIGHT = 0x08 # bright text, dim background
20 BRIGHT_BACKGROUND = 0x80 # dim text, bright background
22 class WinTerm(object):
25 self._default = win32.GetConsoleScreenBufferInfo(win32.STDOUT).wAttributes
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 # In order to emulate LIGHT_EX in windows, we borrow the BRIGHT style.
31 # So that LIGHT_EX colors and BRIGHT style do not clobber each other,
32 # we track them separately, since LIGHT_EX is overwritten by Fore/Back
33 # and BRIGHT is overwritten by Style codes.
37 return self._fore + self._back * 16 + (self._style | self._light)
39 def set_attrs(self, value):
40 self._fore = value & 7
41 self._back = (value >> 4) & 7
42 self._style = value & (WinStyle.BRIGHT | WinStyle.BRIGHT_BACKGROUND)
44 def reset_all(self, on_stderr=None):
45 self.set_attrs(self._default)
46 self.set_console(attrs=self._default)
48 def fore(self, fore=None, light=False, on_stderr=False):
50 fore = self._default_fore
52 # Emulate LIGHT_EX with BRIGHT Style
54 self._light |= WinStyle.BRIGHT
56 self._light &= ~WinStyle.BRIGHT
57 self.set_console(on_stderr=on_stderr)
59 def back(self, back=None, light=False, on_stderr=False):
61 back = self._default_back
63 # Emulate LIGHT_EX with BRIGHT_BACKGROUND Style
65 self._light |= WinStyle.BRIGHT_BACKGROUND
67 self._light &= ~WinStyle.BRIGHT_BACKGROUND
68 self.set_console(on_stderr=on_stderr)
70 def style(self, style=None, on_stderr=False):
72 style = self._default_style
74 self.set_console(on_stderr=on_stderr)
76 def set_console(self, attrs=None, on_stderr=False):
78 attrs = self.get_attrs()
82 win32.SetConsoleTextAttribute(handle, attrs)
84 def get_position(self, handle):
85 position = win32.GetConsoleScreenBufferInfo(handle).dwCursorPosition
86 # Because Windows coordinates are 0-based,
87 # and win32.SetConsoleCursorPosition expects 1-based.
92 def set_cursor_position(self, position=None, on_stderr=False):
94 # I'm not currently tracking the position, so there is no default.
95 # position = self.get_position()
100 win32.SetConsoleCursorPosition(handle, position)
102 def cursor_adjust(self, x, y, on_stderr=False):
103 handle = win32.STDOUT
105 handle = win32.STDERR
106 position = self.get_position(handle)
107 adjusted_position = (position.Y + y, position.X + x)
108 win32.SetConsoleCursorPosition(handle, adjusted_position, adjust=False)
110 def erase_screen(self, mode=0, on_stderr=False):
111 # 0 should clear from the cursor to the end of the screen.
112 # 1 should clear from the cursor to the beginning of the screen.
113 # 2 should clear the entire screen, and move cursor to (1,1)
114 handle = win32.STDOUT
116 handle = win32.STDERR
117 csbi = win32.GetConsoleScreenBufferInfo(handle)
118 # get the number of character cells in the current buffer
119 cells_in_screen = csbi.dwSize.X * csbi.dwSize.Y
120 # get number of character cells before current cursor position
121 cells_before_cursor = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X
123 from_coord = csbi.dwCursorPosition
124 cells_to_erase = cells_in_screen - cells_before_cursor
126 from_coord = win32.COORD(0, 0)
127 cells_to_erase = cells_before_cursor
129 from_coord = win32.COORD(0, 0)
130 cells_to_erase = cells_in_screen
131 # fill the entire screen with blanks
132 win32.FillConsoleOutputCharacter(handle, ' ', cells_to_erase, from_coord)
133 # now set the buffer's attributes accordingly
134 win32.FillConsoleOutputAttribute(handle, self.get_attrs(), cells_to_erase, from_coord)
136 # put the cursor where needed
137 win32.SetConsoleCursorPosition(handle, (1, 1))
139 def erase_line(self, mode=0, on_stderr=False):
140 # 0 should clear from the cursor to the end of the line.
141 # 1 should clear from the cursor to the beginning of the line.
142 # 2 should clear the entire line.
143 handle = win32.STDOUT
145 handle = win32.STDERR
146 csbi = win32.GetConsoleScreenBufferInfo(handle)
148 from_coord = csbi.dwCursorPosition
149 cells_to_erase = csbi.dwSize.X - csbi.dwCursorPosition.X
151 from_coord = win32.COORD(0, csbi.dwCursorPosition.Y)
152 cells_to_erase = csbi.dwCursorPosition.X
154 from_coord = win32.COORD(0, csbi.dwCursorPosition.Y)
155 cells_to_erase = csbi.dwSize.X
156 # fill the entire screen with blanks
157 win32.FillConsoleOutputCharacter(handle, ' ', cells_to_erase, from_coord)
158 # now set the buffer's attributes accordingly
159 win32.FillConsoleOutputAttribute(handle, self.get_attrs(), cells_to_erase, from_coord)
161 def set_title(self, title):
162 win32.SetConsoleTitle(title)