-
Notifications
You must be signed in to change notification settings - Fork 4
/
1_facade.py
43 lines (33 loc) · 973 Bytes
/
1_facade.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
class Buffer:
def __init__(self, width=30, height=20):
self.width = width
self.height = height
self.buffer = [' '] * (width*height)
def __getitem__(self, item):
return self.buffer.__getitem__(item)
def write(self, text):
self.buffer += text
class Viewport:
def __init__(self, buffer=Buffer()):
self.buffer = buffer
self.offset = 0
def get_char_at(self, index):
return self.buffer[self.offset+index]
def append(self, text):
self.buffer += text
class Console:
def __init__(self):
b = Buffer()
self.current_viewport = Viewport(b)
self.buffers = [b]
self.viewports = [self.current_viewport]
# high-level
def write(self, text):
self.current_viewport.buffer.write(text)
# low-level
def get_char_at(self, index):
return self.current_viewport.get_char_at(index)
if __name__ == '__main__':
c = Console()
c.write('hello')
ch = c.get_char_at(0)