-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_ui.py
63 lines (49 loc) · 1.83 KB
/
console_ui.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
from controller import Controller
from entities import Key
import curses
import log
class Console_ui:
should_redraw = False
def write_correct_char(self, key):
self.stdscr.addch(key.char)
def write_wrong_char(self, key):
self.stdscr.addch(key.char, curses.A_STANDOUT)
def redraw(self):
self.stdscr.clear()
def goto_writing_position(self, position=0):
y, x = self.stdscr.getmaxyx()
self.stdscr.move(max(1, round(y * position)), 0)
def get_next_key(self):
return self.stdscr.getkey()
def calculate_x_position(self, justify, text):
maxy, maxx = self.stdscr.getmaxyx()
if justify == "LEFT":
return 0
if justify == "MIDDLE":
return round(maxx / 2 - len(text) / 2)
def write(self, text, justify="LEFT"):
if justify == "LEFT":
self.stdscr.addstr(text + "\n")
if justify == "MIDDLE":
y, x = self.stdscr.getyx()
newx = self.calculate_x_position(justify, text)
self.stdscr.addstr(y, newx, text)
def write_stage(self, text, justify="LEFT"):
y, x = self.stdscr.getyx()
self.write(text, justify)
newx = self.calculate_x_position(justify, text)
self.stdscr.move(y + 1, newx)
def inner_loop(self, stdscr):
self.stdscr = stdscr
self.controller = Controller(self)
c = ""
while c != "`": # main loops waits for Ctrl+C
c = self.get_next_key()
if ord(c) == ord(curses.erasechar()):
self.controller.sendKey(Key(special="ERASE"))
y, x = stdscr.getyx()
stdscr.delch(y, x - 1)
elif ord(c) not in [ord("\n"), curses.KEY_ENTER]:
self.controller.sendKey(Key(c))
def loop(self):
curses.wrapper(self.inner_loop)