[0fa325b] | 1 | #! /usr/bin/env python |
---|
| 2 | # encoding: utf-8 |
---|
| 3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
---|
| 4 | |
---|
| 5 | import sys,os |
---|
| 6 | try: |
---|
| 7 | if not(sys.stderr.isatty()and sys.stdout.isatty()): |
---|
| 8 | raise ValueError('not a tty') |
---|
[1a31baf] | 9 | from ctypes import Structure,windll,c_short,c_ushort,c_ulong,c_int,byref,POINTER,c_long,c_char |
---|
[0fa325b] | 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)] |
---|
[1a31baf] | 18 | windll.kernel32.GetStdHandle.argtypes=[c_ulong] |
---|
| 19 | windll.kernel32.GetStdHandle.restype=c_ulong |
---|
| 20 | windll.kernel32.GetConsoleScreenBufferInfo.argtypes=[c_ulong,POINTER(CONSOLE_SCREEN_BUFFER_INFO)] |
---|
| 21 | windll.kernel32.GetConsoleScreenBufferInfo.restype=c_long |
---|
| 22 | windll.kernel32.SetConsoleTextAttribute.argtypes=[c_ulong,c_ushort] |
---|
| 23 | windll.kernel32.SetConsoleTextAttribute.restype=c_long |
---|
| 24 | windll.kernel32.FillConsoleOutputCharacterA.argtypes=[c_ulong,c_char,c_ulong,POINTER(COORD),POINTER(c_ulong)] |
---|
| 25 | windll.kernel32.FillConsoleOutputCharacterA.restype=c_long |
---|
| 26 | windll.kernel32.FillConsoleOutputAttribute.argtypes=[c_ulong,c_ushort,c_ulong,POINTER(COORD),POINTER(c_ulong)] |
---|
| 27 | windll.kernel32.FillConsoleOutputAttribute.restype=c_long |
---|
| 28 | windll.kernel32.SetConsoleCursorPosition.argtypes=[c_ulong,POINTER(COORD)] |
---|
| 29 | windll.kernel32.SetConsoleCursorPosition.restype=c_long |
---|
| 30 | windll.kernel32.SetConsoleCursorInfo.argtypes=[c_ulong,POINTER(CONSOLE_CURSOR_INFO)] |
---|
| 31 | windll.kernel32.SetConsoleCursorInfo.restype=c_long |
---|
[0fa325b] | 32 | sbinfo=CONSOLE_SCREEN_BUFFER_INFO() |
---|
| 33 | csinfo=CONSOLE_CURSOR_INFO() |
---|
| 34 | hconsole=windll.kernel32.GetStdHandle(-11) |
---|
| 35 | windll.kernel32.GetConsoleScreenBufferInfo(hconsole,byref(sbinfo)) |
---|
| 36 | if sbinfo.Size.X<9 or sbinfo.Size.Y<9:raise ValueError('small console') |
---|
| 37 | windll.kernel32.GetConsoleCursorInfo(hconsole,byref(csinfo)) |
---|
| 38 | except Exception: |
---|
| 39 | pass |
---|
| 40 | else: |
---|
| 41 | import re,threading |
---|
| 42 | is_vista=getattr(sys,"getwindowsversion",None)and sys.getwindowsversion()[0]>=6 |
---|
| 43 | try: |
---|
| 44 | _type=unicode |
---|
| 45 | except NameError: |
---|
| 46 | _type=str |
---|
| 47 | to_int=lambda number,default:number and int(number)or default |
---|
| 48 | wlock=threading.Lock() |
---|
| 49 | STD_OUTPUT_HANDLE=-11 |
---|
| 50 | STD_ERROR_HANDLE=-12 |
---|
| 51 | class AnsiTerm(object): |
---|
| 52 | def __init__(self): |
---|
| 53 | self.encoding=sys.stdout.encoding |
---|
| 54 | self.hconsole=windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) |
---|
| 55 | self.cursor_history=[] |
---|
| 56 | self.orig_sbinfo=CONSOLE_SCREEN_BUFFER_INFO() |
---|
| 57 | self.orig_csinfo=CONSOLE_CURSOR_INFO() |
---|
| 58 | windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(self.orig_sbinfo)) |
---|
| 59 | windll.kernel32.GetConsoleCursorInfo(hconsole,byref(self.orig_csinfo)) |
---|
| 60 | def screen_buffer_info(self): |
---|
| 61 | sbinfo=CONSOLE_SCREEN_BUFFER_INFO() |
---|
| 62 | windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo)) |
---|
| 63 | return sbinfo |
---|
| 64 | def clear_line(self,param): |
---|
| 65 | mode=param and int(param)or 0 |
---|
| 66 | sbinfo=self.screen_buffer_info() |
---|
| 67 | if mode==1: |
---|
| 68 | line_start=COORD(0,sbinfo.CursorPosition.Y) |
---|
| 69 | line_length=sbinfo.Size.X |
---|
| 70 | elif mode==2: |
---|
| 71 | line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y) |
---|
| 72 | line_length=sbinfo.Size.X-sbinfo.CursorPosition.X |
---|
| 73 | else: |
---|
| 74 | line_start=sbinfo.CursorPosition |
---|
| 75 | line_length=sbinfo.Size.X-sbinfo.CursorPosition.X |
---|
[1a31baf] | 76 | chars_written=c_ulong() |
---|
| 77 | windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),line_length,line_start,byref(chars_written)) |
---|
[0fa325b] | 78 | windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,line_length,line_start,byref(chars_written)) |
---|
| 79 | def clear_screen(self,param): |
---|
| 80 | mode=to_int(param,0) |
---|
| 81 | sbinfo=self.screen_buffer_info() |
---|
| 82 | if mode==1: |
---|
| 83 | clear_start=COORD(0,0) |
---|
| 84 | clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y |
---|
| 85 | elif mode==2: |
---|
| 86 | clear_start=COORD(0,0) |
---|
| 87 | clear_length=sbinfo.Size.X*sbinfo.Size.Y |
---|
| 88 | windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start) |
---|
| 89 | else: |
---|
| 90 | clear_start=sbinfo.CursorPosition |
---|
| 91 | clear_length=((sbinfo.Size.X-sbinfo.CursorPosition.X)+sbinfo.Size.X*(sbinfo.Size.Y-sbinfo.CursorPosition.Y)) |
---|
[1a31baf] | 92 | chars_written=c_ulong() |
---|
| 93 | windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),clear_length,clear_start,byref(chars_written)) |
---|
[0fa325b] | 94 | windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,clear_length,clear_start,byref(chars_written)) |
---|
| 95 | def push_cursor(self,param): |
---|
| 96 | sbinfo=self.screen_buffer_info() |
---|
| 97 | self.cursor_history.append(sbinfo.CursorPosition) |
---|
| 98 | def pop_cursor(self,param): |
---|
| 99 | if self.cursor_history: |
---|
| 100 | old_pos=self.cursor_history.pop() |
---|
| 101 | windll.kernel32.SetConsoleCursorPosition(self.hconsole,old_pos) |
---|
| 102 | def set_cursor(self,param): |
---|
| 103 | y,sep,x=param.partition(';') |
---|
| 104 | x=to_int(x,1)-1 |
---|
| 105 | y=to_int(y,1)-1 |
---|
| 106 | sbinfo=self.screen_buffer_info() |
---|
| 107 | new_pos=COORD(min(max(0,x),sbinfo.Size.X),min(max(0,y),sbinfo.Size.Y)) |
---|
| 108 | windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos) |
---|
| 109 | def set_column(self,param): |
---|
| 110 | x=to_int(param,1)-1 |
---|
| 111 | sbinfo=self.screen_buffer_info() |
---|
| 112 | new_pos=COORD(min(max(0,x),sbinfo.Size.X),sbinfo.CursorPosition.Y) |
---|
| 113 | windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos) |
---|
| 114 | def move_cursor(self,x_offset=0,y_offset=0): |
---|
| 115 | sbinfo=self.screen_buffer_info() |
---|
| 116 | 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)) |
---|
| 117 | windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos) |
---|
| 118 | def move_up(self,param): |
---|
| 119 | 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)) |
---|
| 122 | def move_left(self,param): |
---|
| 123 | self.move_cursor(x_offset=-to_int(param,1)) |
---|
| 124 | def move_right(self,param): |
---|
| 125 | self.move_cursor(x_offset=to_int(param,1)) |
---|
| 126 | def next_line(self,param): |
---|
| 127 | sbinfo=self.screen_buffer_info() |
---|
| 128 | self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=to_int(param,1)) |
---|
| 129 | def prev_line(self,param): |
---|
| 130 | sbinfo=self.screen_buffer_info() |
---|
| 131 | self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=-to_int(param,1)) |
---|
| 132 | def rgb2bgr(self,c): |
---|
| 133 | return((c&1)<<2)|(c&2)|((c&4)>>2) |
---|
| 134 | def set_color(self,param): |
---|
| 135 | cols=param.split(';') |
---|
| 136 | sbinfo=CONSOLE_SCREEN_BUFFER_INFO() |
---|
| 137 | windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo)) |
---|
| 138 | attr=sbinfo.Attributes |
---|
| 139 | for c in cols: |
---|
| 140 | if is_vista: |
---|
| 141 | c=int(c) |
---|
| 142 | else: |
---|
| 143 | c=to_int(c,0) |
---|
[1a31baf] | 144 | if 29<c<38: |
---|
[0fa325b] | 145 | attr=(attr&0xfff0)|self.rgb2bgr(c-30) |
---|
[1a31baf] | 146 | elif 39<c<48: |
---|
[0fa325b] | 147 | attr=(attr&0xff0f)|(self.rgb2bgr(c-40)<<4) |
---|
| 148 | elif c==0: |
---|
| 149 | attr=self.orig_sbinfo.Attributes |
---|
| 150 | elif c==1: |
---|
| 151 | attr|=0x08 |
---|
| 152 | elif c==4: |
---|
| 153 | attr|=0x80 |
---|
| 154 | elif c==7: |
---|
| 155 | attr=(attr&0xff88)|((attr&0x70)>>4)|((attr&0x07)<<4) |
---|
| 156 | windll.kernel32.SetConsoleTextAttribute(self.hconsole,attr) |
---|
| 157 | def show_cursor(self,param): |
---|
| 158 | csinfo.bVisible=1 |
---|
| 159 | windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo)) |
---|
| 160 | def hide_cursor(self,param): |
---|
| 161 | csinfo.bVisible=0 |
---|
| 162 | windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo)) |
---|
| 163 | 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,} |
---|
| 164 | ansi_tokens=re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))') |
---|
| 165 | def write(self,text): |
---|
| 166 | try: |
---|
| 167 | wlock.acquire() |
---|
| 168 | for param,cmd,txt in self.ansi_tokens.findall(text): |
---|
| 169 | if cmd: |
---|
| 170 | cmd_func=self.ansi_command_table.get(cmd) |
---|
| 171 | if cmd_func: |
---|
| 172 | cmd_func(self,param) |
---|
| 173 | else: |
---|
| 174 | self.writeconsole(txt) |
---|
| 175 | finally: |
---|
| 176 | wlock.release() |
---|
| 177 | def writeconsole(self,txt): |
---|
| 178 | chars_written=c_int() |
---|
| 179 | writeconsole=windll.kernel32.WriteConsoleA |
---|
| 180 | if isinstance(txt,_type): |
---|
| 181 | writeconsole=windll.kernel32.WriteConsoleW |
---|
| 182 | TINY_STEP=3000 |
---|
| 183 | for x in range(0,len(txt),TINY_STEP): |
---|
| 184 | tiny=txt[x:x+TINY_STEP] |
---|
| 185 | writeconsole(self.hconsole,tiny,len(tiny),byref(chars_written),None) |
---|
| 186 | def flush(self): |
---|
| 187 | pass |
---|
| 188 | def isatty(self): |
---|
| 189 | return True |
---|
| 190 | sys.stderr=sys.stdout=AnsiTerm() |
---|
| 191 | os.environ['TERM']='vt100' |
---|