summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2022-05-28 16:02:06 +0200
committerDaniel Schadt <kingdread@gmx.de>2022-05-28 16:03:18 +0200
commited1c28e5418d90547f8ee53b4391d68453547da7 (patch)
treeeb88e4c46510f57278d21ce1b98e4f39391a2e38
parenta93c4c383b637a0ed45f7cd8236a78d02591273f (diff)
downloadtf2sgu-ed1c28e5418d90547f8ee53b4391d68453547da7.tar.gz
tf2sgu-ed1c28e5418d90547f8ee53b4391d68453547da7.tar.bz2
tf2sgu-ed1c28e5418d90547f8ee53b4391d68453547da7.zip
clean up nested closure code
-rwxr-xr-xtf2sgu159
1 files changed, 91 insertions, 68 deletions
diff --git a/tf2sgu b/tf2sgu
index cb1dc5c..99e23e0 100755
--- a/tf2sgu
+++ b/tf2sgu
@@ -380,95 +380,116 @@ def _tui_select_profile(screen, profiles):
return profiles[view.selection]
-def _tui(args, screen, profile):
+def _tui_title(screen, title):
import curses
- def init_layout():
- _tui_title(screen, "Copy savegames")
- height, width = screen.getmaxyx()
+ _, width = screen.getmaxyx()
+ title = f"{title!s:{width}}"
+ screen.addnstr(0, 0, title, width, curses.A_REVERSE)
+
+
+class Tui:
+ def __init__(self, args, screen, profile):
+ self.args = args
+ self.screen = screen
+ self.profile = profile
+ self.helptexts = [
+ "<Up> and <Down> to select",
+ "<Tab> to switch panel",
+ "<Enter> to copy",
+ "<r> to refresh",
+ "<q> to quit",
+ ]
+ self.views = [None, None]
+
+ def reload(self):
+ local_saves = self.profile.saves()
+ remote_saves = read_saves(self.args.remote_dir)
+ self.views[0].set_items(local_saves)
+ self.views[1].set_items(remote_saves)
+
+ def init_layout(self):
+ import curses
+
+ _tui_title(self.screen, "Copy savegames")
+ height, width = self.screen.getmaxyx()
- screen.addstr(2, 0, "<Up> and <Down> to select")
- screen.addstr(3, 0, "<Tab> to switch panel")
- screen.addstr(4, 0, "<Enter> to copy")
- screen.addstr(5, 0, "<r> to refresh")
- screen.addstr(6, 0, "<q> to quit")
+ for i, text in enumerate(self.helptexts, 2):
+ self.screen.addstr(i, 0, text)
- screen.addstr(9, 0, "Local Savegames", curses.A_BOLD)
- screen.addstr(9, int(width / 2), "Cloud Savegames", curses.A_BOLD)
+ panel_title_y = len(self.helptexts) + 3
+ self.screen.addstr(panel_title_y, 0, "Local Savegames", curses.A_BOLD)
+ self.screen.addstr(
+ panel_title_y, int(width / 2), "Cloud Savegames", curses.A_BOLD
+ )
+ panel_offset = len(self.helptexts) + 4
+ panel_height = height - panel_offset
wins = [
- screen.derwin(height - 10, int(width / 2), 10, 0),
- screen.derwin(height - 10, int(width / 2), 10, int(width / 2)),
+ self.screen.derwin(panel_height, int(width / 2), panel_offset, 0),
+ self.screen.derwin(
+ panel_height, int(width / 2), panel_offset, int(width / 2)
+ ),
]
return wins
- windows = init_layout()
+ def setup(self):
+ windows = self.init_layout()
+ self.views = [
+ TuiListView(windows[0], []),
+ TuiListView(windows[1], []),
+ ]
- views = [
- TuiListView(windows[0], []),
- TuiListView(windows[1], []),
- ]
- active = 0
- views[active].activate()
+ def loop(self):
+ import curses
- def reload():
- local_saves = profile.saves()
- remote_saves = read_saves(args.remote_dir)
- views[0].set_items(local_saves)
- views[1].set_items(remote_saves)
+ active = 0
+ self.views[active].activate()
- reload()
+ self.reload()
- while True:
- for view in views:
- view.render()
+ while True:
+ for view in self.views:
+ view.render()
- c = screen.getch()
+ char = self.screen.getch()
- if c in {ord("Q"), ord("q")}:
- break
+ if char in {ord("Q"), ord("q")}:
+ break
- elif c == curses.KEY_UP:
- views[active].up()
+ elif char == curses.KEY_UP:
+ self.views[active].up()
- elif c == curses.KEY_DOWN:
- views[active].down()
+ elif char == curses.KEY_DOWN:
+ self.views[active].down()
- elif c == ord("\t"):
- views[active].deactivate()
- active = 1 - active
- views[active].activate()
+ elif char == ord("\t"):
+ self.views[active].deactivate()
+ active = 1 - active
+ self.views[active].activate()
- elif c in {ord("R"), ord("r")}:
- reload()
+ elif char in {ord("R"), ord("r")}:
+ self.reload()
- elif c in {10, curses.KEY_ENTER}:
- try:
- item = views[active].selected_item()
- except IndexError:
- pass
- else:
- if active == 0:
- dest = args.remote_dir
+ elif char in {10, curses.KEY_ENTER}:
+ try:
+ item = self.views[active].selected_item()
+ except IndexError:
+ pass
else:
- dest = profile.save_dir
- item.copy_to(dest)
- reload()
- screen.addstr(8, 0, f"{item.name} copied!")
-
- elif c == curses.KEY_RESIZE:
- screen.clear()
- windows = init_layout()
- for view, window in zip(views, windows):
- view.set_window(window)
+ if active == 0:
+ dest = self.args.remote_dir
+ else:
+ dest = self.profile.save_dir
+ item.copy_to(dest)
+ self.reload()
+ self.screen.addstr(8, 0, f"{item.name} copied!")
-
-def _tui_title(screen, title):
- import curses
-
- _, width = screen.getmaxyx()
- title = f"{title!s:{width}}"
- screen.addnstr(0, 0, title, width, curses.A_REVERSE)
+ elif char == curses.KEY_RESIZE:
+ self.screen.clear()
+ windows = self.init_layout()
+ for view, window in zip(self.views, windows):
+ view.set_window(window)
def _curses_tui(args, screen):
@@ -496,7 +517,9 @@ def _curses_tui(args, screen):
screen.clear()
- _tui(args, screen, profile)
+ tui = Tui(args, screen, profile)
+ tui.setup()
+ tui.loop()
if __name__ == "__main__":