Skip to content

menu

dev_tool.launcher.menu

LauncherMenu

The constructor for the LauncherMenu class.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance.

Source code in dev_tool/launcher/menu.py
def __init__(self, terminal: Terminal):
    """
    The constructor for the LauncherMenu class.

    :param terminal: The blessed Terminal instance.
    """

    self.index = 0
    self.options = [
        ('Retry', UserResponse.RETRY),
        ('Quit', UserResponse.QUIT),
        ('Terminate', UserResponse.TERMINATE)
    ]
    self.terminal = terminal

index = 0 instance-attribute

options = [('Retry', UserResponse.RETRY), ('Quit', UserResponse.QUIT), ('Terminate', UserResponse.TERMINATE)] instance-attribute

terminal = terminal instance-attribute

draw_menu

A method that draws the retry menu.

Source code in dev_tool/launcher/menu.py
def draw_menu(self) -> None:
    """A method that draws the retry menu."""

    output = []
    output.append(self.terminal.home + self.terminal.clear)

    header = ' Application Error'
    background = ' ' * self.terminal.width

    output.append(
        self.terminal.move(0, 0) +
        self.terminal.on_color_rgb(43, 40, 59) +
        self.terminal.bold +
        self.terminal.color_rgb(255, 255, 255) +
        background +
        self.terminal.normal
    )

    output.append(
        self.terminal.move(0, 0) +
        self.terminal.on_color_rgb(43, 40, 59) +
        self.terminal.bold +
        self.terminal.color_rgb(255, 255, 255) +
        header +
        self.terminal.normal
    )

    output.append(
        self.terminal.move(2, 4) +
        'The application encountered an error. What would you like to do?'
    )

    for index, (label, _) in enumerate(self.options):
        line = 4 + index
        text = f'  {label}'

        if index == self.index:
            output.append(
                self.terminal.move(line, 4) +
                self.terminal.on_color_rgb(230, 200, 255) +
                self.terminal.color_rgb(0, 0, 0) +
                text +
                ' ' * (20 - len(text)) +
                self.terminal.normal
            )
        else:
            output.append(
                self.terminal.move(line, 4) +
                text
            )

    status_bg = ' ' * self.terminal.width
    status_line = self.terminal.height - 1

    output.append(
        self.terminal.move(status_line, 0) +
        self.terminal.on_color_rgb(70, 68, 80) +
        self.terminal.color_rgb(255, 255, 255) +
        status_bg +
        self.terminal.normal
    )

    print(''.join(output), end='', flush=True)  # noqa: T201

handle_input

A method that handles input for the menu.

Parameters:

  • key (str) –

    The key name.

Returns:

  • UserResponse | None

    The selected response or None to continue.

Source code in dev_tool/launcher/menu.py
def handle_input(self, key: str) -> UserResponse | None:
    """
    A method that handles input for the menu.

    :param key: The key name.
    :return: The selected response or None to continue.
    """

    if key == Key.UP:
        self.index = (self.index - 1) % len(self.options)
        return None

    if key == Key.DOWN:
        self.index = (self.index + 1) % len(self.options)
        return None

    if key in (Key.ENTER, Key.LINE_FEED, Key.CARRIAGE_RETURN):
        _, response = self.options[self.index]
        return response

    if key.lower() == Key.QUIT:
        return UserResponse.QUIT

    return None

run

A method that runs the menu loop.

Returns:

Source code in dev_tool/launcher/menu.py
def run(self) -> UserResponse | None:
    """
    A method that runs the menu loop.

    :return: The selected user response.
    """

    result: UserResponse | None = None

    with self.terminal.fullscreen(), self.terminal.cbreak(), self.terminal.hidden_cursor():
        self.draw_menu()

        while result is None:
            key = self.terminal.inkey()

            if key:
                result = self.handle_input(key.name or key)

                if result is None:
                    self.draw_menu()

    return result