3
if (not sys.stderr.isatty()) or (not sys.stdout.isatty()):
4
raise ValueError('not a tty')
8
class COORD(Structure):
9
_fields_ = [("X", c_short), ("Y", c_short)]
11
class SMALL_RECT(Structure):
12
_fields_ = [("Left", c_short), ("Top", c_short), ("Right", c_short), ("Bottom", c_short)]
14
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
15
_fields_ = [("Size", COORD), ("CursorPosition", COORD), ("Attributes", c_short), ("Window", SMALL_RECT), ("MaximumWindowSize", COORD)]
17
class CONSOLE_CURSOR_INFO(Structure):
18
_fields_ = [('dwSize',c_ulong), ('bVisible', c_int)]
20
sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
21
csinfo = CONSOLE_CURSOR_INFO()
22
hconsole = windll.kernel32.GetStdHandle(-11)
23
windll.kernel32.GetConsoleScreenBufferInfo(hconsole, byref(sbinfo))
24
if sbinfo.Size.X < 10 or sbinfo.Size.Y < 10: raise Exception('small console')
25
windll.kernel32.GetConsoleCursorInfo(hconsole, byref(csinfo))
31
to_int = lambda number, default: number and int(number) or default
32
wlock = threading.Lock()
34
STD_OUTPUT_HANDLE = -11
35
STD_ERROR_HANDLE = -12
37
class AnsiTerm(object):
39
self.hconsole = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
40
self.cursor_history = []
41
self.orig_sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
42
self.orig_csinfo = CONSOLE_CURSOR_INFO()
43
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(self.orig_sbinfo))
44
windll.kernel32.GetConsoleCursorInfo(hconsole, byref(self.orig_csinfo))
47
def screen_buffer_info(self):
48
sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
49
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(sbinfo))
52
def clear_line(self, param):
53
mode = param and int(param) or 0
54
sbinfo = self.screen_buffer_info()
55
if mode == 1: # Clear from begining of line to cursor position
56
line_start = COORD(0, sbinfo.CursorPosition.Y)
57
line_length = sbinfo.Size.X
58
elif mode == 2: # Clear entire line
59
line_start = COORD(sbinfo.CursorPosition.X, sbinfo.CursorPosition.Y)
60
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
61
else: # Clear from cursor position to end of line
62
line_start = sbinfo.CursorPosition
63
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
64
chars_written = c_int()
65
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), line_length, line_start, byref(chars_written))
66
windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, line_length, line_start, byref(chars_written))
68
def clear_screen(self, param):
69
mode = to_int(param, 0)
70
sbinfo = self.screen_buffer_info()
71
if mode == 1: # Clear from begining of screen to cursor position
72
clear_start = COORD(0, 0)
73
clear_length = sbinfo.CursorPosition.X * sbinfo.CursorPosition.Y
74
elif mode == 2: # Clear entire screen and return cursor to home
75
clear_start = COORD(0, 0)
76
clear_length = sbinfo.Size.X * sbinfo.Size.Y
77
windll.kernel32.SetConsoleCursorPosition(self.hconsole, clear_start)
78
else: # Clear from cursor position to end of screen
79
clear_start = sbinfo.CursorPosition
80
clear_length = ((sbinfo.Size.X - sbinfo.CursorPosition.X) + sbinfo.Size.X * (sbinfo.Size.Y - sbinfo.CursorPosition.Y))
81
chars_written = c_int()
82
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), clear_length, clear_start, byref(chars_written))
83
windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, clear_length, clear_start, byref(chars_written))
85
def push_cursor(self, param):
86
sbinfo = self.screen_buffer_info()
87
self.cursor_history.push(sbinfo.CursorPosition)
89
def pop_cursor(self, param):
90
if self.cursor_history:
91
old_pos = self.cursor_history.pop()
92
windll.kernel32.SetConsoleCursorPosition(self.hconsole, old_pos)
94
def set_cursor(self, param):
95
x, sep, y = param.partition(';')
98
sbinfo = self.screen_buffer_info()
100
min(max(0, x), sbinfo.Size.X),
101
min(max(0, y), sbinfo.Size.Y)
103
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
105
def set_column(self, param):
106
x = to_int(param, 1) - 1
107
sbinfo = self.screen_buffer_info()
109
min(max(0, x), sbinfo.Size.X),
110
sbinfo.CursorPosition.Y
112
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
114
def move_cursor(self, x_offset=0, y_offset=0):
115
sbinfo = self.screen_buffer_info()
117
min(max(0, sbinfo.CursorPosition.X + x_offset), sbinfo.Size.X),
118
min(max(0, sbinfo.CursorPosition.Y + y_offset), sbinfo.Size.Y)
120
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
122
def move_up(self, param):
123
self.move_cursor(y_offset = -to_int(param, 1))
125
def move_down(self, param):
126
self.move_cursor(y_offset = to_int(param, 1))
128
def move_left(self, param):
129
self.move_cursor(x_offset = -to_int(param, 1))
131
def move_right(self, param):
132
self.move_cursor(x_offset = to_int(param, 1))
134
def next_line(self, param):
135
sbinfo = self.screen_buffer_info()
137
x_offset = -sbinfo.CursorPosition.X,
138
y_offset = to_int(param, 1)
141
def prev_line(self, param):
142
sbinfo = self.screen_buffer_info()
144
x_offset = -sbinfo.CursorPosition.X,
145
y_offset = -to_int(param, 1)
148
escape_to_color = { (0, 30): 0x0, #black
151
(0, 33): 0x4+0x2, #dark yellow
153
(0, 35): 0x1+0x4, #purple
154
(0, 36): 0x2+0x4, #cyan
155
(0, 37): 0x1+0x2+0x4, #grey
156
(1, 30): 0x1+0x2+0x4, #dark gray
157
(1, 31): 0x4+0x8, #red
158
(1, 32): 0x2+0x8, #light green
159
(1, 33): 0x4+0x2+0x8, #yellow
160
(1, 34): 0x1+0x8, #light blue
161
(1, 35): 0x1+0x4+0x8, #light purple
162
(1, 36): 0x1+0x2+0x8, #light cyan
163
(1, 37): 0x1+0x2+0x4+0x8, #white
166
def set_color(self, param):
167
cols = param.split(';')
168
attr = self.orig_sbinfo.Attributes
171
if c in range(30,38):
172
attr = (attr & 0xf0) | (self.escape_to_color.get((0,c), 0x7))
173
elif c in range(40,48):
174
attr = (attr & 0x0f) | (self.escape_to_color.get((0,c), 0x7) << 8)
175
elif c in range(90,98):
176
attr = (attr & 0xf0) | (self.escape_to_color.get((1,c-60), 0x7))
177
elif c in range(100,108):
178
attr = (attr & 0x0f) | (self.escape_to_color.get((1,c-60), 0x7) << 8)
181
windll.kernel32.SetConsoleTextAttribute(self.hconsole, attr)
183
def show_cursor(self,param):
185
windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
187
def hide_cursor(self,param):
189
windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
191
ansi_command_table = {
209
# Match either the escape sequence or text not containing escape sequence
210
ansi_tokans = re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
211
def write(self, text):
214
for param, cmd, txt in self.ansi_tokans.findall(text):
216
cmd_func = self.ansi_command_table.get(cmd)
218
cmd_func(self, param)
220
chars_written = c_int()
221
if isinstance(txt, unicode):
222
windll.kernel32.WriteConsoleW(self.hconsole, txt, len(txt), byref(chars_written), None)
224
windll.kernel32.WriteConsoleA(self.hconsole, txt, len(txt), byref(chars_written), None)
234
sys.stderr = sys.stdout = AnsiTerm()
235
os.environ['TERM'] = 'vt100'