1#! /usr/bin/env python 2# encoding: utf-8 3# WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file 4 5import os,re,sys 6from waflib import Utils 7wlock=Utils.threading.Lock() 8try: 9 from ctypes import Structure,windll,c_short,c_ushort,c_ulong,c_int,byref,c_wchar,POINTER,c_long 10except ImportError: 11 class AnsiTerm(object): 12 def __init__(self,stream): 13 self.stream=stream 14 try: 15 self.errors=self.stream.errors 16 except AttributeError: 17 pass 18 self.encoding=self.stream.encoding 19 def write(self,txt): 20 try: 21 wlock.acquire() 22 self.stream.write(txt) 23 self.stream.flush() 24 finally: 25 wlock.release() 26 def fileno(self): 27 return self.stream.fileno() 28 def flush(self): 29 self.stream.flush() 30 def isatty(self): 31 return self.stream.isatty() 32else: 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)] 41 try: 42 _type=unicode 43 except NameError: 44 _type=str 45 to_int=lambda number,default:number and int(number)or default 46 STD_OUTPUT_HANDLE=-11 47 STD_ERROR_HANDLE=-12 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): 63 def __init__(self,s): 64 self.stream=s 65 try: 66 self.errors=s.errors 67 except AttributeError: 68 pass 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)) 78 self._isatty=r==1 79 def screen_buffer_info(self): 80 windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(self._sbinfo)) 81 return self._sbinfo 82 def clear_line(self,param): 83 mode=param and int(param)or 0 84 sbinfo=self.screen_buffer_info() 85 if mode==1: 86 line_start=COORD(0,sbinfo.CursorPosition.Y) 87 line_length=sbinfo.Size.X 88 elif mode==2: 89 line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y) 90 line_length=sbinfo.Size.X-sbinfo.CursorPosition.X 91 else: 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): 98 mode=to_int(param,0) 99 sbinfo=self.screen_buffer_info() 100 if mode==1: 101 clear_start=COORD(0,0) 102 clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y 103 elif mode==2: 104 clear_start=COORD(0,0) 105 clear_length=sbinfo.Size.X*sbinfo.Size.Y 106 windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start) 107 else: 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(';') 122 x=to_int(x,1)-1 123 y=to_int(y,1)-1 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): 128 x=to_int(param,1)-1 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)) 150 def rgb2bgr(self,c): 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 156 for c in cols: 157 c=to_int(c,0) 158 if 29<c<38: 159 attr=(attr&0xfff0)|self.rgb2bgr(c-30) 160 elif 39<c<48: 161 attr=(attr&0xff0f)|(self.rgb2bgr(c-40)<<4) 162 elif c==0: 163 attr=self._orig_sbinfo.Attributes 164 elif c==1: 165 attr|=0x08 166 elif c==4: 167 attr|=0x80 168 elif c==7: 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): 180 try: 181 wlock.acquire() 182 if self._isatty: 183 for param,cmd,txt in self.ansi_tokens.findall(text): 184 if cmd: 185 cmd_func=self.ansi_command_table.get(cmd) 186 if cmd_func: 187 cmd_func(self,param) 188 else: 189 self.writeconsole(txt) 190 else: 191 self.stream.write(text) 192 finally: 193 wlock.release() 194 def writeconsole(self,txt): 195 chars_written=c_ulong() 196 writeconsole=windll.kernel32.WriteConsoleA 197 if isinstance(txt,_type): 198 writeconsole=windll.kernel32.WriteConsoleW 199 done=0 200 todo=len(txt) 201 chunk=32<<10 202 while todo!=0: 203 doing=min(chunk,todo) 204 buf=txt[done:done+doing] 205 r=writeconsole(self.hconsole,buf,doing,byref(chars_written),None) 206 if r==0: 207 chunk>>=1 208 continue 209 done+=doing 210 todo-=doing 211 def fileno(self): 212 return self.stream.fileno() 213 def flush(self): 214 pass 215 def isatty(self): 216 return self._isatty 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() 221 def get_term_cols(): 222 windll.kernel32.GetConsoleScreenBufferInfo(console,byref(sbinfo)) 223 return sbinfo.Size.X-1 224try: 225 import struct,fcntl,termios 226except ImportError: 227 pass 228else: 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() 231 def fun(): 232 return struct.unpack("HHHH",fcntl.ioctl(FD,termios.TIOCGWINSZ,struct.pack("HHHH",0,0,0,0)))[1] 233 try: 234 fun() 235 except Exception as e: 236 pass 237 else: 238 get_term_cols=fun 239