Skip to content

controller

dev_tool.tui.controller

log = logging.getLogger(__name__) module-attribute

MenuController

A controller class for the TUI menu.

This class handles user input and navigation in the TUI menu.

The constructor for the MenuController class.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance.

  • ui (UserInterface) –

    The user interface instance.

Source code in dev_tool/tui/controller.py
def __init__(self, terminal: Terminal, ui: UserInterface) -> None:
    """
    The constructor for the MenuController class.

    :param terminal: The blessed Terminal instance.
    :param ui: The user interface instance.
    """

    self.command: Callable[[], None] | None = None
    self.event_manager = CONTEXT.get_event_manager()
    self.lock = threading.Lock()
    self.notification = CONTEXT.get_notification_manager(terminal)
    self.previous_height = terminal.height
    self.previous_view = ViewType.MAIN_MENU
    self.previous_width = terminal.width
    self.running = True
    self.should_restart = False
    self.terminal = terminal
    self.ui = ui
    self.views = ViewManager(terminal, ui)

command = None instance-attribute

event_manager = CONTEXT.get_event_manager() instance-attribute

lock = threading.Lock() instance-attribute

notification = CONTEXT.get_notification_manager(terminal) instance-attribute

previous_height = terminal.height instance-attribute

previous_view = ViewType.MAIN_MENU instance-attribute

previous_width = terminal.width instance-attribute

running = True instance-attribute

should_restart = False instance-attribute

terminal = terminal instance-attribute

ui = ui instance-attribute

views = ViewManager(terminal, ui) instance-attribute

handle_input

A method that handles user input.

Parameters:

  • key (str) –

    The key name.

Source code in dev_tool/tui/controller.py
def handle_input(self, key: str) -> None:
    """
    A method that handles user input.

    :param key: The key name.
    """

    current = self.views.current
    result = self.views.handle_input(key)

    if result == MenuAction.QUIT:
        self.running = False
        return

    if result == MenuAction.REDRAW:
        self.views.draw()
        return

    if self.views.current == ViewType.MAIN_MENU and current == ViewType.MAIN_MENU:
        if key in (Key.ENTER, Key.LINE_FEED, Key.CARRIAGE_RETURN):
            item = self.ui.get_selected_item()
            command = item.action.run(self)  # type: ignore[arg-type]

            if callable(command):
                self.command = command  # type: ignore[assignment]

            return

        self.ui.handle_navigation(key)

    self.previous_view = current

is_resized

A method that checks if the terminal size has changed.

Returns:

  • bool

    True if terminal was resized, False otherwise.

Source code in dev_tool/tui/controller.py
def is_resized(self) -> bool:
    """
    A method that checks if the terminal size has changed.

    :return: True if terminal was resized, False otherwise.
    """

    if self.terminal.width != self.previous_width or self.terminal.height != self.previous_height:
        self.previous_width = self.terminal.width
        self.previous_height = self.terminal.height

        return True

    return False

restart

A method that signals the controller to restart itself.

Source code in dev_tool/tui/controller.py
def restart(self) -> None:
    """A method that signals the controller to restart itself."""

    with self.lock:
        self.should_restart = True

run

A method that runs the menu controller's main loop.

Source code in dev_tool/tui/controller.py
def run(self) -> None:
    """A method that runs the menu controller's main loop."""

    self.event_manager.emit_app_event(AppEventType.APP_STARTED)

    try:
        while True:
            self._initialize_loop()
            self._run_tui_loop()

            if self.command:
                self._execute_command()

            if not self.should_restart:
                break
    finally:
        self.event_manager.emit_app_event(AppEventType.APP_CLOSING)
        self.event_manager.emit_app_event(AppEventType.APP_CLOSED)