3
# WARNING! All changes made to this file will be lost!
7
if not(sys.stderr.isatty()and sys.stdout.isatty()):
8
raise ValueError('not a tty')
10
class COORD(Structure):
11
_fields_=[("X",c_short),("Y",c_short)]
12
class SMALL_RECT(Structure):
13
_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)]
16
class CONSOLE_CURSOR_INFO(Structure):
17
_fields_=[('dwSize',c_ulong),('bVisible',c_int)]
18
sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
19
csinfo=CONSOLE_CURSOR_INFO()
20
hconsole=windll.kernel32.GetStdHandle(-11)
21
windll.kernel32.GetConsoleScreenBufferInfo(hconsole,byref(sbinfo))
22
if sbinfo.Size.X<9 or sbinfo.Size.Y<9:raise ValueError('small console')
23
windll.kernel32.GetConsoleCursorInfo(hconsole,byref(csinfo))
32
to_int=lambda number,default:number and int(number)or default
33
wlock=threading.Lock()
36
class AnsiTerm(object):
38
self.encoding=sys.stdout.encoding
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))
45
def screen_buffer_info(self):
46
sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
47
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo))
49
def clear_line(self,param):
50
mode=param and int(param)or 0
51
sbinfo=self.screen_buffer_info()
53
line_start=COORD(0,sbinfo.CursorPosition.Y)
54
line_length=sbinfo.Size.X
56
line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y)
57
line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
59
line_start=sbinfo.CursorPosition
60
line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
62
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),line_length,line_start,byref(chars_written))
63
windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,line_length,line_start,byref(chars_written))
64
def clear_screen(self,param):
66
sbinfo=self.screen_buffer_info()
68
clear_start=COORD(0,0)
69
clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y
71
clear_start=COORD(0,0)
72
clear_length=sbinfo.Size.X*sbinfo.Size.Y
73
windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start)
75
clear_start=sbinfo.CursorPosition
76
clear_length=((sbinfo.Size.X-sbinfo.CursorPosition.X)+sbinfo.Size.X*(sbinfo.Size.Y-sbinfo.CursorPosition.Y))
78
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),clear_length,clear_start,byref(chars_written))
79
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.append(sbinfo.CursorPosition)
83
def pop_cursor(self,param):
84
if self.cursor_history:
85
old_pos=self.cursor_history.pop()
86
windll.kernel32.SetConsoleCursorPosition(self.hconsole,old_pos)
87
def set_cursor(self,param):
88
y,sep,x=param.partition(';')
91
sbinfo=self.screen_buffer_info()
92
new_pos=COORD(min(max(0,x),sbinfo.Size.X),min(max(0,y),sbinfo.Size.Y))
93
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
94
def set_column(self,param):
96
sbinfo=self.screen_buffer_info()
97
new_pos=COORD(min(max(0,x),sbinfo.Size.X),sbinfo.CursorPosition.Y)
98
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
99
def move_cursor(self,x_offset=0,y_offset=0):
100
sbinfo=self.screen_buffer_info()
101
new_pos=COORD(min(max(0,sbinfo.CursorPosition.X+x_offset),sbinfo.Size.X),min(max(0,sbinfo.CursorPosition.Y+y_offset),sbinfo.Size.Y))
102
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
103
def move_up(self,param):
104
self.move_cursor(y_offset=-to_int(param,1))
105
def move_down(self,param):
106
self.move_cursor(y_offset=to_int(param,1))
107
def move_left(self,param):
108
self.move_cursor(x_offset=-to_int(param,1))
109
def move_right(self,param):
110
self.move_cursor(x_offset=to_int(param,1))
111
def next_line(self,param):
112
sbinfo=self.screen_buffer_info()
113
self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=to_int(param,1))
114
def prev_line(self,param):
115
sbinfo=self.screen_buffer_info()
116
self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=-to_int(param,1))
118
return((c&1)<<2)|(c&2)|((c&4)>>2)
119
def set_color(self,param):
120
cols=param.split(';')
121
sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
122
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo))
123
attr=sbinfo.Attributes
127
if c in range(30,38):
128
attr=(attr&0xfff0)|self.rgb2bgr(c-30)
129
elif c in range(40,48):
130
attr=(attr&0xff0f)|(self.rgb2bgr(c-40)<<4)
132
attr=self.orig_sbinfo.Attributes
138
attr=(attr&0xff88)|((attr&0x70)>>4)|((attr&0x07)<<4)
139
windll.kernel32.SetConsoleTextAttribute(self.hconsole,attr)
140
def show_cursor(self,param):
142
windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
143
def hide_cursor(self,param):
145
windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
146
ansi_command_table={'A':move_up,'B':move_down,'C':move_right,'D':move_left,'E':next_line,'F':prev_line,'G':set_column,'H':set_cursor,'f':set_cursor,'J':clear_screen,'K':clear_line,'h':show_cursor,'l':hide_cursor,'m':set_color,'s':push_cursor,'u':pop_cursor,}
147
ansi_tokens=re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
148
def write(self,text):
151
for param,cmd,txt in self.ansi_tokens.findall(text):
153
cmd_func=self.ansi_command_table.get(cmd)
157
self.writeconsole(txt)
160
def writeconsole(self,txt):
161
chars_written=c_int()
162
writeconsole=windll.kernel32.WriteConsoleA
163
if isinstance(txt,_type):
164
writeconsole=windll.kernel32.WriteConsoleW
166
for x in range(0,len(txt),TINY_STEP):
167
tiny=txt[x:x+TINY_STEP]
168
writeconsole(self.hconsole,tiny,len(tiny),byref(chars_written),None)
173
sys.stderr=sys.stdout=AnsiTerm()
174
os.environ['TERM']='vt100'