Skip to content

interface

dev_tool.tui.stacktrace.interface

StacktraceInterface

A class for displaying scrollable stacktrace content in the TUI.

This class provides a dedicated view for viewing full stacktraces with scrolling support.

The constructor for the StacktraceInterface class.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance.

Source code in dev_tool/tui/stacktrace/interface.py
def __init__(self, terminal: Terminal) -> None:
    """
    The constructor for the StacktraceInterface class.

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

    self._cached_height: int = 0
    self._cached_lines: list[str] = []
    self._cached_width: int = 0
    self._header_style: str = ''
    self._needs_full_redraw: bool = True
    self._status_style: str = ''
    self.content: str | None = None
    self.copied = False
    self.offset = 0
    self.terminal = terminal

content = None instance-attribute

copied = False instance-attribute

offset = 0 instance-attribute

terminal = terminal instance-attribute

draw_stacktrace

A method that draws the stacktrace view with the current content.

Source code in dev_tool/tui/stacktrace/interface.py
def draw_stacktrace(self) -> None:
    """A method that draws the stacktrace view with the current content."""

    if not self.content:
        return

    resized = self._check_terminal_resize()
    full_redraw = self._needs_full_redraw or resized

    width = self.terminal.width
    output = []

    if full_redraw:
        output.append(self.terminal.home + self.terminal.clear)
        self._build_styles()
        self._needs_full_redraw = False

    if full_redraw or self.copied:
        header = ' Stacktrace (copied)' if self.copied else ' Stacktrace'
        header = self._pad_line(header, width)

        output.append(
            move(self.terminal, 0, 0) +
            self._header_style +
            header +
            self.terminal.normal
        )

    start_line = 2
    available_lines = self.terminal.height - start_line - 2
    content_width = min(120, width - 4)

    all_lines = self._get_wrapped_lines(content_width)
    total_lines = len(all_lines)
    maximum = max(0, total_lines - available_lines)

    self.offset = min(self.offset, maximum)
    self.offset = max(0, self.offset)

    end_index = min(total_lines, self.offset + available_lines)

    for i in range(available_lines):
        line_index = self.offset + i
        line_number = start_line + i

        if line_index < end_index:
            line = self._pad_line(all_lines[line_index], width)
        else:
            line = ' ' * width

        output.append(move(self.terminal, line_number, 0) + line)

    left_text = f' Line {self.offset + 1}-{end_index} of {total_lines}'
    right_text = ' Copied! ' if self.copied else ' c to copy | d or Backspace to close '

    padding = width - len(left_text) - len(right_text)
    status_text = left_text + (' ' * max(0, padding)) + right_text
    status_text = self._pad_line(status_text, width)

    status_line = self.terminal.height - 1

    output.append(
        move(self.terminal, status_line, 0) +
        self._status_style +
        status_text +
        self.terminal.normal
    )

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

handle_input

A method that handles input for the stacktrace interface.

Parameters:

  • key (str) –

    The key name.

Returns:

  • str | None

    The navigation result or None to continue.

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

    :param key: The key name.
    :return: The navigation result or None to continue.
    """

    match key:
        case Key.UP:
            self.offset = max(0, self.offset - 1)
            return MenuAction.REDRAW
        case Key.DOWN:
            self.offset = self.offset + 1
            return MenuAction.REDRAW
        case _ if key.lower() == Key.COPY:
            self.copied = self._copy_to_clipboard()
            return MenuAction.REDRAW
        case _ if key in (Key.BACKSPACE, Key.DELETE, Key.ESCAPE, Key.STACKTRACE):
            return MenuAction.MAIN_MENU
        case _ if key.lower() == Key.QUIT:
            return MenuAction.QUIT
        case _:
            return None

set_content

A method that sets the stacktrace content to display.

Parameters:

  • content (str) –

    The stacktrace string.

Source code in dev_tool/tui/stacktrace/interface.py
def set_content(self, content: str) -> None:
    """
    A method that sets the stacktrace content to display.

    :param content: The stacktrace string.
    """

    self._cached_lines = []
    self._needs_full_redraw = True
    self.content = content
    self.copied = False
    self.offset = 0