termbuf

package
v0.167.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Screen(b []byte, cols, rows int) []string

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.

func NewInputs added in v0.75.0

func NewInputs(ttl time.Duration) *Inputs

NewInputs returns an empty table that forgets a client after ttl.

func (*Inputs) Apply added in v0.75.0

func (in *Inputs) Apply(client string, seq uint64, write func() error) (uint64, error)

Apply calls write for a sequence newer than the client's last, and returns the client's last applied sequence afterwards. Sequence 0 applies nothing: it asks for the last. A failed write leaves the last where it was, so the retry is applied.

func (*Inputs) Last added in v0.75.0

func (in *Inputs) Last(client string) uint64

Last is the client's last applied sequence, 0 when it is not known.

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 NewRing

func NewRing(size int) *Ring

NewRing returns a ring that keeps the last size bytes.

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

func (r *Ring) Mode(n int) (on, seen bool)

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

func (r *Ring) Restore(prefix, held []byte, total uint64)

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

func (r *Ring) Since(offset uint64) ([]byte, bool)

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

func (r *Ring) Snapshot() []byte

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

func (r *Ring) SnapshotAt() ([]byte, uint64)

SnapshotAt is Snapshot and the offset it brings a terminal to, read together.

func (*Ring) Start added in v0.75.0

func (r *Ring) Start() uint64

Start is the offset of the oldest byte the ring still holds.

func (*Ring) Total added in v0.75.0

func (r *Ring) Total() uint64

Total is the offset one past the newest byte.

func (*Ring) Write

func (r *Ring) Write(p []byte)

Write appends output, dropping the oldest bytes past the ring's size.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL