~ubuntu-branches/ubuntu/trusty/xiphos/trusty

« back to all changes in this revision

Viewing changes to wafadmin/ansiterm.py

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs, Dmitrijs Ledkovs
  • Date: 2012-03-11 18:43:32 UTC
  • mfrom: (17.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120311184332-splq3ecpx7tyi87d
Tags: 3.1.5+dfsg-1
[ Dmitrijs Ledkovs <dmitrij.ledkov@ubuntu.com> ]  
* New upstream release.
* Build using webkit backend
* Contains unpacked source for waf binary (Closes: #654511)
* Update debian/copyright to latest specification

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# encoding: utf-8
 
3
 
 
4
import sys,os
 
5
try:
 
6
        if(not sys.stderr.isatty())or(not sys.stdout.isatty()):
 
7
                raise ValueError('not a tty')
 
8
        from ctypes import*
 
9
        class COORD(Structure):
 
10
                _fields_=[("X",c_short),("Y",c_short)]
 
11
        class SMALL_RECT(Structure):
 
12
                _fields_=[("Left",c_short),("Top",c_short),("Right",c_short),("Bottom",c_short)]
 
13
        class CONSOLE_SCREEN_BUFFER_INFO(Structure):
 
14
                _fields_=[("Size",COORD),("CursorPosition",COORD),("Attributes",c_short),("Window",SMALL_RECT),("MaximumWindowSize",COORD)]
 
15
        class CONSOLE_CURSOR_INFO(Structure):
 
16
                _fields_=[('dwSize',c_ulong),('bVisible',c_int)]
 
17
        sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
 
18
        csinfo=CONSOLE_CURSOR_INFO()
 
19
        hconsole=windll.kernel32.GetStdHandle(-11)
 
20
        windll.kernel32.GetConsoleScreenBufferInfo(hconsole,byref(sbinfo))
 
21
        if sbinfo.Size.X<10 or sbinfo.Size.Y<10:raise Exception('small console')
 
22
        windll.kernel32.GetConsoleCursorInfo(hconsole,byref(csinfo))
 
23
except Exception:
 
24
        pass
 
25
else:
 
26
        import re,threading
 
27
        to_int=lambda number,default:number and int(number)or default
 
28
        wlock=threading.Lock()
 
29
        STD_OUTPUT_HANDLE=-11
 
30
        STD_ERROR_HANDLE=-12
 
31
        class AnsiTerm(object):
 
32
                def __init__(self):
 
33
                        self.hconsole=windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
 
34
                        self.cursor_history=[]
 
35
                def screen_buffer_info(self):
 
36
                        sbinfo=CONSOLE_SCREEN_BUFFER_INFO()
 
37
                        windll.kernel32.GetConsoleScreenBufferInfo(self.hconsole,byref(sbinfo))
 
38
                        return sbinfo
 
39
                def clear_line(self,param):
 
40
                        mode=param and int(param)or 0
 
41
                        sbinfo=self.screen_buffer_info()
 
42
                        if mode==1:
 
43
                                line_start=COORD(0,sbinfo.CursorPosition.Y)
 
44
                                line_length=sbinfo.Size.X
 
45
                        elif mode==2:
 
46
                                line_start=COORD(sbinfo.CursorPosition.X,sbinfo.CursorPosition.Y)
 
47
                                line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
 
48
                        else:
 
49
                                line_start=sbinfo.CursorPosition
 
50
                                line_length=sbinfo.Size.X-sbinfo.CursorPosition.X
 
51
                        chars_written=c_int()
 
52
                        windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),line_length,line_start,byref(chars_written))
 
53
                        windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,line_length,line_start,byref(chars_written))
 
54
                def clear_screen(self,param):
 
55
                        mode=to_int(param,0)
 
56
                        sbinfo=self.screen_buffer_info()
 
57
                        if mode==1:
 
58
                                clear_start=COORD(0,0)
 
59
                                clear_length=sbinfo.CursorPosition.X*sbinfo.CursorPosition.Y
 
60
                        elif mode==2:
 
61
                                clear_start=COORD(0,0)
 
62
                                clear_length=sbinfo.Size.X*sbinfo.Size.Y
 
63
                                windll.kernel32.SetConsoleCursorPosition(self.hconsole,clear_start)
 
64
                        else:
 
65
                                clear_start=sbinfo.CursorPosition
 
66
                                clear_length=((sbinfo.Size.X-sbinfo.CursorPosition.X)+sbinfo.Size.X*(sbinfo.Size.Y-sbinfo.CursorPosition.Y))
 
67
                        chars_written=c_int()
 
68
                        windll.kernel32.FillConsoleOutputCharacterA(self.hconsole,c_char(' '),clear_length,clear_start,byref(chars_written))
 
69
                        windll.kernel32.FillConsoleOutputAttribute(self.hconsole,sbinfo.Attributes,clear_length,clear_start,byref(chars_written))
 
70
                def push_cursor(self,param):
 
71
                        sbinfo=self.screen_buffer_info()
 
72
                        self.cursor_history.push(sbinfo.CursorPosition)
 
73
                def pop_cursor(self,param):
 
74
                        if self.cursor_history:
 
75
                                old_pos=self.cursor_history.pop()
 
76
                                windll.kernel32.SetConsoleCursorPosition(self.hconsole,old_pos)
 
77
                def set_cursor(self,param):
 
78
                        x,sep,y=param.partition(';')
 
79
                        x=to_int(x,1)-1
 
80
                        y=to_int(y,1)-1
 
81
                        sbinfo=self.screen_buffer_info()
 
82
                        new_pos=COORD(min(max(0,x),sbinfo.Size.X),min(max(0,y),sbinfo.Size.Y))
 
83
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
 
84
                def set_column(self,param):
 
85
                        x=to_int(param,1)-1
 
86
                        sbinfo=self.screen_buffer_info()
 
87
                        new_pos=COORD(min(max(0,x),sbinfo.Size.X),sbinfo.CursorPosition.Y)
 
88
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
 
89
                def move_cursor(self,x_offset=0,y_offset=0):
 
90
                        sbinfo=self.screen_buffer_info()
 
91
                        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))
 
92
                        windll.kernel32.SetConsoleCursorPosition(self.hconsole,new_pos)
 
93
                def move_up(self,param):
 
94
                        self.move_cursor(y_offset=-to_int(param,1))
 
95
                def move_down(self,param):
 
96
                        self.move_cursor(y_offset=to_int(param,1))
 
97
                def move_left(self,param):
 
98
                        self.move_cursor(x_offset=-to_int(param,1))
 
99
                def move_right(self,param):
 
100
                        self.move_cursor(x_offset=to_int(param,1))
 
101
                def next_line(self,param):
 
102
                        sbinfo=self.screen_buffer_info()
 
103
                        self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=to_int(param,1))
 
104
                def prev_line(self,param):
 
105
                        sbinfo=self.screen_buffer_info()
 
106
                        self.move_cursor(x_offset=-sbinfo.CursorPosition.X,y_offset=-to_int(param,1))
 
107
                escape_to_color={(0,30):0x0,(0,31):0x4,(0,32):0x2,(0,33):0x4+0x2,(0,34):0x1,(0,35):0x1+0x4,(0,36):0x2+0x4,(0,37):0x1+0x2+0x4,(1,30):0x1+0x2+0x4,(1,31):0x4+0x8,(1,32):0x2+0x8,(1,33):0x4+0x2+0x8,(1,34):0x1+0x8,(1,35):0x1+0x4+0x8,(1,36):0x1+0x2+0x8,(1,37):0x1+0x2+0x4+0x8,}
 
108
                def set_color(self,param):
 
109
                        intensity,sep,color=param.partition(';')
 
110
                        intensity=to_int(intensity,0)
 
111
                        color=to_int(color,0)
 
112
                        if intensity and not color:
 
113
                                color,intensity=intensity,color
 
114
                        attrib=self.escape_to_color.get((intensity,color),0x7)
 
115
                        windll.kernel32.SetConsoleTextAttribute(self.hconsole,attrib)
 
116
                def show_cursor(self,param):
 
117
                        csinfo.bVisible=1
 
118
                        windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
 
119
                def hide_cursor(self,param):
 
120
                        csinfo.bVisible=0
 
121
                        windll.kernel32.SetConsoleCursorInfo(self.hconsole,byref(csinfo))
 
122
                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,}
 
123
                ansi_tokans=re.compile('(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
 
124
                def write(self,text):
 
125
                        wlock.acquire()
 
126
                        for param,cmd,txt in self.ansi_tokans.findall(text):
 
127
                                if cmd:
 
128
                                        cmd_func=self.ansi_command_table.get(cmd)
 
129
                                        if cmd_func:
 
130
                                                cmd_func(self,param)
 
131
                                else:
 
132
                                        chars_written=c_int()
 
133
                                        if isinstance(txt,unicode):
 
134
                                                windll.kernel32.WriteConsoleW(self.hconsole,txt,len(txt),byref(chars_written),None)
 
135
                                        else:
 
136
                                                windll.kernel32.WriteConsoleA(self.hconsole,txt,len(txt),byref(chars_written),None)
 
137
                        wlock.release()
 
138
                def flush(self):
 
139
                        pass
 
140
                def isatty(self):
 
141
                        return True
 
142
        sys.stderr=sys.stdout=AnsiTerm()
 
143
        os.environ['TERM']='vt100'
 
144