s3: libsmbclient: Add missing talloc stackframe.
[sfrench/samba-autobuild/.git] / third_party / waf / wafadmin / ansiterm.py
1 import sys, os
2 try:
3         if (not sys.stderr.isatty()) or (not sys.stdout.isatty()):
4                 raise ValueError('not a tty')
5
6         from ctypes import *
7
8         class COORD(Structure):
9                 _fields_ = [("X", c_short), ("Y", c_short)]
10
11         class SMALL_RECT(Structure):
12                 _fields_ = [("Left", c_short), ("Top", c_short), ("Right", c_short), ("Bottom", c_short)]
13
14         class CONSOLE_SCREEN_BUFFER_INFO(Structure):
15                 _fields_ = [("Size", COORD), ("CursorPosition", COORD), ("Attributes", c_short), ("Window", SMALL_RECT), ("MaximumWindowSize", COORD)]
16
17         class CONSOLE_CURSOR_INFO(Structure):
18                 _fields_ = [('dwSize',c_ulong), ('bVisible', c_int)]
19
20         sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
21         csinfo = CONSOLE_CURSOR_INFO()
22         hconsole = windll.kernel32.GetStdHandle(-11)
23         windll.kernel32.GetConsoleScreenBufferInfo(hconsole, byref(sbinfo))
24         if sbinfo.Size.X < 10 or sbinfo.Size.Y < 10: raise Exception('small console')
25         windll.kernel32.GetConsoleCursorInfo(hconsole, byref(csinfo))
26 except Exception:
27         pass
28 else:
29         import re, threading
30
31         to_int = lambda number, default: number and int(number) or default
32         wlock = threading.Lock()
33
34         STD_OUTPUT_HANDLE = -11
35         STD_ERROR_HANDLE = -12
36
37         class AnsiTerm(object):
38                 def __init__(self):
39                         self.hconsole = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
40                         self.cursor_history = []
41                         self.orig_sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
42                         self.orig_csinfo = CONSOLE_CURSOR_INFO()
43                         windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(self.orig_sbinfo))
44                         windll.kernel32.GetConsoleCursorInfo(hconsole, byref(self.orig_csinfo))
45
46
47                 def screen_buffer_info(self):
48                         sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
49                         windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(sbinfo))
50                         return sbinfo
51
52                 def clear_line(self, param):
53                         mode = param and int(param) or 0
54                         sbinfo = self.screen_buffer_info()
55                         if mode == 1: # Clear from begining of line to cursor position
56                                 line_start = COORD(0, sbinfo.CursorPosition.Y)
57                                 line_length = sbinfo.Size.X
58                         elif mode == 2: # Clear entire line
59                                 line_start = COORD(sbinfo.CursorPosition.X, sbinfo.CursorPosition.Y)
60                                 line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
61                         else: # Clear from cursor position to end of line
62                                 line_start = sbinfo.CursorPosition
63                                 line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
64                         chars_written = c_int()
65                         windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), line_length, line_start, byref(chars_written))
66                         windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, line_length, line_start, byref(chars_written))
67
68                 def clear_screen(self, param):
69                         mode = to_int(param, 0)
70                         sbinfo = self.screen_buffer_info()
71                         if mode == 1: # Clear from begining of screen to cursor position
72                                 clear_start = COORD(0, 0)
73                                 clear_length = sbinfo.CursorPosition.X * sbinfo.CursorPosition.Y
74                         elif mode == 2: # Clear entire screen and return cursor to home
75                                 clear_start = COORD(0, 0)
76                                 clear_length = sbinfo.Size.X * sbinfo.Size.Y
77                                 windll.kernel32.SetConsoleCursorPosition(self.hconsole, clear_start)
78                         else: # Clear from cursor position to end of screen
79                                 clear_start = sbinfo.CursorPosition
80                                 clear_length = ((sbinfo.Size.X - sbinfo.CursorPosition.X) + sbinfo.Size.X * (sbinfo.Size.Y - sbinfo.CursorPosition.Y))
81                         chars_written = c_int()
82                         windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), clear_length, clear_start, byref(chars_written))
83                         windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, clear_length, clear_start, byref(chars_written))
84
85                 def push_cursor(self, param):
86                         sbinfo = self.screen_buffer_info()
87                         self.cursor_history.push(sbinfo.CursorPosition)
88
89                 def pop_cursor(self, param):
90                         if self.cursor_history:
91                                 old_pos = self.cursor_history.pop()
92                                 windll.kernel32.SetConsoleCursorPosition(self.hconsole, old_pos)
93
94                 def set_cursor(self, param):
95                         x, sep, y = param.partition(';')
96                         x = to_int(x, 1) - 1
97                         y = to_int(y, 1) - 1
98                         sbinfo = self.screen_buffer_info()
99                         new_pos = COORD(
100                                 min(max(0, x), sbinfo.Size.X),
101                                 min(max(0, y), sbinfo.Size.Y)
102                         )
103                         windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
104
105                 def set_column(self, param):
106                         x = to_int(param, 1) - 1
107                         sbinfo = self.screen_buffer_info()
108                         new_pos = COORD(
109                                 min(max(0, x), sbinfo.Size.X),
110                                 sbinfo.CursorPosition.Y
111                         )
112                         windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
113
114                 def move_cursor(self, x_offset=0, y_offset=0):
115                         sbinfo = self.screen_buffer_info()
116                         new_pos = COORD(
117                                 min(max(0, sbinfo.CursorPosition.X + x_offset), sbinfo.Size.X),
118                                 min(max(0, sbinfo.CursorPosition.Y + y_offset), sbinfo.Size.Y)
119                         )
120                         windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
121
122                 def move_up(self, param):
123                         self.move_cursor(y_offset = -to_int(param, 1))
124
125                 def move_down(self, param):
126                         self.move_cursor(y_offset = to_int(param, 1))
127
128                 def move_left(self, param):
129                         self.move_cursor(x_offset = -to_int(param, 1))
130
131                 def move_right(self, param):
132                         self.move_cursor(x_offset = to_int(param, 1))
133
134                 def next_line(self, param):
135                         sbinfo = self.screen_buffer_info()
136                         self.move_cursor(
137                                 x_offset = -sbinfo.CursorPosition.X,
138                                 y_offset = to_int(param, 1)
139                         )
140
141                 def prev_line(self, param):
142                         sbinfo = self.screen_buffer_info()
143                         self.move_cursor(
144                                 x_offset = -sbinfo.CursorPosition.X,
145                                 y_offset = -to_int(param, 1)
146                         )
147
148                 escape_to_color = { (0, 30): 0x0,                        #black
149                                                         (0, 31): 0x4,                    #red
150                                                         (0, 32): 0x2,                    #green
151                                                         (0, 33): 0x4+0x2,                #dark yellow
152                                                         (0, 34): 0x1,                    #blue
153                                                         (0, 35): 0x1+0x4,                #purple
154                                                         (0, 36): 0x2+0x4,                #cyan
155                                                         (0, 37): 0x1+0x2+0x4,    #grey
156                                                         (1, 30): 0x1+0x2+0x4,    #dark gray
157                                                         (1, 31): 0x4+0x8,                #red
158                                                         (1, 32): 0x2+0x8,                #light green
159                                                         (1, 33): 0x4+0x2+0x8,    #yellow
160                                                         (1, 34): 0x1+0x8,                #light blue
161                                                         (1, 35): 0x1+0x4+0x8,    #light purple
162                                                         (1, 36): 0x1+0x2+0x8,    #light cyan
163                                                         (1, 37): 0x1+0x2+0x4+0x8, #white
164                                                    }
165
166                 def set_color(self, param):
167                         cols = param.split(';')
168                         attr = self.orig_sbinfo.Attributes
169                         for c in cols:
170                                 c = to_int(c, 0)
171                                 if c in range(30,38):
172                                         attr = (attr & 0xf0) | (self.escape_to_color.get((0,c), 0x7))
173                                 elif c in range(40,48):
174                                         attr = (attr & 0x0f) | (self.escape_to_color.get((0,c), 0x7) << 8)
175                                 elif c in range(90,98):
176                                         attr = (attr & 0xf0) | (self.escape_to_color.get((1,c-60), 0x7))
177                                 elif c in range(100,108):
178                                         attr = (attr & 0x0f) | (self.escape_to_color.get((1,c-60), 0x7) << 8)
179                                 elif c == 1:
180                                         attr |= 0x08
181                         windll.kernel32.SetConsoleTextAttribute(self.hconsole, attr)
182
183                 def show_cursor(self,param):
184                         csinfo.bVisible = 1
185                         windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
186
187                 def hide_cursor(self,param):
188                         csinfo.bVisible = 0
189                         windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
190
191                 ansi_command_table = {
192                         'A': move_up,
193                         'B': move_down,
194                         'C': move_right,
195                         'D': move_left,
196                         'E': next_line,
197                         'F': prev_line,
198                         'G': set_column,
199                         'H': set_cursor,
200                         'f': set_cursor,
201                         'J': clear_screen,
202                         'K': clear_line,
203                         'h': show_cursor,
204                         'l': hide_cursor,
205                         'm': set_color,
206                         's': push_cursor,
207                         'u': pop_cursor,
208                 }
209                 # Match either the escape sequence or text not containing escape sequence
210                 ansi_tokans = re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
211                 def write(self, text):
212                         try:
213                                 wlock.acquire()
214                                 for param, cmd, txt in self.ansi_tokans.findall(text):
215                                         if cmd:
216                                                 cmd_func = self.ansi_command_table.get(cmd)
217                                                 if cmd_func:
218                                                         cmd_func(self, param)
219                                         else:
220                                                 chars_written = c_int()
221                                                 if isinstance(txt, unicode):
222                                                         windll.kernel32.WriteConsoleW(self.hconsole, txt, len(txt), byref(chars_written), None)
223                                                 else:
224                                                         windll.kernel32.WriteConsoleA(self.hconsole, txt, len(txt), byref(chars_written), None)
225                         finally:
226                                 wlock.release()
227
228                 def flush(self):
229                         pass
230
231                 def isatty(self):
232                         return True
233
234         sys.stderr = sys.stdout = AnsiTerm()
235         os.environ['TERM'] = 'vt100'