TDLPrint.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
"""
3
    Not much commentary in this example.  It's more of a demo.
4
"""
5
import sys
6
import code
7
import io
8
import time
9
import traceback
10
11
import tdl
12
13
sys.ps1 = '>>> '
14
sys.ps2 = '... '
15
16
WIDTH, HEIGHT = 80, 50
17
console = tdl.init(WIDTH, HEIGHT, 'Python Interpeter in TDL')
18
console.setMode('scroll')
19
20
class TDLPrint(io.TextIOBase):
21
    def __init__(self, fgcolor=(255, 255, 255), bgcolor=(0, 0, 0)):
22
        self.colors = fgcolor, bgcolor
23
24
    def write(self, string):
25
        olderr.write(string)
26
        console.setColors(*self.colors)
27
        console.write(string)
28
29
#sys.stdout = TDLPrint()
30
sys.stdout = console
31
sys.stdout.move(0, HEIGHT-1)
32
olderr = newerr = sys.stderr
33
newerr = TDLPrint((255, 255, 255), (127, 0, 0))
34
35
def exit():
36
    raise SystemExit
37
38
interpeter = code.InteractiveConsole({'tdl':tdl,
39
                                      'console':console,
40
                                      'exit':exit})
41
42
def main():
43
    print()
44
    print('Python %s' % sys.version)
45
    print('Press ESC to quit')
46
47
    buffer = ''
48
    commands = ['']
49
    banner = sys.ps1
50
    cursor = 0
51
    while 1:
52
        console.draw_rect(0, HEIGHT-1, None, 1, ' ', (255, 255, 255), (0, 0, 0))
53
        console.draw_str(0, HEIGHT-1, banner + buffer)
54
        try:
55
            console.draw_char(len(banner) + cursor, HEIGHT-1, None, None, (0, 255, 255))
56
        except tdl.TDLError:
57
            pass
58
        tdl.flush()
59
60
        for event in tdl.event.get():
61
            if event.type == 'QUIT':
62
                raise SystemExit()
63
            if event.type == 'KEYDOWN':
64
                if event.key == 'ENTER' or event.key == 'KPENTER':
65
                    sys.stderr = newerr
66
                    try:
67
                        console.draw_rect(0, HEIGHT-1, None, 1, None, (255, 255, 255), (0, 0, 0))
68
                        console.scroll(0, -1)
69
                        if interpeter.push(buffer):
70
                            banner = sys.ps2
71
                        else:
72
                            banner = sys.ps1
73
                    except SystemExit:
74
                        raise
75
                    except:
76
                        sys.excepthook(*sys.exc_info())
77
                        banner = sys.ps1
78
                    finally:
79
                        sys.stderr = olderr
80
                        sys.stdout = olderr
81
                    sys.stdout = console
82
                    if buffer not in commands:
83
                        commands.append(buffer)
84
                    buffer = ''
85
                elif event.key == 'BACKSPACE':
86
                    if cursor == 0:
87
                        continue
88
                    if buffer[:cursor][-4:] == '    ':
89
                        buffer = buffer[:cursor-4] + buffer[cursor:]
90
                        cursor -= 4
91
                    elif buffer:
92
                        buffer = buffer[:cursor-1] + buffer[cursor:]
93
                        cursor -= 1
94
                elif event.key == 'DELETE':
95
                    buffer = buffer[:cursor] + buffer[cursor+1:]
96
                elif event.key == 'LEFT':
97
                    cursor -= 1
98
                elif event.key == 'RIGHT':
99
                    cursor += 1
100
                elif event.key == 'HOME':
101
                    cursor = 0
102
                elif event.key == 'END':
103
                    cursor = len(buffer)
104
                elif event.key == 'UP':
105
                    commands.insert(0, buffer)
106
                    buffer = commands.pop()
107
                    cursor = len(buffer)
108
                elif event.key == 'DOWN':
109
                    commands.append(buffer)
110
                    buffer = commands.pop(0)
111
                    cursor = len(buffer)
112
                elif event.key == 'TAB':
113
                    buffer = buffer[:cursor] + '    ' + buffer[cursor:]
114
                    cursor += 4
115
                elif event.key == 'ESCAPE':
116
                    raise SystemExit()
117
                elif event.char:
118
                    buffer = buffer[:cursor] + event.char + buffer[cursor:]
119
                    cursor += 1
120
                cursor = max(0, min(cursor, len(buffer)))
121
        time.sleep(.01)
122
123
if __name__ == '__main__':
124
    main()
125