3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
6 from waflib.Utils import threading
9 from ctypes import Structure,windll,c_short,c_ushort,c_ulong,c_int,byref,c_wchar,POINTER,c_long
11 class AnsiTerm(object):
12 def __init__(self,stream):
15 self.errors=self.stream.errors
16 except AttributeError:
18 self.encoding=self.stream.encoding
22 self.stream.write(txt)
27 return self.stream.fileno()
31 return self.stream.isatty()
33 class COORD(Structure):
34 _fields_=[("X",c_short),("Y",c_short)]
35 class SMALL_RECT(Structure):
36 _fields_=[("Left",c_short),("Top",c_short),("Right",c_short),("Bottom",c_short)]
37 class CONSOLE_SCREEN_BUFFER_INFO(Structure):
38 _fields_=[("Size",COORD),("CursorPosition",COORD),("Attributes",c_ushort),("Window",SMALL_RECT),("MaximumWindowSize",COORD)]
39 class CONSOLE_CURSOR_INFO(Structure):
40 _fields_=[('dwSize',c_ulong),('bVisible',c_int)]
45 to_int=lambda number,default:number and int(number)or default
48 windll.kernel32.GetStdHandle.argtypes=[c_ulong]
49 windll.kernel32.GetStdHandle.restype=c_ulong
50 windll.kernel32.GetConsoleScreenBufferInfo.argtypes=[c_ulong,POINTER(CONSOLE_SCREEN_BUFFER_INFO)]
51 windll.kernel32.GetConsoleScreenBufferInfo.restype=c_long
52 windll.kernel32.SetConsoleTextAttribute.argtypes=[c_ulong,c_ushort]
53 windll.kernel32.SetConsoleTextAttribute.restype=c_long
54 windll.kernel32.FillConsoleOutputCharacterW.argtypes=[c_ulong,c_wchar,c_ulong,POINTER(COORD),POINTER(c_ulong)]
55 windll.kernel32.FillConsoleOutputCharacterW.restype=c_long
56 windll.kernel32.FillConsoleOutputAttribute.argtypes=[c_ulong,c_ushort,c_ulong,POINTER(COORD),POINTER(c_ulong)]
57 windll.kernel32.FillConsoleOutputAttribute.restype=c_long
58 windll.kernel32.SetConsoleCursorPosition.argtypes=[c_ulong,POINTER(COORD)]
59 windll.kernel32.SetConsoleCursorPosition.restype=c_long
60 windll.kernel32.SetConsoleCursorInfo.argtypes=[c_ulong,POINTER(CONSOLE_CURSOR_INFO)]
61 windll.kernel32.SetConsoleCursorInfo.restype=c_long
62 class AnsiTerm(object):
67 except AttributeError:
69 self.encoding=s.encoding
70 self.cursor_history=[]
71 handle=(s.fileno()==2)and STD_ERROR_HANDLE or STD_OUTPUT_HANDLE
72 self.hconsole=windll.kernel32.GetStdHandle(handle)
73 self._sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
74 self._csinfo=CONSOLE_CURSOR_INFO()
75 windll.kernel32.GetConsoleCursorInfo(self.hconsole,byref(self._csinfo))
76 self._orig_sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
77 r=windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(self._orig_sbinfo))
79 def screen_buffer_info(self):
80 windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(self._sbinfo))
82 def clear_line(self,param):
83 mode=param and int(param)or 0
84 sbinfo=self.screen_buffer_info()
86 line_start=COORD(0,sbinfo.CursorPosition.Y)
87 line_length=sbinfo.Size.X
89 line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y)
90 line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
92 line_start=sbinfo.CursorPosition
93 line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
94 chars_written=c_ulong()
95 windll.kernel32.FillConsoleOutputCharacterW(self.hconsole,c_wchar(' '),line_length,line_start,byref(chars_written))
96 windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,line_length,line_start,byref(chars_written))
97 def clear_screen(self,param):
99 sbinfo=self.screen_buffer_info()
101 clear_start=COORD(0,0)
102 clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y
104 clear_start=COORD(0,0)
105 clear_length=sbinfo.Size.X*sbinfo.Size.Y
106 windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start)
108 clear_start=sbinfo.CursorPosition
109 clear_length=((sbinfo.Size.X-sbinfo.CursorPosition.X)+sbinfo.Size.X*(sbinfo.Size.Y-sbinfo.CursorPosition.Y))
110 chars_written=c_ulong()
111 windll.kernel32.FillConsoleOutputCharacterW(self.hconsole,c_wchar(' '),clear_length,clear_start,byref(chars_written))
112 windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,clear_length,clear_start,byref(chars_written))
113 def push_cursor(self,param):
114 sbinfo=self.screen_buffer_info()
115 self.cursor_history.append(sbinfo.CursorPosition)
116 def pop_cursor(self,param):
117 if self.cursor_history:
118 old_pos=self.cursor_history.pop()
119 windll.kernel32.SetConsoleCursorPosition(self.hconsole,old_pos)
120 def set_cursor(self,param):
121 y,sep,x=param.partition(';')
124 sbinfo=self.screen_buffer_info()
125 new_pos=COORD(min(max(0,x),sbinfo.Size.X),min(max(0,y),sbinfo.Size.Y))
126 windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
127 def set_column(self,param):
129 sbinfo=self.screen_buffer_info()
130 new_pos=COORD(min(max(0,x),sbinfo.Size.X),sbinfo.CursorPosition.Y)
131 windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
132 def move_cursor(self,x_offset=0,y_offset=0):
133 sbinfo=self.screen_buffer_info()
134 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))
135 windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
136 def move_up(self,param):
137 self.move_cursor(y_offset=-to_int(param,1))
138 def move_down(self,param):
139 self.move_cursor(y_offset=to_int(param,1))
140 def move_left(self,param):
141 self.move_cursor(x_offset=-to_int(param,1))
142 def move_right(self,param):
143 self.move_cursor(x_offset=to_int(param,1))
144 def next_line(self,param):
145 sbinfo=self.screen_buffer_info()
146 self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=to_int(param,1))
147 def prev_line(self,param):
148 sbinfo=self.screen_buffer_info()
149 self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=-to_int(param,1))
151 return((c&1)<<2)|(c&2)|((c&4)>>2)
152 def set_color(self,param):
153 cols=param.split(';')
154 sbinfo=self.screen_buffer_info()
155 attr=sbinfo.Attributes
159 attr=(attr&0xfff0)|self.rgb2bgr(c-30)
161 attr=(attr&0xff0f)|(self.rgb2bgr(c-40)<<4)
163 attr=self._orig_sbinfo.Attributes
169 attr=(attr&0xff88)|((attr&0x70)>>4)|((attr&0x07)<<4)
170 windll.kernel32.SetConsoleTextAttribute(self.hconsole,attr)
171 def show_cursor(self,param):
172 self._csinfo.bVisible=1
173 windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(self._csinfo))
174 def hide_cursor(self,param):
175 self._csinfo.bVisible=0
176 windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(self._csinfo))
177 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,}
178 ansi_tokens=re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
179 def write(self,text):
183 for param,cmd,txt in self.ansi_tokens.findall(text):
185 cmd_func=self.ansi_command_table.get(cmd)
189 self.writeconsole(txt)
191 self.stream.write(text)
194 def writeconsole(self,txt):
195 chars_written=c_int()
196 writeconsole=windll.kernel32.WriteConsoleA
197 if isinstance(txt,_type):
198 writeconsole=windll.kernel32.WriteConsoleW
203 doing=min(chunk,todo)
204 buf=txt[done:done+doing]
205 r=writeconsole(self.hconsole,buf,doing,byref(chars_written),None)
212 return self.stream.fileno()
217 if sys.stdout.isatty()or sys.stderr.isatty():
218 handle=sys.stdout.isatty()and STD_OUTPUT_HANDLE or STD_ERROR_HANDLE
219 console=windll.kernel32.GetStdHandle(handle)
220 sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
222 windll.kernel32.GetConsoleScreenBufferInfo(console,byref(sbinfo))
223 return sbinfo.Size.X-1
225 import struct,fcntl,termios
229 if(sys.stdout.isatty()or sys.stderr.isatty())and os.environ.get('TERM','')not in('dumb','emacs'):
230 FD=sys.stdout.isatty()and sys.stdout.fileno()or sys.stderr.fileno()
232 return struct.unpack("HHHH",fcntl.ioctl(FD,termios.TIOCGWINSZ,struct.pack("HHHH",0,0,0,0)))[1]