-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.py
executable file
·86 lines (65 loc) · 2.16 KB
/
tui.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
#!/usr/bin/env python3
import urwid
from typing import *
GENERATE = False
class Grid(urwid.Pile):
def __init__(self, rows: int, cols: int, cellSize: int = 7):
self.cellsEditor = [urwid.Edit("", align="center") for i in range(rows*cols)]
self.__rows = rows
self.__cols = cols
div = urwid.Divider()
super(Grid, self).__init__([
urwid.Columns(
[div] +
[
(cellSize, urwid.LineBox(self.cellsEditor[r*cols+c])) for c in range(cols)
] +
[div]
) for r in range(rows)
])
def get_text(self, r: int, c: int) -> str:
return self.cellsEditor[r * self.__cols + c].get_text()
class FocusManager:
def __init__(self, frame: urwid.Frame):
self.frame = frame
def __call__(self, input: str) -> bool:
if input == 'tab':
self.frame.set_focus(
"footer" if self.frame.get_focus() == "body"
else "body"
)
return True
return False
def onExitClick(button: urwid.Button) -> NoReturn:
raise urwid.ExitMainLoop()
def onGenerateClick(button: urwid.Button) -> NoReturn:
global GENERATE
GENERATE = True
raise urwid.ExitMainLoop()
def startInteractiveScreen(rows: int, cols: int) -> None:
headerTxt = urwid.Text(
u"Fill the matrix with coefficients or scalars by moving with arrow keys.\n"
"Click on GENERATE to quit this interactive screen and print the result on stdout.\n"
"Use TAB to change focus."
)
exitButton = urwid.Button(u"EXIT")
urwid.connect_signal(exitButton, "click", onExitClick)
generateButton = urwid.Button(u"GENERATE")
urwid.connect_signal(generateButton, "click", onGenerateClick)
div = urwid.Divider()
buttonsList = urwid.Columns([
div,
(8, exitButton),
(5, div),
(12, generateButton)
])
grid = Grid(rows, cols)
frame = urwid.Frame(urwid.Filler(grid), header=headerTxt, footer=buttonsList, focus_part="body")
focusManager = FocusManager(frame)
urwid.MainLoop(frame, unhandled_input = focusManager).run()
if GENERATE:
return [
editor.get_text()[0] if editor.get_text()[0] != "" else "0" for editor in grid.cellsEditor
]
if __name__ == "__main__":
print(startInteractiveScreen(3, 4))