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 = []
42
def screen_buffer_info(self):
43
sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
44
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(sbinfo))
47
def clear_line(self, param):
48
mode = param and int(param) or 0
49
sbinfo = self.screen_buffer_info()
50
if mode == 1: # Clear from begining of line to cursor position
51
line_start = COORD(0, sbinfo.CursorPosition.Y)
52
line_length = sbinfo.Size.X
53
elif mode == 2: # Clear entire line
54
line_start = COORD(sbinfo.CursorPosition.X, sbinfo.CursorPosition.Y)
55
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
56
else: # Clear from cursor position to end of line
57
line_start = sbinfo.CursorPosition
58
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
59
chars_written = c_int()
60
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), line_length, line_start, byref(chars_written))
61
windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, line_length, line_start, byref(chars_written))
63
def clear_screen(self, param):
64
mode = to_int(param, 0)
65
sbinfo = self.screen_buffer_info()
66
if mode == 1: # Clear from begining of screen to cursor position
67
clear_start = COORD(0, 0)
68
clear_length = sbinfo.CursorPosition.X * sbinfo.CursorPosition.Y
69
elif mode == 2: # Clear entire screen and return cursor to home
70
clear_start = COORD(0, 0)
71
clear_length = sbinfo.Size.X * sbinfo.Size.Y
72
windll.kernel32.SetConsoleCursorPosition(self.hconsole, clear_start)
73
else: # Clear from cursor position to end of screen
74
clear_start = sbinfo.CursorPosition
75
clear_length = ((sbinfo.Size.X - sbinfo.CursorPosition.X) + sbinfo.Size.X * (sbinfo.Size.Y - sbinfo.CursorPosition.Y))
76
chars_written = c_int()
77
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), clear_length, clear_start, byref(chars_written))
78
windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, clear_length, clear_start, byref(chars_written))
80
def push_cursor(self, param):
81
sbinfo = self.screen_buffer_info()
82
self.cursor_history.push(sbinfo.CursorPosition)
84
def pop_cursor(self, param):
85
if self.cursor_history:
86
old_pos = self.cursor_history.pop()
87
windll.kernel32.SetConsoleCursorPosition(self.hconsole, old_pos)
89
def set_cursor(self, param):
90
x, sep, y = param.partition(';')
93
sbinfo = self.screen_buffer_info()
95
min(max(0, x), sbinfo.Size.X),
96
min(max(0, y), sbinfo.Size.Y)
98
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
100
def set_column(self, param):
101
x = to_int(param, 1) - 1
102
sbinfo = self.screen_buffer_info()
104
min(max(0, x), sbinfo.Size.X),
105
sbinfo.CursorPosition.Y
107
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
109
def move_cursor(self, x_offset=0, y_offset=0):
110
sbinfo = self.screen_buffer_info()
112
min(max(0, sbinfo.CursorPosition.X + x_offset), sbinfo.Size.X),
113
min(max(0, sbinfo.CursorPosition.Y + y_offset), sbinfo.Size.Y)
115
windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
117
def move_up(self, param):
118
self.move_cursor(y_offset = -to_int(param, 1))
120
def move_down(self, param):
121
self.move_cursor(y_offset = to_int(param, 1))
123
def move_left(self, param):
124
self.move_cursor(x_offset = -to_int(param, 1))
126
def move_right(self, param):
127
self.move_cursor(x_offset = to_int(param, 1))
129
def next_line(self, param):
130
sbinfo = self.screen_buffer_info()
132
x_offset = -sbinfo.CursorPosition.X,
133
y_offset = to_int(param, 1)
136
def prev_line(self, param):
137
sbinfo = self.screen_buffer_info()
139
x_offset = -sbinfo.CursorPosition.X,
140
y_offset = -to_int(param, 1)
143
escape_to_color = { (0, 30): 0x0, #black
146
(0, 33): 0x4+0x2, #dark yellow
148
(0, 35): 0x1+0x4, #purple
149
(0, 36): 0x2+0x4, #cyan
150
(0, 37): 0x1+0x2+0x4, #grey
151
(1, 30): 0x1+0x2+0x4, #dark gray
152
(1, 31): 0x4+0x8, #red
153
(1, 32): 0x2+0x8, #light green
154
(1, 33): 0x4+0x2+0x8, #yellow
155
(1, 34): 0x1+0x8, #light blue
156
(1, 35): 0x1+0x4+0x8, #light purple
157
(1, 36): 0x1+0x2+0x8, #light cyan
158
(1, 37): 0x1+0x2+0x4+0x8, #white
161
def set_color(self, param):
162
intensity, sep, color = param.partition(';')
163
intensity = to_int(intensity, 0)
164
color = to_int(color, 0)
165
if intensity and not color:
166
color, intensity = intensity, color
167
attrib = self.escape_to_color.get((intensity, color), 0x7)
168
windll.kernel32.SetConsoleTextAttribute(self.hconsole, attrib)
170
def show_cursor(self,param):
172
windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
174
def hide_cursor(self,param):
176
windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
178
ansi_command_table = {
196
# Match either the escape sequence or text not containing escape sequence
197
ansi_tokans = re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
198
def write(self, text):
200
for param, cmd, txt in self.ansi_tokans.findall(text):
202
cmd_func = self.ansi_command_table.get(cmd)
204
cmd_func(self, param)
206
chars_written = c_int()
207
if isinstance(txt, unicode):
208
windll.kernel32.WriteConsoleW(self.hconsole, txt, len(txt), byref(chars_written), None)
210
windll.kernel32.WriteConsoleA(self.hconsole, txt, len(txt), byref(chars_written), None)
219
sys.stderr = sys.stdout = AnsiTerm()
220
os.environ['TERM'] = 'vt100'