-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
66 lines (53 loc) · 1.9 KB
/
tests.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
import urwid
from twisted.internet import epollreactor
epollreactor.install()
from twisted.python import log
log.startLogging(open('/dev/null', 'w'), setStdout=False)
from widgets import CompletingEdit
class View(object):
palette = [
('body', 'default', 'default'),
('edit', 'white', 'dark blue')
]
# command name: command parameters completing function
completing_dict = {
'help': None,
'quit': None,
'hakumamatata': None,
'tataramamama': None,
'tatarasratara': None
}
def __init__(self):
self.walker = urwid.SimpleListWalker([])
self.listbox = urwid.ListBox(self.walker)
self.edit = CompletingEdit(' > ', completing_dict=self.completing_dict)
self.frame = urwid.Frame(self._wrap(self.listbox, 'body'),
footer=self._wrap(self.edit, 'edit'), focus_part='footer')
@staticmethod
def _wrap(widget, attr_map):
return urwid.AttrMap(widget, attr_map)
def write(self, text):
self.walker.append(urwid.Text(unicode(text)))
self.walker.set_focus(len(self.walker.contents))
class TestApplication(object):
def __init__(self):
self.view = View()
def run(self):
self.loop = urwid.MainLoop(self.view.frame, self.view.palette,
unhandled_input=self.handle_keys,
event_loop=urwid.TwistedEventLoop(),
handle_mouse=False)
self.loop.run()
def handle_keys(self, key):
if key == 'enter':
self.view.write(self.view.edit.edit_text)
self.loop.draw_screen()
if self.view.edit.edit_text.startswith('quit'):
raise urwid.ExitMainLoop()
self.view.edit.set_edit_text(u'')
else:
return
return True
if __name__ == '__main__':
app = TestApplication()
app.run()