This commit is contained in:
Ivan Habunek 2024-04-13 08:29:57 +02:00 committed by GitHub
commit c639a2609c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 70 additions and 2 deletions

View File

@ -66,9 +66,13 @@ class Header(urwid.WidgetWrap):
class Footer(urwid.Pile):
def __init__(self):
def __init__(self, tui):
self.tui = tui
self.status = urwid.Text("")
self.message = urwid.Text("")
self.command = Command(tui)
urwid.connect_signal(self.command, "close", self.end_command)
return super().__init__([
urwid.AttrMap(self.status, "footer_status"),
@ -90,6 +94,60 @@ class Footer(urwid.Pile):
def clear_message(self):
self.message.set_text("")
def start_command(self):
self.clear_message()
self.command.set_edit_text("")
self.contents[1] = (self.command, ("weight", 1))
self.focus_position = 1
def end_command(self, widget, success, message):
self.contents[1] = (self.message, ("weight", 1))
self.tui.focus_body()
if message:
if success:
self.set_message(message)
else:
self.set_error_message(message)
class Command(urwid.Edit):
"""Allows execution of vim-like commands in the footer"""
signals = ["close"]
tui: "TUI"
def __init__(self, tui):
self.tui = tui
super().__init__(":")
def keypress(self, size, key):
logger.debug((size, key))
if key == "enter":
self.run_command()
if key == "esc":
self.close()
return super().keypress(size, key)
def close(self, success=True, message=None):
self._emit("close", success, message)
def run_command(self):
command = self.get_edit_text()
if command in ("q", "quit"):
raise urwid.ExitMainLoop()
elif command in ("h", "help"):
self.tui.show_help()
self.close()
else:
self.close(False, f"Unknown command: {command}")
class TUI(urwid.Frame):
"""Main TUI frame."""
@ -134,7 +192,7 @@ class TUI(urwid.Frame):
# Show intro screen while toots are being loaded
self.body = self.build_intro()
self.header = Header(app, user)
self.footer = Footer()
self.footer = Footer(self)
self.footer.set_status("Loading...")
# Default max status length, updated on startup
@ -852,6 +910,12 @@ class TUI(urwid.Frame):
self.async_load_timeline(is_initial=True, timeline_name=self.timeline.name)
def focus_footer(self):
self.focus_part = "footer"
def focus_body(self):
self.focus_part = "body"
# --- Keys -----------------------------------------------------------------
def unhandled_input(self, key):
@ -886,3 +950,7 @@ class TUI(urwid.Frame):
self.close_overlay()
else:
raise urwid.ExitMainLoop()
elif key == ":" and not self.overlay:
self.focus_footer()
self.footer.start_command()