Documentation
¶
Overview ¶
Package termbuf holds the recent output of a terminal: the last bytes it printed plus the terminal modes that output set, so a terminal that attaches late can be put back into the state the program expects.
Two places keep one. The daemon keeps one per owned session for the browser tabs that connect to it, and the pty-host process (internal/ptyhost) keeps one for the daemon itself — after a daemon restart, that buffer is the only record of what the screen looked like.
Every byte has an offset: the count of bytes the session printed before it. A client that knows how far it got asks for what came after (terminal protocol v2) instead of repainting the whole screen.
Index ¶
- Constants
- func Screen(b []byte, cols, rows int) []string
- type Inputs
- type Ring
- func (r *Ring) Changed() <-chan struct{}
- func (r *Ring) Mode(n int) (on, seen bool)
- func (r *Ring) Restore(prefix, held []byte, total uint64)
- func (r *Ring) Since(offset uint64) ([]byte, bool)
- func (r *Ring) Snapshot() []byte
- func (r *Ring) SnapshotAt() ([]byte, uint64)
- func (r *Ring) Start() uint64
- func (r *Ring) Total() uint64
- func (r *Ring) Write(p []byte)
Constants ¶
const InputTTL = 120 * time.Second
InputTTL is how long the last applied sequence of a client is remembered after it was last heard from. A client that comes back later starts afresh.
Variables ¶
This section is empty.
Functions ¶
func Screen ¶ added in v0.78.2
Screen replays terminal output onto a cols×rows grid and returns the text of each row, trailing blanks trimmed: what a terminal fed the same bytes would show now.
It is the smallest emulator that reads what a TUI drew, not a terminal: it follows printing, line feeds, carriage returns, the cursor moves and erases a renderer uses (absolute and relative, in either screen buffer), and ignores colours, modes, titles and anything else that does not move text. Claude Code's renderer writes a line as words between cursor moves (`Do\x1b[5Gyou\x1b[9Gwant`), so reading the screen without placing the cursor is reading it wrong.
The bytes need not start at a clean boundary — a ring that dropped its oldest bytes starts mid-sequence — and the result is still right for whatever the output drew after that, which for a dialog on the screen now is the part that matters. cols or rows ≤ 0 pick a wide default.
Types ¶
type Inputs ¶ added in v0.75.0
type Inputs struct {
// contains filtered or unexported fields
}
Inputs applies sequenced input exactly once per client (terminal protocol v2). A client numbers what it types; a retry after a dropped connection resends what was not acknowledged, and anything at or below the highest sequence already applied for that client is dropped instead of typed twice.
The pty-host keeps one, so the guarantee holds across a daemon restart; the daemon keeps one for a session it holds itself.
type Ring ¶
type Ring struct {
// contains filtered or unexported fields
}
Ring is a fixed-size byte buffer holding the most recent terminal output, so a terminal that connects late still sees recent scrollback — and the terminal modes that output set, which may have long since scrolled out of it.
The bytes live in one array allocated up front and written in a circle: a write copies into it, wrapping at the end, and allocates nothing. Only a reader that wants the bytes in one piece (Since, SnapshotAt) gets a fresh slice, put together from the up to two runs the held bytes make.
func (*Ring) Changed ¶ added in v0.75.0
func (r *Ring) Changed() <-chan struct{}
Changed returns a channel closed at the next write (or restore). Take it before reading, so a write in between is never missed.
func (*Ring) Mode ¶ added in v0.132.0
Mode reports the last value the output set for one of the tracked DEC private modes (trackedModes), and whether the output has set it at all. The daemon reads 2004 before it types a message into a session: an app that turned bracketed paste on is sent the message as a paste. It reads the held bytes on each call, which is rare, rather than every write twice.
func (*Ring) Restore ¶ added in v0.75.0
Restore replaces the ring's contents with another ring's: its mode prefix, the bytes it held, and the offset one past them. The daemon does this with the pty-host's snapshot, so offsets keep counting across a daemon restart.
func (*Ring) Since ¶ added in v0.75.0
Since returns a copy of every byte from offset on, and false when the ring no longer holds the byte at offset (or never had it: an offset past the end belongs to some other stream).
func (*Ring) Snapshot ¶
Snapshot is what a terminal attaching now should be sent: the modes in force where the scrollback starts, then the scrollback.
func (*Ring) SnapshotAt ¶ added in v0.75.0
SnapshotAt is Snapshot and the offset it brings a terminal to, read together.