Skip to content

ShellCommand

🛈 DocStrings

One command the user ran, with its output and (once finished) exit code.

Source code in src/acptui/shell.py
@dataclass(eq=False)
class ShellCommand:
    """One command the user ran, with its output and (once finished) exit code."""

    command: str
    cwd: str
    """Working directory the command started in."""
    output: bytearray = field(default_factory=bytearray)
    exit_code: int | None = None
    """``None`` while running; ``-1`` if the shell went away."""

    @property
    def done(self) -> bool:
        """Whether the command finished."""
        return self.exit_code is not None

    def append(self, data: bytes) -> None:
        """Add output, keeping at most :data:`MAX_OUTPUT_BYTES`."""
        self.output.extend(data)
        if len(self.output) > MAX_OUTPUT_BYTES:
            del self.output[: len(self.output) - MAX_OUTPUT_BYTES]

    @property
    def display_output(self) -> bytes:
        """The output with colors, without other control sequences (for display)."""
        return clean_output(bytes(self.output))

    def context_text(self) -> str:
        """The command and its output as plain text for the agent (long output cut)."""
        text = plain_text(bytes(self.output))
        lines = text.splitlines()
        if len(lines) > MAX_CONTEXT_LINES:
            head, tail = MAX_CONTEXT_LINES // 5, MAX_CONTEXT_LINES - MAX_CONTEXT_LINES // 5
            omitted = len(lines) - head - tail
            lines = [*lines[:head], f"[... {omitted} lines omitted ...]", *lines[-tail:]]
        body = "\n".join(lines)
        if len(body) > MAX_CONTEXT_CHARS:
            body = (
                f"[... output cut to its last {MAX_CONTEXT_CHARS} characters ...]\n"
                + (body[-MAX_CONTEXT_CHARS:])
            )
        status = "still running" if self.exit_code is None else f"exit code {self.exit_code}"
        return f"$ {self.command}\n({status}, in {self.cwd})\n\n{body}\n"

cwd instance-attribute

cwd: str

Working directory the command started in.

display_output property

display_output: bytes

The output with colors, without other control sequences (for display).

done property

done: bool

Whether the command finished.

exit_code class-attribute instance-attribute

exit_code: int | None = None

None while running; -1 if the shell went away.

append

append(data: bytes) -> None

Add output, keeping at most :data:MAX_OUTPUT_BYTES.

Source code in src/acptui/shell.py
def append(self, data: bytes) -> None:
    """Add output, keeping at most :data:`MAX_OUTPUT_BYTES`."""
    self.output.extend(data)
    if len(self.output) > MAX_OUTPUT_BYTES:
        del self.output[: len(self.output) - MAX_OUTPUT_BYTES]

context_text

context_text() -> str

The command and its output as plain text for the agent (long output cut).

Source code in src/acptui/shell.py
def context_text(self) -> str:
    """The command and its output as plain text for the agent (long output cut)."""
    text = plain_text(bytes(self.output))
    lines = text.splitlines()
    if len(lines) > MAX_CONTEXT_LINES:
        head, tail = MAX_CONTEXT_LINES // 5, MAX_CONTEXT_LINES - MAX_CONTEXT_LINES // 5
        omitted = len(lines) - head - tail
        lines = [*lines[:head], f"[... {omitted} lines omitted ...]", *lines[-tail:]]
    body = "\n".join(lines)
    if len(body) > MAX_CONTEXT_CHARS:
        body = (
            f"[... output cut to its last {MAX_CONTEXT_CHARS} characters ...]\n"
            + (body[-MAX_CONTEXT_CHARS:])
        )
    status = "still running" if self.exit_code is None else f"exit code {self.exit_code}"
    return f"$ {self.command}\n({status}, in {self.cwd})\n\n{body}\n"

:fa-brands-github: Show source on GitHub