Skip to content

item

dev_tool.tui.components.item

ItemComponent

A component for rendering the menu items.

The constructor for the ItemComponent class.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance.

  • theme (Default) –

    The theme instance for styling.

  • state (StateManager) –

    The state manager instance.

Source code in dev_tool/tui/components/item.py
def __init__(self, terminal: Terminal, theme: Default, state: StateManager) -> None:
    """
    The constructor for the ItemComponent class.

    :param terminal: The blessed Terminal instance.
    :param theme: The theme instance for styling.
    :param state: The state manager instance.
    """

    self.notification = NotificationDisplay(terminal)
    self.state = state
    self.terminal = terminal
    self.theme = theme

notification = NotificationDisplay(terminal) instance-attribute

state = state instance-attribute

terminal = terminal instance-attribute

theme = theme instance-attribute

draw

A method that draws the menu items.

Parameters:

  • output (list) –

    The list to append terminal output strings to.

Source code in dev_tool/tui/components/item.py
def draw(self, output: list) -> None:
    """
    A method that draws the menu items.

    :param output: The list to append terminal output strings to.
    """

    lines = self.notification.get_notification_lines()
    length = len(lines)

    if self.state.is_root_with_tabs():
        start = 5 + length
        selected = self.state.menu.items[self.state.index]
        items = selected.get_items()
    else:
        start = (5 if self.state.is_tabs() else 4) + length
        items = self.state.menu.items

    available = self.terminal.height - start - 2
    total = len(items)

    if not items:
        message = '    No item(s) available'
        line = start
        output.append(self.terminal.move(line, 0) + self.theme.command_text(message))
        return

    self.state.offset = min(self.state.row, self.state.offset)

    if self.state.row >= self.state.offset + available:
        self.state.offset = self.state.row - available + 1

    minimum = min(total, self.state.offset + available)

    for index in range(self.state.offset, minimum):
        item = items[index]

        if item.as_menu():
            continue

        line = start + (index - self.state.offset)
        selected = index == self.state.row
        menuitem = item.as_menu_item()

        if menuitem:
            submenu = menuitem.action.is_submenu()

            suffix = ' ↵' if submenu else ''
            text = f'    {menuitem.label}{suffix}'

            if selected and submenu:
                background = ' ' * self.terminal.width
                output.append(self.terminal.move(line, 0) + self.theme.selected_menu_background(background))
                output.append(self.terminal.move(line, 0) + self.theme.selected_menu_background(text))

            if selected and not submenu:
                background = ' ' * self.terminal.width
                output.append(self.terminal.move(line, 0) + self.theme.selected_command_background(background))
                output.append(self.terminal.move(line, 0) + self.theme.selected_command_background(text))

            if not selected and submenu:
                output.append(self.terminal.move(line, 0) + self.theme.menu_text(text))

            if not selected and not submenu:
                output.append(self.terminal.move(line, 0) + self.theme.command_text(text))