aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/conch/insults/client.py
blob: 89c79cdaf0938bd358aad6c195edbf5e4ada05e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
You don't really want to use this module. Try insults.py instead.
"""

from twisted.internet import protocol

class InsultsClient(protocol.Protocol):

    escapeTimeout = 0.2

    def __init__(self):
        self.width = self.height = None
        self.xpos = self.ypos = 0
        self.commandQueue = []
        self.inEscape = ''

    def setSize(self, width, height):
        call = 0
        if self.width:
            call = 1
        self.width = width
        self.height = height
        if call:
            self.windowSizeChanged()

    def dataReceived(self, data):
        from twisted.internet import reactor
        for ch in data:
            if ch == '\x1b':
                if self.inEscape:
                    self.keyReceived(ch)
                    self.inEscape = ''
                else:
                    self.inEscape = ch
                    self.escapeCall = reactor.callLater(self.escapeTimeout,
                                                        self.endEscape)
            elif ch in 'ABCD' and self.inEscape:
                self.inEscape = ''
                self.escapeCall.cancel()
                if ch == 'A':
                    self.keyReceived('<Up>')
                elif ch == 'B':
                    self.keyReceived('<Down>')
                elif ch == 'C':
                    self.keyReceived('<Right>')
                elif ch == 'D':
                    self.keyReceived('<Left>')
            elif self.inEscape:
                self.inEscape += ch
            else:
                self.keyReceived(ch)

    def endEscape(self):
        ch = self.inEscape
        self.inEscape = ''
        self.keyReceived(ch)

    def initScreen(self):
        self.transport.write('\x1b=\x1b[?1h')

    def gotoXY(self, x, y):
        """Go to a position on the screen.
        """
        self.xpos = x
        self.ypos = y
        self.commandQueue.append(('gotoxy', x, y))

    def writeCh(self, ch):
        """Write a character to the screen.  If we're at the end of the row,
        ignore the write.
        """
        if self.xpos < self.width - 1:
            self.commandQueue.append(('write', ch))
            self.xpos += 1

    def writeStr(self, s):
        """Write a string to the screen.  This does not wrap a the edge of the
        screen, and stops at \\r and \\n.
        """
        s = s[:self.width-self.xpos]
        if '\n' in s:
            s=s[:s.find('\n')]
        if '\r' in s:
            s=s[:s.find('\r')]
        self.commandQueue.append(('write', s))
        self.xpos += len(s)

    def eraseToLine(self):
        """Erase from the current position to the end of the line.
        """
        self.commandQueue.append(('eraseeol',))

    def eraseToScreen(self):
        """Erase from the current position to the end of the screen.
        """
        self.commandQueue.append(('eraseeos',))
    
    def clearScreen(self):
        """Clear the screen, and return the cursor to 0, 0.
        """
        self.commandQueue = [('cls',)]
        self.xpos = self.ypos = 0

    def setAttributes(self, *attrs):
        """Set the attributes for drawing on the screen.
        """
        self.commandQueue.append(('attributes', attrs))

    def refresh(self):
        """Redraw the screen.
        """
        redraw = ''
        for command in self.commandQueue:
            if command[0] == 'gotoxy':
                redraw += '\x1b[%i;%iH' % (command[2]+1, command[1]+1)
            elif command[0] == 'write':
                redraw += command[1]
            elif command[0] == 'eraseeol':
                redraw += '\x1b[0K'
            elif command[0] == 'eraseeos':
                redraw += '\x1b[OJ'
            elif command[0] == 'cls':
                redraw += '\x1b[H\x1b[J'
            elif command[0] == 'attributes':
                redraw += '\x1b[%sm' % ';'.join(map(str, command[1]))
            else:
                print command
        self.commandQueue = []
        self.transport.write(redraw)

    def windowSizeChanged(self):
        """Called when the size of the window changes.
        Might want to redraw the screen here, or something.
        """

    def keyReceived(self, key):
        """Called when the user hits a key.
        """