commit e4301b3b82041608f173a97eb78139f89f24407f
parent 6bf1493fe5cdb8aa3e87029e4b434055a024aaa6
Author: luke8086 <55237178+luke8086@users.noreply.github.com>
Date: Mon, 25 Jul 2022 21:42:04 +0000
Simplify code for updating layout
Diffstat:
| M | retronews.py | | | 55 | +++++++++++++++++++------------------------------------ |
1 file changed, 19 insertions(+), 36 deletions(-)
diff --git a/retronews.py b/retronews.py
@@ -97,12 +97,12 @@ class StoriesPage:
title: str = ""
-@dataclass(frozen=True)
+@dataclass
class Layout:
lines: int = 0
cols: int = 0
top_menu_row: int = 0
- index_start: int = 0
+ index_start: int = 1
index_height: int = 0
middle_menu_row: Optional[int] = None
pager_start: Optional[int] = None
@@ -600,8 +600,24 @@ def app_render_menus(app: AppState) -> None:
app.screen.insstr(lt.flash_menu_row, 0, app.flash or "")
+def app_update_layout(app: AppState) -> None:
+ lt = app.layout
+
+ (lt.lines, lt.cols) = app.screen.getmaxyx()
+
+ max_index_height = lt.lines - 3
+ lt.index_height = (max_index_height // 3) if app.pager_visible else max_index_height
+
+ lt.middle_menu_row = lt.index_start + lt.index_height if app.pager_visible else None
+ lt.pager_start = lt.index_start + lt.index_height + 1 if app.pager_visible else None
+ lt.pager_height = lt.lines - lt.pager_start - 2 if lt.pager_start is not None else None
+
+ lt.bottom_menu_row = lt.lines - 2
+ lt.flash_menu_row = lt.lines - 1
+
+
def app_render(app: AppState) -> None:
- app.layout = app_compute_layout(app)
+ app_update_layout(app)
app.screen.erase()
app_render_index(app)
@@ -616,39 +632,6 @@ def app_render(app: AppState) -> None:
app.flash = ""
-def app_compute_layout(app: AppState) -> Layout:
- (lines, cols) = app.screen.getmaxyx()
-
- if lines < 25 or cols < 80:
- raise Exception("At least 80x25 terminal is required")
-
- index_start = 1
- max_index_height = lines - 3
- index_height = (max_index_height // 3) if app.pager_visible else max_index_height
-
- if app.pager_visible:
- middle_menu_row = index_start + index_height
- pager_start = index_start + index_height + 1
- pager_height = lines - pager_start - 2
- else:
- middle_menu_row = None
- pager_start = None
- pager_height = None
-
- return Layout(
- lines=lines,
- cols=cols,
- top_menu_row=0,
- index_start=index_start,
- index_height=index_height,
- middle_menu_row=middle_menu_row,
- pager_start=pager_start,
- pager_height=pager_height,
- bottom_menu_row=lines - 2,
- flash_menu_row=lines - 1,
- )
-
-
def app_init_logging() -> None:
format = "%(asctime)s %(levelname)s: %(message)s"
stream = open("tmp/retronews.log", "a")