Skip to content

TerminalView

Base classes

Name Children Inherits
Vertical
textual.containers
An expanding container with vertical layout and no scrollbars.

⋔ Inheritance diagram

graph TD
  107846721688992["terminal.TerminalView"]
  107846720594576["containers.Vertical"]
  107846718477920["widget.Widget"]
  107846718489888["dom.DOMNode"]
  107846718374032["message_pump.MessagePump"]
  137699722799616["builtins.object"]
  107846720594576 --> 107846721688992
  107846718477920 --> 107846720594576
  107846718489888 --> 107846718477920
  107846718374032 --> 107846718489888
  137699722799616 --> 107846718374032

🛈 DocStrings

Bases: Vertical

Command, live output and exit status of a terminal.

Source code in src/acptui/ui/widgets/terminal.py
class TerminalView(Vertical):
    """Command, live output and exit status of a terminal."""

    DEFAULT_CSS = """
    TerminalView {
        height: auto;
        border-left: outer $secondary;
        padding: 0 0 0 1;
        margin: 0 0 1 0;
        .terminal-header { height: 1; width: 1fr; }
        .terminal-output {
            height: auto;
            max-height: 16;
            background: $background-darken-1;
            scrollbar-size-vertical: 1;
        }
        .terminal-output > Static { height: auto; }
        Button { min-width: 10; height: 1; border: none; margin: 0 1; }
        #terminal-top { height: 1; }
    }
    """

    class CancelRequested(Message):
        """The user asked to cancel the terminal."""

        def __init__(self, terminal_id: str) -> None:
            super().__init__()
            self.terminal_id = terminal_id

    def __init__(self, terminal: TerminalState) -> None:
        super().__init__(classes=f"terminal-{terminal.terminal_id}")
        self.terminal = terminal
        self._dirty = False

    def compose(self) -> ComposeResult:
        from textual.containers import Horizontal

        with Horizontal(id="terminal-top"):
            yield Static(self._header(), classes="terminal-header")
            yield Button("cancel", variant="warning", compact=True, id="terminal-cancel")
        with VerticalScroll(classes="terminal-output"):
            yield Static(terminal_text(bytes(self.terminal.output)), id="terminal-text")

    def on_mount(self) -> None:
        self.query_one(".terminal-output", VerticalScroll).anchor()
        self._sync_cancel()
        self.set_interval(REFRESH_INTERVAL, self._flush)

    def _header(self) -> Content:
        terminal = self.terminal
        command = Content.assemble(("$ ", "bold $secondary"), (terminal.command or "…", "bold"))
        status = terminal.exit_status
        if status is None:
            badge = pill("running", "$accent-muted", "$text-accent")
        else:
            code = status.exit_code if is_set(status.exit_code) else None
            signal = status.signal if is_set(status.signal) else None
            if signal:
                badge = pill(f"signal {signal}", "$error-muted", "$text-error")
            elif code in {0, None}:
                badge = pill("exit 0" if code == 0 else "exited", "$success-muted", "$text-success")
            else:
                badge = pill(f"exit {code}", "$error-muted", "$text-error")
        return Content.assemble(command, "  ", badge)

    def _sync_cancel(self) -> None:
        button = self.query_one("#terminal-cancel", Button)
        button.display = self.terminal.can_cancel and not self.terminal.exited

    def refresh_terminal(self) -> None:
        """Schedule a redraw (output is redrawn at most every :data:`REFRESH_INTERVAL`)."""
        self._dirty = True

    def _flush(self) -> None:
        if not self._dirty or not self.is_mounted:
            return
        self._dirty = False
        self.query_one(".terminal-header", Static).update(self._header())
        self.query_one("#terminal-text", Static).update(terminal_text(bytes(self.terminal.output)))
        self._sync_cancel()

    def on_button_pressed(self, event: Button.Pressed) -> None:
        event.stop()
        self.post_message(self.CancelRequested(self.terminal.terminal_id))

CancelRequested

Bases: Message

The user asked to cancel the terminal.

Source code in src/acptui/ui/widgets/terminal.py
class CancelRequested(Message):
    """The user asked to cancel the terminal."""

    def __init__(self, terminal_id: str) -> None:
        super().__init__()
        self.terminal_id = terminal_id

refresh_terminal

refresh_terminal() -> None

Schedule a redraw (output is redrawn at most every :data:REFRESH_INTERVAL).

Source code in src/acptui/ui/widgets/terminal.py
def refresh_terminal(self) -> None:
    """Schedule a redraw (output is redrawn at most every :data:`REFRESH_INTERVAL`)."""
    self._dirty = True

:fa-brands-github: Show source on GitHub