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
import sqlite3
import sys
import threading
from time import sleep
from typing import Optional, Tuple, TypeVar
@ -86,16 +87,11 @@ def setup() -> Optional[Exception]:
return None
def main() -> Optional[Exception]:
username = getuser()
e = setup()
if e is not None:
return e
# TODO rug sweep curses stuff
parentwin = curses.initscr()
parentwin.keypad(True)
class UI:
def __init__(self) -> None:
self.quitting = False
self.pwin = curses.initscr()
self.pwin.keypad(True)
curses.noecho()
curses.raw()
if curses.has_colors():
@ -108,19 +104,49 @@ def main() -> Optional[Exception]:
# so we ignore this error for terminal compatibility.
pass
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:
return Exception("the terminal window is too short")
menuwin = curses.newwin(10, 30, 4, 2)
plantwin = curses.newwin(30, 40, 4, 31)
scorewin = curses.newwin(2, 30, 15, 2)
raise Exception("the terminal window is too short")
self.menuwin = curses.newwin(10, 30, 4, 2)
self.plantwin = curses.newwin(30, 40, 4, 31)
self.scorewin = curses.newwin(2, 30, 15, 2)
# TODO info area (is this where prompt is rendered?)
def quit(self) -> None:
self.quitting = True
def handle_input(self) -> None:
while True:
c = parentwin.getch()
c = self.pwin.getch()
if c == -1 or c == ord("q") or c == ord("x") or c == 27:
self.quit()
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)
try: