~voluntatefaber/beat-box/bug952329

« back to all changes in this revision

Viewing changes to .waf-1.6.2-ad4cc42bd7d347f7e283789e711b993f/waflib/ansiterm.py

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

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