Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement vim-like commands in footer #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ def set_text(self, text):


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"),
Expand All @@ -68,6 +72,60 @@ def set_error_message(self, text):
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."""
Expand Down Expand Up @@ -99,7 +157,7 @@ def __init__(self, app, user):
# 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
Expand Down Expand Up @@ -640,6 +698,12 @@ def close_overlay(self):
self.body = self.overlay.bottom_w
self.overlay = None

def focus_footer(self):
self.focus_part = "footer"

def focus_body(self):
self.focus_part = "body"

# --- Keys -----------------------------------------------------------------

def unhandled_input(self, key):
Expand Down Expand Up @@ -676,3 +740,7 @@ def unhandled_input(self, key):
self.close_overlay()
else:
raise urwid.ExitMainLoop()

elif key == ":" and not self.overlay:
self.focus_footer()
self.footer.start_command()