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