~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to tools/wafadmin/ansiterm.py

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
42
 
                def screen_buffer_info(self):
43
 
                        sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
44
 
                        windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(sbinfo))
45
 
                        return sbinfo
46
 
 
47
 
                def clear_line(self, param):
48
 
                        mode = param and int(param) or 0
49
 
                        sbinfo = self.screen_buffer_info()
50
 
                        if mode == 1: # Clear from begining of line to cursor position
51
 
                                line_start = COORD(0, sbinfo.CursorPosition.Y)
52
 
                                line_length = sbinfo.Size.X
53
 
                        elif mode == 2: # Clear entire line
54
 
                                line_start = COORD(sbinfo.CursorPosition.X, sbinfo.CursorPosition.Y)
55
 
                                line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
56
 
                        else: # Clear from cursor position to end of line
57
 
                                line_start = sbinfo.CursorPosition
58
 
                                line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
59
 
                        chars_written = c_int()
60
 
                        windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), line_length, line_start, byref(chars_written))
61
 
                        windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, line_length, line_start, byref(chars_written))
62
 
 
63
 
                def clear_screen(self, param):
64
 
                        mode = to_int(param, 0)
65
 
                        sbinfo = self.screen_buffer_info()
66
 
                        if mode == 1: # Clear from begining of screen to cursor position
67
 
                                clear_start = COORD(0, 0)
68
 
                                clear_length = sbinfo.CursorPosition.X * sbinfo.CursorPosition.Y
69
 
                        elif mode == 2: # Clear entire screen and return cursor to home
70
 
                                clear_start = COORD(0, 0)
71
 
                                clear_length = sbinfo.Size.X * sbinfo.Size.Y
72
 
                                windll.kernel32.SetConsoleCursorPosition(self.hconsole, clear_start)
73
 
                        else: # Clear from cursor position to end of screen
74
 
                                clear_start = sbinfo.CursorPosition
75
 
                                clear_length = ((sbinfo.Size.X - sbinfo.CursorPosition.X) + sbinfo.Size.X * (sbinfo.Size.Y - sbinfo.CursorPosition.Y))
76
 
                        chars_written = c_int()
77
 
                        windll.kernel32.FillConsoleOutputCharacterA(self.hconsole, c_char(' '), clear_length, clear_start, byref(chars_written))
78
 
                        windll.kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, clear_length, clear_start, byref(chars_written))
79
 
 
80
 
                def push_cursor(self, param):
81
 
                        sbinfo = self.screen_buffer_info()
82
 
                        self.cursor_history.push(sbinfo.CursorPosition)
83
 
 
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
 
 
89
 
                def set_cursor(self, param):
90
 
                        x, sep, y = param.partition(';')
91
 
                        x = to_int(x, 1) - 1
92
 
                        y = to_int(y, 1) - 1
93
 
                        sbinfo = self.screen_buffer_info()
94
 
                        new_pos = COORD(
95
 
                                min(max(0, x), sbinfo.Size.X),
96
 
                                min(max(0, y), sbinfo.Size.Y)
97
 
                        )
98
 
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
99
 
 
100
 
                def set_column(self, param):
101
 
                        x = to_int(param, 1) - 1
102
 
                        sbinfo = self.screen_buffer_info()
103
 
                        new_pos = COORD(
104
 
                                min(max(0, x), sbinfo.Size.X),
105
 
                                sbinfo.CursorPosition.Y
106
 
                        )
107
 
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
108
 
 
109
 
                def move_cursor(self, x_offset=0, y_offset=0):
110
 
                        sbinfo = self.screen_buffer_info()
111
 
                        new_pos = COORD(
112
 
                                min(max(0, sbinfo.CursorPosition.X + x_offset), sbinfo.Size.X),
113
 
                                min(max(0, sbinfo.CursorPosition.Y + y_offset), sbinfo.Size.Y)
114
 
                        )
115
 
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
116
 
 
117
 
                def move_up(self, param):
118
 
                        self.move_cursor(y_offset = -to_int(param, 1))
119
 
 
120
 
                def move_down(self, param):
121
 
                        self.move_cursor(y_offset = to_int(param, 1))
122
 
 
123
 
                def move_left(self, param):
124
 
                        self.move_cursor(x_offset = -to_int(param, 1))
125
 
 
126
 
                def move_right(self, param):
127
 
                        self.move_cursor(x_offset = to_int(param, 1))
128
 
 
129
 
                def next_line(self, param):
130
 
                        sbinfo = self.screen_buffer_info()
131
 
                        self.move_cursor(
132
 
                                x_offset = -sbinfo.CursorPosition.X,
133
 
                                y_offset = to_int(param, 1)
134
 
                        )
135
 
 
136
 
                def prev_line(self, param):
137
 
                        sbinfo = self.screen_buffer_info()
138
 
                        self.move_cursor(
139
 
                                x_offset = -sbinfo.CursorPosition.X,
140
 
                                y_offset = -to_int(param, 1)
141
 
                        )
142
 
 
143
 
                escape_to_color = { (0, 30): 0x0,                        #black
144
 
                                                        (0, 31): 0x4,                    #red
145
 
                                                        (0, 32): 0x2,                    #green
146
 
                                                        (0, 33): 0x4+0x2,                #dark yellow
147
 
                                                        (0, 34): 0x1,                    #blue
148
 
                                                        (0, 35): 0x1+0x4,                #purple
149
 
                                                        (0, 36): 0x2+0x4,                #cyan
150
 
                                                        (0, 37): 0x1+0x2+0x4,    #grey
151
 
                                                        (1, 30): 0x1+0x2+0x4,    #dark gray
152
 
                                                        (1, 31): 0x4+0x8,                #red
153
 
                                                        (1, 32): 0x2+0x8,                #light green
154
 
                                                        (1, 33): 0x4+0x2+0x8,    #yellow
155
 
                                                        (1, 34): 0x1+0x8,                #light blue
156
 
                                                        (1, 35): 0x1+0x4+0x8,    #light purple
157
 
                                                        (1, 36): 0x1+0x2+0x8,    #light cyan
158
 
                                                        (1, 37): 0x1+0x2+0x4+0x8, #white
159
 
                                                   }
160
 
 
161
 
                def set_color(self, param):
162
 
                        intensity, sep, color = param.partition(';')
163
 
                        intensity = to_int(intensity, 0)
164
 
                        color = to_int(color, 0)
165
 
                        if intensity and not color:
166
 
                                color, intensity = intensity, color
167
 
                        attrib = self.escape_to_color.get((intensity, color), 0x7)
168
 
                        windll.kernel32.SetConsoleTextAttribute(self.hconsole, attrib)
169
 
 
170
 
                def show_cursor(self,param):
171
 
                        csinfo.bVisible = 1
172
 
                        windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
173
 
 
174
 
                def hide_cursor(self,param):
175
 
                        csinfo.bVisible = 0
176
 
                        windll.kernel32.SetConsoleCursorInfo(self.hconsole, byref(csinfo))
177
 
 
178
 
                ansi_command_table = {
179
 
                        'A': move_up,
180
 
                        'B': move_down,
181
 
                        'C': move_right,
182
 
                        'D': move_left,
183
 
                        'E': next_line,
184
 
                        'F': prev_line,
185
 
                        'G': set_column,
186
 
                        'H': set_cursor,
187
 
                        'f': set_cursor,
188
 
                        'J': clear_screen,
189
 
                        'K': clear_line,
190
 
                        'h': show_cursor,
191
 
                        'l': hide_cursor,
192
 
                        'm': set_color,
193
 
                        's': push_cursor,
194
 
                        'u': pop_cursor,
195
 
                }
196
 
                # Match either the escape sequence or text not containing escape sequence
197
 
                ansi_tokans = re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
198
 
                def write(self, text):
199
 
                        wlock.acquire()
200
 
                        for param, cmd, txt in self.ansi_tokans.findall(text):
201
 
                                if cmd:
202
 
                                        cmd_func = self.ansi_command_table.get(cmd)
203
 
                                        if cmd_func:
204
 
                                                cmd_func(self, param)
205
 
                                else:
206
 
                                        chars_written = c_int()
207
 
                                        if isinstance(txt, unicode):
208
 
                                                windll.kernel32.WriteConsoleW(self.hconsole, txt, len(txt), byref(chars_written), None)
209
 
                                        else:
210
 
                                                windll.kernel32.WriteConsoleA(self.hconsole, txt, len(txt), byref(chars_written), None)
211
 
                        wlock.release()
212
 
 
213
 
                def flush(self):
214
 
                        pass
215
 
 
216
 
                def isatty(self):
217
 
                        return True
218
 
 
219
 
        sys.stderr = sys.stdout = AnsiTerm()
220
 
        os.environ['TERM'] = 'vt100'
221