pyceo/ceo/tui/start.py

70 lines
2.2 KiB
Python
Raw Normal View History

2021-08-28 23:09:02 -04:00
import sys
from asciimatics.event import KeyboardEvent
from asciimatics.exceptions import ResizeScreenError, StopApplication
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from .ConfirmView import ConfirmView
2021-09-06 12:40:05 -04:00
from .ErrorView import ErrorView
2021-08-28 23:09:02 -04:00
from .Model import Model
2021-09-06 12:40:05 -04:00
from .ResultView import ResultView
2021-08-28 23:09:02 -04:00
from .TransactionView import TransactionView
from .WelcomeView import WelcomeView
from .members.AddUserView import AddUserView
2021-09-06 22:29:53 -04:00
from .members.GetUserView import GetUserView
from .members.GetUserResultView import GetUserResultView
2021-09-06 12:40:05 -04:00
from .members.RenewUserView import RenewUserView
2021-08-28 23:09:02 -04:00
def unhandled(event):
if isinstance(event, KeyboardEvent):
c = event.key_code
# Stop on 'q' or 'Esc'
if c in (113, 27):
raise StopApplication("User terminated app")
2021-09-05 18:48:20 -04:00
# tuples of (name, view)
views = []
def screen_wrapper(screen, last_scene, model):
global views
# unload the old views
for name, view in views:
2021-09-06 22:29:53 -04:00
if hasattr(view, '_on_ceoframe_unload'):
view._on_ceoframe_unload()
2021-08-28 23:09:02 -04:00
width = min(screen.width, 90)
height = min(screen.height, 24)
2021-09-05 18:48:20 -04:00
views = [
('Welcome', WelcomeView(screen, width, height, model)),
('Confirm', ConfirmView(screen, width, height, model)),
('Transaction', TransactionView(screen, width, height, model)),
2021-09-06 12:40:05 -04:00
('Result', ResultView(screen, width, height, model)),
('Error', ErrorView(screen, width, height, model)),
('AddUser', AddUserView(screen, width, height, model)),
('RenewUser', RenewUserView(screen, width, height, model)),
2021-09-06 22:29:53 -04:00
('GetUser', GetUserView(screen, width, height, model)),
('GetUserResult', GetUserResultView(screen, width, height, model)),
2021-09-05 18:48:20 -04:00
]
2021-08-28 23:09:02 -04:00
scenes = [
2021-09-05 18:48:20 -04:00
Scene([view], -1, name=name) for name, view in views
2021-08-28 23:09:02 -04:00
]
2021-09-06 22:29:53 -04:00
model.screen = screen
model.views = [view for name, view in views]
2021-08-28 23:09:02 -04:00
screen.play(
2021-09-05 18:48:20 -04:00
scenes, stop_on_resize=True, start_scene=last_scene, allow_int=True,
2021-08-28 23:09:02 -04:00
unhandled_input=unhandled)
def main():
last_scene = None
model = Model()
while True:
try:
Screen.wrapper(screen_wrapper, arguments=[last_scene, model])
sys.exit(0)
except ResizeScreenError as e:
last_scene = e.scene