-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInterface.py
258 lines (211 loc) · 8.35 KB
/
Interface.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#MineBot
#GPL and all that
# - espes
import io
import logging
from textwrap import wrap
from signal import signal, SIGWINCH
from fcntl import ioctl
from tty import TIOCGWINSZ
from struct import unpack
from twisted.python import log
from twisted.internet import reactor, protocol, task
from twisted.conch.manhole import Manhole
from twisted.conch.insults.insults import TerminalProtocol, privateModes, _KEY_NAMES, FUNCTION_KEYS
from twisted.conch.insults.helper import TerminalBuffer
from twisted.conch.insults.window import Widget, TopWindow, TextOutput, VBox, YieldFocus, horizontalLine, cursor
class FixedTerminalBuffer(TerminalBuffer):
lastWrite = ''
def write(self, bytes):
self.lastWrite = bytes
TerminalBuffer.write(self, bytes)
def nextLine(self):
self.x = 0
self.write('\n')
#make key ids the same as ServerProtocol
for name, const in zip(_KEY_NAMES, FUNCTION_KEYS):
setattr(FixedTerminalBuffer, name, const)
class AnnouncingTerminalBuffer(FixedTerminalBuffer):
def __init__(self, modifyCallback):
self.modifyCallback = modifyCallback
for m in ['insertAtCursor', '_scrollDown', '_scrollUp',
'eraseToLineEnd', 'eraseToLineBeginning',
'eraseDisplay', 'eraseToDisplayEnd',
'eraseToDisplayBeginning', 'deleteCharacter',
'insertLine', 'deleteLine',
'cursorUp', 'cursorDown', 'cursorBackward',
'cursorForward', 'cursorPosition', 'cursorHome',
'restoreCursor']:
exec """\
def %s(self, *args, **kargs):
r = FixedTerminalBuffer.%s(self, *args, **kargs)
self.modifyCallback()
return r
""" % (m, m)
class TerminalProtocolWidget(Widget):
width = 80
height = 24
def __init__(self, tp):
Widget.__init__(self)
self.tp = tp
self._buf = AnnouncingTerminalBuffer(self.repaint)
self._buf.width = self.width
self._buf.height = self.height
self._buf.connectionMade()
self.tp.makeConnection(self._buf)
def keystrokeReceived(self, keyID, modifier):
self.tp.keystrokeReceived(keyID, modifier)
def draw(self, width, height, terminal):
if width != self.width or height != self.height:
self.resizeTerminal(width, height)
Widget.draw(self, width, height, terminal)
def resizeTerminal(self, width, height):
self._buf.width = width
self._buf.height = height
self._buf.eraseDisplay()
self.tp.terminalSize(width, height)
self.repaint()
def render(self, width, height, terminal):
for y, line in enumerate(self._buf.lines[:height]):
terminal.cursorPosition(0, y)
n = 0
for n, (ch, attr) in enumerate(line[:width]):
if ch is self._buf.void:
ch = ' '
if y == self._buf.y and n == self._buf.x:
cursor(terminal, ch)
else:
terminal.write(ch)
if n < width:
terminal.write(' ' * (width - n - 1))
#adapted from invective (http://twistedmatrix.com/trac/browser/sandbox/exarkun/invective)
class OutputWidget(TextOutput):
def __init__(self, size=None):
TextOutput.__init__(self, size)
self.messages = []
def formatMessage(self, s, width):
return wrap(s, width=width, subsequent_indent=" ")
def addMessage(self, message):
self.messages.append(message.encode("utf-8"))
self.messages = self.messages[-200:]
self.repaint()
def render(self, width, height, terminal):
output = []
for i in xrange(len(self.messages) - 1, -1, -1):
output[:0] = self.formatMessage(self.messages[i], width - 2)
if len(output) >= height:
break
if len(output) < height:
output[:0] = [''] * (height - len(output))
for n, L in enumerate(output[-height:]):
terminal.cursorPosition(0, n)
terminal.write(L + ' ' * (width - len(L)))
class SeperatorWidget(Widget):
def sizeHint(self):
return (None, 1)
def focusReceived(self):
raise YieldFocus()
def render(self, width, height, terminal):
horizontalLine(terminal, 0, 0, width)
class OutputLogStream(io.BufferedIOBase):
def __init__(self, logWidget):
self.logWidget = logWidget
self.buffer = ""
def write(self, b):
self.buffer += b
if "\n" in b:
self.flush()
def flush(self):
lines = self.buffer.split("\n")
self.buffer = lines[-1]
for line in lines[:-1]:
self.logWidget.addMessage(line)
class BotInterface(TerminalProtocol):
width = 80
height = 24
def __init__(self, manholeNamespace = None):
self.manholeNamespace = manholeNamespace
def _draw(self):
self.window.draw(self.width, self.height, self.terminal)
def _schedule(self, f):
reactor.callLater(0, f)
def connectionMade(self):
TerminalProtocol.connectionMade(self)
self.terminal.eraseDisplay()
self.terminal.resetPrivateModes([privateModes.CURSOR_MODE])
self.window = TopWindow(self._draw, self._schedule)
vbox = VBox()
self.logWidget = OutputWidget()
vbox.addChild(self.logWidget)
logHandler = logging.StreamHandler(OutputLogStream(self.logWidget))
#make it use the default formatter
logHandler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
logging.getLogger().addHandler(logHandler)
vbox.addChild(SeperatorWidget())
#TODO: factory stuff
self.manhole = Manhole()
self.manholeView = TerminalProtocolWidget(self.manhole)
#set the namespace directly so it's mutable
if self.manholeNamespace is not None:
self.manhole.interpreter.locals = self.manholeNamespace
vbox.addChild(self.manholeView)
self.window.addChild(vbox)
def keystrokeReceived(self, keyID, modifier):
self.window.keystrokeReceived(keyID, modifier)
def terminalSize(self, width, height):
self.width = width
self.height = height
self._draw()
#because twisted is too fail to have an interface
#from invective (http://twistedmatrix.com/trac/browser/sandbox/exarkun/invective)
class CommandLineBotInterface(BotInterface):
def connectionMade(self):
signal(SIGWINCH, self.windowChanged)
winSize = self.getWindowSize()
self.width = winSize[0]
self.height = winSize[1]
BotInterface.connectionMade(self)
def connectionLost(self, reason):
if reactor.running: reactor.stop()
# XXX Should be part of runWithProtocol
def getWindowSize(self):
winsz = ioctl(0, TIOCGWINSZ, '12345678')
winSize = unpack('4H', winsz)
newSize = winSize[1], winSize[0], winSize[3], winSize[2]
return newSize
def windowChanged(self, signum, frame):
winSize = self.getWindowSize()
self.terminalSize(winSize[0], winSize[1])
#better version of twisted.conch.stdio.runWithProtocol
def runReactorWithTerminal(terminalProtocol, *args):
import os, tty, sys, termios
from twisted.internet import stdio
from twisted.conch.insults.insults import ServerProtocol
#Fix quitter so it doesn't close stdin
#so we can call quit() from the terminal
#and not mess up our terminal
class NewQuitter(quit.__class__):
def __call__(self, code=None):
raise SystemExit(code)
if isinstance(__builtins__, dict):
__builtins__['quit'] = NewQuitter('quit')
__builtins__['exit'] = NewQuitter('exit')
else:
__builtins__.quit = NewQuitter('quit')
__builtins__.exit = NewQuitter('exit')
fd = sys.__stdin__.fileno()
oldSettings = termios.tcgetattr(fd)
tty.setraw(fd)
try:
p = ServerProtocol(terminalProtocol, *args)
stdio.StandardIO(p)
reactor.run()
finally:
termios.tcsetattr(fd, termios.TCSANOW, oldSettings)
os.write(fd, "\r\x1bc\r")
if __name__ == "__main__":
logging.basicConfig(filename="client.log", level=logging.DEBUG)
observer = log.PythonLoggingObserver()
observer.start()
log.startLoggingWithObserver(lambda a: '')
runReactorWithTerminal(CommandLineBotInterface)