Skip to content

tui

dev_tool.tui

__all__ = ['Action', 'ActionGroup', 'CommandAction', 'DisplayAction', 'Menu', 'MenuElement', 'MenuFactory', 'MenuItem', 'SubmenuAction'] module-attribute

Action

Bases: Protocol

A protocol for menu actions.

This class defines the interface for executing actions in the TUI menu.

run

A method that executes the action.

Parameters:

Source code in dev_tool/tui/menu/action.py
def run(self, controller: MenuController) -> object | None:
    """
    A method that executes the action.

    :param controller: The menu controller.
    """

    ...

is_submenu

A method that checks if this action is a submenu action.

Returns:

  • bool

    True if this is a submenu action, False otherwise.

Source code in dev_tool/tui/menu/action.py
def is_submenu(self) -> bool:
    """
    A method that checks if this action is a submenu action.

    :return: True if this is a submenu action, False otherwise.
    """

    ...

get_action_type

A method that returns the type of action as an enum.

Returns:

Source code in dev_tool/tui/menu/action.py
def get_action_type(self) -> ActionType:
    """
    A method that returns the type of action as an enum.

    :return: The action type enum.
    """

    ...

CommandAction dataclass

A class for executing commands from the TUI menu.

This class runs a command function when selected from the menu.

command instance-attribute

run

A method that stores the command for execution after exiting cbreak mode.

Parameters:

Source code in dev_tool/tui/menu/action.py
def run(self, controller: MenuController) -> object:
    """
    A method that stores the command for execution after exiting cbreak mode.

    :param controller: The menu controller.
    """

    controller.running = False
    return self.command

is_submenu

A method that checks if this action is a submenu action.

Returns:

  • bool

    True if this is a submenu action, False otherwise.

Source code in dev_tool/tui/menu/action.py
def is_submenu(self) -> bool:
    """
    A method that checks if this action is a submenu action.

    :return: True if this is a submenu action, False otherwise.
    """

    return False

get_action_type

A method that returns the type of action as an enum.

Returns:

Source code in dev_tool/tui/menu/action.py
def get_action_type(self) -> ActionType:
    """
    A method that returns the type of action as an enum.

    :return: The action type enum.
    """

    return ActionType.COMMAND

DisplayAction dataclass

A class for displaying scrollable content in the TUI.

This class displays content within the TUI loop without exiting cbreak mode, allowing for scrolling and navigation back to the menu.

content instance-attribute

run

A method that displays the content in the TUI display view.

Parameters:

Source code in dev_tool/tui/menu/action.py
def run(self, controller: MenuController) -> None:
    """
    A method that displays the content in the TUI display view.

    :param controller: The menu controller.
    """

    controller.views.set_display_content(self.content())

is_submenu

A method that checks if this action is a submenu action.

Returns:

  • bool

    True if this is a submenu action, False otherwise.

Source code in dev_tool/tui/menu/action.py
def is_submenu(self) -> bool:
    """
    A method that checks if this action is a submenu action.

    :return: True if this is a submenu action, False otherwise.
    """

    return False

get_action_type

A method that returns the type of action as an enum.

Returns:

Source code in dev_tool/tui/menu/action.py
def get_action_type(self) -> ActionType:
    """
    A method that returns the type of action as an enum.

    :return: The action type enum.
    """

    return ActionType.DISPLAY

SubmenuAction dataclass

A class for opening submenus from the TUI menu.

This class navigates to a submenu when selected from the menu.

submenu instance-attribute

run

A method that opens the submenu.

Parameters:

Source code in dev_tool/tui/menu/action.py
def run(self, controller: MenuController) -> None:
    """
    A method that opens the submenu.

    :param controller: The menu controller.
    """

    menu = (
        self.submenu()
        if callable(self.submenu)
        else self.submenu
    )

    controller.ui.set_menu(menu)
    controller.ui.draw_menu()

is_submenu

A method that checks if this action is a submenu action.

Returns:

  • bool

    True if this is a submenu action, False otherwise.

Source code in dev_tool/tui/menu/action.py
def is_submenu(self) -> bool:
    """
    A method that checks if this action is a submenu action.

    :return: True if this is a submenu action, False otherwise.
    """

    return True

get_action_type

A method that returns the type of action as an enum.

Returns:

Source code in dev_tool/tui/menu/action.py
def get_action_type(self) -> ActionType:
    """
    A method that returns the type of action as an enum.

    :return: The action type enum.
    """

    return ActionType.MENU

ActionGroup

Bases: StrEnum

An enumeration of action groups for the TUI menu.

COMMAND = 'command' class-attribute instance-attribute

DISPLAY = 'display' class-attribute instance-attribute

SUBMENU = 'submenu' class-attribute instance-attribute

MenuFactory

A factory class for creating TUI menus.

This class provides static methods for creating different types of menus.

create_main_menu staticmethod

A method that creates the main menu.

Returns:

  • Menu

    The main menu.

Source code in dev_tool/tui/menu/factory.py
@staticmethod
def create_main_menu() -> Menu:
    """
    A method that creates the main menu.

    :return: The main menu.
    """

    tabs = {}

    for specification in REGISTRY.values():
        label = specification['label']
        title = specification['category']
        command = specification['bound']
        group = specification['group']
        order = specification['order']

        if title not in tabs:
            tabs[title] = []

        match group:
            case ActionGroup.COMMAND:
                action = CommandAction(command=command)
                item = MenuItem(label=label, action=action)
            case ActionGroup.SUBMENU:
                action = SubmenuAction(submenu=command)
                item = MenuItem(label=label, action=action)

        pair = (order, item)
        tabs[title].append(pair)

    for title, items in tabs.items():
        items.sort(key=lambda x: x[0])
        tabs[title] = [item for order, item in items]

    items: list[Menu | MenuItem] = [
        Menu(title=title, items=items)
        for title, items in tabs.items()
    ]

    return Menu(title='Main Menu', items=items)

Menu dataclass

A data class representing a TUI menu.

A menu contains a title and a list of menu items.

title instance-attribute

items instance-attribute

label property

A property that returns the menu title as the label.

Returns:

  • str

    The menu title.

__hash__

A method that generates a hash for the menu.

Returns:

  • int

    The hash value.

Source code in dev_tool/tui/menu/menu.py
def __hash__(self) -> int:
    """
    A method that generates a hash for the menu.

    :return: The hash value.
    """

    return hash(self.title)

get_items

A method that gets the items of this menu.

Returns:

Source code in dev_tool/tui/menu/menu.py
def get_items(self) -> list[MenuItem | Menu]:
    """
    A method that gets the items of this menu.

    :return: The list of items.
    """

    return self.items

get_element_type

A method that returns the type of element as a string.

Returns:

  • str

    The element type string.

Source code in dev_tool/tui/menu/menu.py
def get_element_type(self) -> str:
    """
    A method that returns the type of element as a string.

    :return: The element type string.
    """

    return ElementType.MENU

as_menu

A method that returns self as a Menu.

Returns:

  • Menu | None

    This Menu instance.

Source code in dev_tool/tui/menu/menu.py
def as_menu(self) -> Menu | None:
    """
    A method that returns self as a Menu.

    :return: This Menu instance.
    """

    return self

as_menu_item

A method that returns None since this is not a MenuItem.

Returns:

Source code in dev_tool/tui/menu/menu.py
def as_menu_item(self) -> MenuItem | None:
    """
    A method that returns None since this is not a MenuItem.

    :return: None.
    """

    return None

MenuItem dataclass

A data class representing a TUI menu item.

A menu item contains a label and an action to perform when selected.

label instance-attribute

action instance-attribute

__hash__

A method that generates a hash for the menu item.

Returns:

  • int

    The hash value.

Source code in dev_tool/tui/menu/menu.py
def __hash__(self) -> int:
    """
    A method that generates a hash for the menu item.

    :return: The hash value.
    """

    return hash(self.label)

get_items

A method that returns an empty list since menu items have no sub-items.

Returns:

Source code in dev_tool/tui/menu/menu.py
def get_items(self) -> list[MenuItem | Menu]:
    """
    A method that returns an empty list since menu items have no sub-items.

    :return: An empty list.
    """

    return []

get_element_type

A method that returns the type of element as a string.

Returns:

  • str

    The element type string based on the action.

Source code in dev_tool/tui/menu/menu.py
def get_element_type(self) -> str:
    """
    A method that returns the type of element as a string.

    :return: The element type string based on the action.
    """

    return self.action.get_action_type()

as_menu

A method that returns None since this is not a Menu.

Returns:

  • Menu | None

    None.

Source code in dev_tool/tui/menu/menu.py
def as_menu(self) -> Menu | None:
    """
    A method that returns None since this is not a Menu.

    :return: None.
    """

    return None

as_menu_item

A method that returns self as a MenuItem.

Returns:

  • MenuItem | None

    This MenuItem instance.

Source code in dev_tool/tui/menu/menu.py
def as_menu_item(self) -> MenuItem | None:
    """
    A method that returns self as a MenuItem.

    :return: This MenuItem instance.
    """

    return self