forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.py
More file actions
executable file
·126 lines (114 loc) · 4.09 KB
/
interactive.py
File metadata and controls
executable file
·126 lines (114 loc) · 4.09 KB
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
#!/usr/bin/env python
"""
Not much commentary in this example. It's more of a demo.
"""
import sys
import code
import textwrap
import io
import time
import traceback
sys.path.insert(0, '../')
import tdl
sys.ps1 = '>>> '
sys.ps2 = '... '
WIDTH, HEIGHT = 80, 50
console = tdl.init(WIDTH, HEIGHT, 'Python Interpeter in TDL')
console.setMode('scroll')
class TDLPrint(io.TextIOBase):
def __init__(self, fgcolor=(255, 255, 255), bgcolor=(0, 0, 0)):
self.colors = fgcolor, bgcolor
def write(self, string):
olderr.write(string)
console.setColors(*self.colors)
console.write(string)
#sys.stdout = TDLPrint()
sys.stdout = console
sys.stdout.move(0, HEIGHT-1)
olderr = newerr = sys.stderr
newerr = TDLPrint((255, 255, 255), (127, 0, 0))
def exit():
raise SystemExit
interpeter = code.InteractiveConsole({'tdl':tdl,
'console':console,
'exit':exit})
def main():
print()
print('Python %s' % sys.version)
print('Press ESC to quit')
buffer = ''
commands = ['']
banner = sys.ps1
cursor = 0
while 1:
console.drawRect(0, HEIGHT-1, None, 1, ' ', (255, 255, 255), (0, 0, 0))
console.drawStr(0, HEIGHT-1, banner + buffer)
try:
console.drawChar(len(banner) + cursor, HEIGHT-1, None, None, (0, 255, 255))
except tdl.TDLError:
pass
tdl.flush()
for event in tdl.event.get():
if event.type == 'QUIT':
raise SystemExit()
if event.type == 'KEYDOWN':
if event.key == 'ENTER' or event.key == 'KPENTER':
sys.stderr = newerr
try:
console.drawRect(0, HEIGHT-1, None, 1, None, (255, 255, 255), (0, 0, 0))
console.scroll(0, -1)
if interpeter.push(buffer):
banner = sys.ps2
else:
banner = sys.ps1
except SystemExit:
raise
except:
sys.excepthook(*sys.exc_info())
banner = sys.ps1
finally:
sys.stderr = olderr
sys.stdout = olderr
sys.stdout = console
if buffer not in commands:
commands.append(buffer)
buffer = ''
elif event.key == 'BACKSPACE':
if cursor == 0:
continue
if buffer[:cursor][-4:] == ' ':
buffer = buffer[:cursor-4] + buffer[cursor:]
cursor -= 4
elif buffer:
buffer = buffer[:cursor-1] + buffer[cursor:]
cursor -= 1
elif event.key == 'DELETE':
buffer = buffer[:cursor] + buffer[cursor+1:]
elif event.key == 'LEFT':
cursor -= 1
elif event.key == 'RIGHT':
cursor += 1
elif event.key == 'HOME':
cursor = 0
elif event.key == 'END':
cursor = len(buffer)
elif event.key == 'UP':
commands.insert(0, buffer)
buffer = commands.pop()
cursor = len(buffer)
elif event.key == 'DOWN':
commands.append(buffer)
buffer = commands.pop(0)
cursor = len(buffer)
elif event.key == 'TAB':
buffer = buffer[:cursor] + ' ' + buffer[cursor:]
cursor += 4
elif event.key == 'ESCAPE':
raise SystemExit()
elif event.char:
buffer = buffer[:cursor] + event.char + buffer[cursor:]
cursor += 1
cursor = max(0, min(cursor, len(buffer)))
time.sleep(.01)
if __name__ == '__main__':
main()