add a UI class

rewrite
Nate Smith 2023-12-10 15:38:46 -08:00
parent 6811deaedc
commit dcab278223
1 changed files with 49 additions and 23 deletions

60
main.py
View File

@ -7,6 +7,7 @@ import os
from datetime import timezone from datetime import timezone
import sqlite3 import sqlite3
import sys import sys
import threading
from time import sleep from time import sleep
from typing import Optional, Tuple, TypeVar from typing import Optional, Tuple, TypeVar
@ -86,16 +87,11 @@ def setup() -> Optional[Exception]:
return None return None
def main() -> Optional[Exception]: class UI:
username = getuser() def __init__(self) -> None:
self.quitting = False
e = setup() self.pwin = curses.initscr()
if e is not None: self.pwin.keypad(True)
return e
# TODO rug sweep curses stuff
parentwin = curses.initscr()
parentwin.keypad(True)
curses.noecho() curses.noecho()
curses.raw() curses.raw()
if curses.has_colors(): if curses.has_colors():
@ -108,19 +104,49 @@ def main() -> Optional[Exception]:
# so we ignore this error for terminal compatibility. # so we ignore this error for terminal compatibility.
pass pass
if curses.COLS < MIN_SCREEN_WIDTH: if curses.COLS < MIN_SCREEN_WIDTH:
return Exception("the terminal window is too narrow") raise Exception("the terminal window is too narrow")
if curses.LINES < MIN_SCREEN_HEIGHT: if curses.LINES < MIN_SCREEN_HEIGHT:
return Exception("the terminal window is too short") raise Exception("the terminal window is too short")
self.menuwin = curses.newwin(10, 30, 4, 2)
menuwin = curses.newwin(10, 30, 4, 2) self.plantwin = curses.newwin(30, 40, 4, 31)
plantwin = curses.newwin(30, 40, 4, 31) self.scorewin = curses.newwin(2, 30, 15, 2)
scorewin = curses.newwin(2, 30, 15, 2)
# TODO info area (is this where prompt is rendered?) # TODO info area (is this where prompt is rendered?)
def quit(self) -> None:
self.quitting = True
def handle_input(self) -> None:
while True: while True:
c = parentwin.getch() c = self.pwin.getch()
if c == -1 or c == ord("q") or c == ord("x") or c == 27: if c == -1 or c == ord("q") or c == ord("x") or c == 27:
self.quit()
break break
# TODO Plant
def main() -> Optional[Exception]:
username = getuser()
e = setup()
if e is not None:
return e
try:
ui = UI()
except Exception as e:
return Exception(f"could not initialize UI: {e}")
ithread = threading.Thread(target=ui.handle_input, args=())
ithread.start()
while True:
if ui.quitting:
break
# TODO get plant info from db
# TODO update in-memory representation of derived characteristics / plant info
# TODO redraw plant (if changed)
# TODO redraw water gauge
sleep(INTERVAL) sleep(INTERVAL)
try: try: