forked from zyga/textland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo1.py
executable file
·94 lines (84 loc) · 2.95 KB
/
demo1.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
#!/usr/bin/env python3
# This file is part of textland.
#
# Copyright 2014 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <[email protected]>
#
# Textland is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Textland is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Textland. If not, see <http://www.gnu.org/licenses/>.
import argparse
from textland import DrawingContext
from textland import EVENT_KEYBOARD
from textland import EVENT_RESIZE
from textland import Event
from textland import IApplication
from textland import KeyboardData
from textland import Size
from textland import TextImage
from textland import get_display
class DemoApp(IApplication):
def __init__(self):
self.image = TextImage(Size(0, 0))
def consume_event(self, event: Event):
if event.kind == EVENT_RESIZE:
self.image = TextImage(event.data) # data is the new size
elif event.kind == EVENT_KEYBOARD and event.data.key == 'q':
raise StopIteration
self.repaint(event)
return self.image
def repaint(self, event: Event):
# Draw something on the image
ctx = DrawingContext(self.image)
ctx.fill('.')
title = "TextLand Demo Application"
ctx.move_to((self.image.size.width - len(title)) // 2, 0)
ctx.print(title)
ctx.print('=' * len(title))
ctx.move_to(0, 3)
ctx.print("Type 'q' to quit")
ctx.move_to(10, 10)
ctx.print("Event: {}".format(event))
def main():
parser = argparse.ArgumentParser()
parser.set_defaults(display=None)
parser.add_argument(
'--curses',
help="Use curses for display",
action="store_const",
const="curses",
dest="display")
parser.add_argument(
'--print',
help="Use simple line printer for display",
action="store_const",
const="print",
dest="display")
parser.add_argument(
"--test",
help="Use test display (just for testing)",
action="store_const",
const="test",
dest="display")
ns = parser.parse_args()
display = get_display(ns.display)
if ns.display == 'test':
display.inject_event(Event(EVENT_KEYBOARD, KeyboardData('x')))
display.inject_event(Event(EVENT_KEYBOARD, KeyboardData('y')))
display.inject_event(Event(EVENT_KEYBOARD, KeyboardData('q')))
display.run(DemoApp())
if ns.display == 'test':
for frame, image in enumerate(display.screen_log, 1):
print("Frame {}:".format(frame))
image.print_frame()
if __name__ == "__main__":
main()