6
if(not sys.stderr.isatty())or(not sys.stdout.isatty()):
7
raise ValueError('not a tty')
9
class COORD(Structure):
10
_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)]
13
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
14
_fields_=[("Size",COORD),("CursorPosition",COORD),("Attributes",c_short),("Window",SMALL_RECT),("MaximumWindowSize",COORD)]
15
class CONSOLE_CURSOR_INFO(Structure):
16
_fields_=[('dwSize',c_ulong),('bVisible',c_int)]
17
sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
18
csinfo=CONSOLE_CURSOR_INFO()
19
hconsole=windll.kernel32.GetStdHandle(-11)
20
windll.kernel32.GetConsoleScreenBufferInfo(hconsole,byref(sbinfo))
21
if sbinfo.Size.X<10 or sbinfo.Size.Y<10:raise Exception('small console')
22
windll.kernel32.GetConsoleCursorInfo(hconsole,byref(csinfo))
27
to_int=lambda number,default:number and int(number)or default
28
wlock=threading.Lock()
31
class AnsiTerm(object):
33
self.hconsole=windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
34
self.cursor_history=[]
35
def screen_buffer_info(self):
36
sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
37
windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo))
39
def clear_line(self,param):
40
mode=param and int(param)or 0
41
sbinfo=self.screen_buffer_info()
43
line_start=COORD(0,sbinfo.CursorPosition.Y)
44
line_length=sbinfo.Size.X
46
line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y)
47
line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
49
line_start=sbinfo.CursorPosition
50
line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
52
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),line_length,line_start,byref(chars_written))
53
windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,line_length,line_start,byref(chars_written))
54
def clear_screen(self,param):
56
sbinfo=self.screen_buffer_info()
58
clear_start=COORD(0,0)
59
clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y
61
clear_start=COORD(0,0)
62
clear_length=sbinfo.Size.X*sbinfo.Size.Y
63
windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start)
65
clear_start=sbinfo.CursorPosition
66
clear_length=((sbinfo.Size.X-sbinfo.CursorPosition.X)+sbinfo.Size.X*(sbinfo.Size.Y-sbinfo.CursorPosition.Y))
68
windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),clear_length,clear_start,byref(chars_written))
69
windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,clear_length,clear_start,byref(chars_written))
70
def push_cursor(self,param):
71
sbinfo=self.screen_buffer_info()
72
self.cursor_history.push(sbinfo.CursorPosition)
73
def pop_cursor(self,param):
74
if self.cursor_history:
75
old_pos=self.cursor_history.pop()
76
windll.kernel32.SetConsoleCursorPosition(self.hconsole,old_pos)
77
def set_cursor(self,param):
78
x,sep,y=param.partition(';')
81
sbinfo=self.screen_buffer_info()
82
new_pos=COORD(min(max(0,x),sbinfo.Size.X),min(max(0,y),sbinfo.Size.Y))
83
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
84
def set_column(self,param):
86
sbinfo=self.screen_buffer_info()
87
new_pos=COORD(min(max(0,x),sbinfo.Size.X),sbinfo.CursorPosition.Y)
88
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
89
def move_cursor(self,x_offset=0,y_offset=0):
90
sbinfo=self.screen_buffer_info()
91
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))
92
windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
93
def move_up(self,param):
94
self.move_cursor(y_offset=-to_int(param,1))
95
def move_down(self,param):
96
self.move_cursor(y_offset=to_int(param,1))
97
def move_left(self,param):
98
self.move_cursor(x_offset=-to_int(param,1))
99
def move_right(self,param):
100
self.move_cursor(x_offset=to_int(param,1))
101
def next_line(self,param):
102
sbinfo=self.screen_buffer_info()
103
self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=to_int(param,1))
104
def prev_line(self,param):
105
sbinfo=self.screen_buffer_info()
106
self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=-to_int(param,1))
107
escape_to_color={(0,30):0x0,(0,31):0x4,(0,32):0x2,(0,33):0x4+0x2,(0,34):0x1,(0,35):0x1+0x4,(0,36):0x2+0x4,(0,37):0x1+0x2+0x4,(1,30):0x1+0x2+0x4,(1,31):0x4+0x8,(1,32):0x2+0x8,(1,33):0x4+0x2+0x8,(1,34):0x1+0x8,(1,35):0x1+0x4+0x8,(1,36):0x1+0x2+0x8,(1,37):0x1+0x2+0x4+0x8,}
108
def set_color(self,param):
109
intensity,sep,color=param.partition(';')
110
intensity=to_int(intensity,0)
111
color=to_int(color,0)
112
if intensity and not color:
113
color,intensity=intensity,color
114
attrib=self.escape_to_color.get((intensity,color),0x7)
115
windll.kernel32.SetConsoleTextAttribute(self.hconsole,attrib)
116
def show_cursor(self,param):
118
windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
119
def hide_cursor(self,param):
121
windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
122
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,}
123
ansi_tokans=re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
124
def write(self,text):
126
for param,cmd,txt in self.ansi_tokans.findall(text):
128
cmd_func=self.ansi_command_table.get(cmd)
132
chars_written=c_int()
133
if isinstance(txt,unicode):
134
windll.kernel32.WriteConsoleW(self.hconsole,txt,len(txt),byref(chars_written),None)
136
windll.kernel32.WriteConsoleA(self.hconsole,txt,len(txt),byref(chars_written),None)
142
sys.stderr=sys.stdout=AnsiTerm()
143
os.environ['TERM']='vt100'