Documentation
¶
Overview ¶
Package termpreview is the presentation layer shared by every Sidecar surface that puts a list beside an embedded terminal.
It sits *beneath* the pane tree, not beside it. What lives here is the arithmetic the surfaces were doing inline — the outer sidebar/preview split, the one header row above every terminal, and the rendering of a pane's body into a box, captured or live.
"A second independent geometry computation" is the failure mode the doc-panes plan names, and it is guarded structurally rather than by keeping surfaces simple: both the project Workspaces plugin and the global Workspaces browser ("Sessions") instantiate a pane tree, and both place it with internal/panelayout and draw it with internal/paneframe. Nothing computes a pane's chrome, border, handle or hit box for itself.
Nothing here reads tmux, owns a buffer, or forwards input. A consumer hands in an immutable Snapshot, or a body some terminal component already rendered, and gets back rows of text.
Index ¶
- Constants
- func CanvasBackground(buffer *tty.OutputBuffer, paneTop, paneHeight int) string
- func CanvasRowShare(rows int) int
- func DrawRows(in RowsInput) []string
- func HeaderRow(chips []string, hints string, width, hintFloor int, ...) string
- func Letterboxed(paneHeight int, interactive, follow bool) bool
- func RenderBody(in RenderBufferInput) string
- func RenderBuffer(in RenderBufferInput) string
- func RenderHeader(in RenderBufferInput) string
- func TruncateANSI(value string, width int) string
- type Box
- type ChipPlacement
- type RenderBufferInput
- type RowsInput
- type Split
- type SplitConfig
- type Surface
Constants ¶
const ChipGap = 1
ChipGap is the columns between two chips, and the minimum gap between a header row's left and right regions.
const HeaderRows = 1
HeaderRows is the single row every embedded terminal reserves above its viewport for identity chips and hints. It is one row for every surface, so a terminal always begins on the row immediately below its box's first row.
Variables ¶
This section is empty.
Functions ¶
func CanvasBackground ¶
func CanvasBackground(buffer *tty.OutputBuffer, paneTop, paneHeight int) string
CanvasBackground recognizes the background carried across a substantial share of a fullscreen TUI's live rows. tmux renders later default-background cells correctly in a real terminal because that terminal's default matches the canvas. Inside Sidecar those cells otherwise fall through to the surrounding surface and form rectangular seams.
It is measured over the live grid, so a window with no pane behind it — a watched capture, a scrollback-only view — has no canvas to find.
func CanvasRowShare ¶
CanvasRowShare is how many of the observed rows a background must cover to be the pane's canvas rather than highlighting drawn on top of one.
A canvas is on every row by definition — it is the surface the application paints onto — so this is deliberately near-total rather than a simple majority. Measured against live panes: Grok's canvas covers 43 of 43 and 56 of 56 rows, while a Claude Code diff's added-line green covers 19 of 55. An earlier one-third rule sat directly between those two, so scrolling a long diff by a single row flipped it and repainted the whole pane green.
func DrawRows ¶
DrawRows renders the window RowsInput describes into drawn rows.
Every rule that decides what a row looks like lives here — carried backgrounds, tab stops, decoration, the selection highlight, horizontal offset, truncation, letterboxing and the pane's canvas background — because a surface that reimplements any one of them draws the same buffer differently from every other surface showing it.
It draws no cursor and no scrollbar: those are placed against the box a host puts these rows in, and a second answer here would contradict the first.
func HeaderRow ¶
func HeaderRow(chips []string, hints string, width, hintFloor int, truncate func(string, int) string) string
HeaderRow composes the one row above an embedded terminal: identity chips on the left, hints right-aligned on the right.
Truncation is deliberately asymmetric. The chips say what the surface is and carry the only hit regions on the row, so they are kept whole — a chip either fits or is dropped entirely, never clipped mid-chip — while the hints are advisory and give way first. The result is always exactly one row and never wider than width, so the terminal below can never lose a row to overflow.
hintFloor inverts that priority for the columns it names: the right region keeps at least that many, dropping chips to find them.
truncate is passed in rather than reached for so this stays a plain function over strings: the caller supplies its own ANSI-aware truncation cache.
func Letterboxed ¶
Letterboxed reports whether the drawn rows are padded out to the viewport.
A live grid wants it: tmux strips trailing blank rows, so a short capture would otherwise shift the chrome below it every time the pane emptied. A scrollback window does not — there is nothing below its last line. Passive follow and interactive answer the same because both show the live pane.
func RenderBody ¶ added in v0.99.0
func RenderBody(in RenderBufferInput) string
RenderBody draws the window of a terminal buffer that sits under the header row: exactly Height-HeaderRows rows of exactly Width columns, or nothing when the box has no row to spare below its header.
It renders no cursor. A host that owns one places it natively against the box this returns, using the same Layout — a cursor drawn here and a cursor placed there would be two answers to one question.
func RenderBuffer ¶
func RenderBuffer(in RenderBufferInput) string
RenderBuffer draws a whole embedded terminal box: RenderHeader over RenderBody. It is kept as their composition for the surfaces that want the box in one call, so splitting the two halves costs its callers nothing; a frame that owns the header row calls the halves instead.
func RenderHeader ¶ added in v0.99.0
func RenderHeader(in RenderBufferInput) string
RenderHeader draws the one row above an embedded terminal — identity chips left, hints right — filled to exactly Width columns.
It is separate from RenderBody because the header belongs to the frame that places the box, not to whatever the box is showing. A frame that draws every leaf's header itself is the only way every leaf's body can start on the same relative row, which is what HeaderRows states.
func TruncateANSI ¶
TruncateANSI is the default ANSI-safe truncation: no ellipsis, so a clipped terminal line looks clipped rather than annotated.
Types ¶
type Box ¶
Box is a rectangle in surface-local coordinates. It is the currency between the pane tree and this package: `LayoutPanes` produces boxes, and everything here consumes them.
It is an alias of mouse.Rect, not a separate struct: a box and a hit region are the same rectangle, so the layout that draws a pane and the hit map that receives its clicks cannot disagree about the geometry.
type ChipPlacement ¶
ChipPlacement is where one chip landed on a header row: Col is its first column, relative to the row's own first column, and Drawn is false for a chip the row had no columns for.
func HeaderRowSplit ¶ added in v1.0.0
func HeaderRowSplit(left, right []string, hints string, width, hintFloor int, truncate func(string, int) string) (string, []ChipPlacement)
HeaderRowSplit is HeaderRow with a second, right-aligned group of chips.
The row reads: identity chips left, then space, then the right chips, then the hints hard against the right edge. Action chips belong there because they are the same two buttons on every row — putting them immediately after a name of unpredictable length left them at a different column on every row, and nothing about them is part of the row's identity.
Priority under pressure is hints, then right chips, then left chips, and the first left chip is the row's name: it is truncated to make room rather than dropped, so a narrow row keeps a name and its buttons instead of losing both.
The returned placements are where the right chips landed, in columns relative to the row's first column. Hit regions must come from these rather than a second calculation: a right-aligned chip's column depends on the hints beside it, which only this function has seen.
func LayoutChips ¶
func LayoutChips(chips []string, width, hintFloor int) []ChipPlacement
LayoutChips places chips left to right in the columns a header row can give them, dropping whole chips rather than clipping one. It is the single authority on which chips a row drew and where: HeaderRow renders from it and hit regions are registered from it, so a chip dropped for want of columns cannot keep a live click target.
hintFloor is the columns the row's right region has claimed; zero leaves the chips the whole width.
type RenderBufferInput ¶
type RenderBufferInput struct {
Width, Height int
Chips []string
Hints string
Layout tty.Viewport
Buffer *tty.OutputBuffer
// AbsoluteBase lifts a window line index into the coordinates a selection is
// recorded in.
AbsoluteBase int
Selection *ui.SelectionState
// PaneHeight, Interactive and Follow describe the live grid behind the
// window, which is what decides letterboxing and what the pane's canvas
// background is measured over. A watched capture has no pane behind it.
PaneHeight int
Interactive bool
Follow bool
// TotalItems is the scrollbar's idea of how much there is to scroll through,
// which can exceed the loaded buffer while older history is still unfetched.
TotalItems int
TabWidth int
// Truncate is the consumer's ANSI-aware truncation cache; nil uses
// TruncateANSI.
Truncate func(string, int) string
// Message replaces the body when there is nothing to draw: no pane, an
// ambiguous match, a failed capture. Multi-line messages are rendered as
// written, so a caller can put the item's metadata under the reason.
Message string
// Decorate is the host's per-row decoration (links). Nil draws the row
// undecorated.
Decorate func(line string, absoluteLine int) string
}
RenderBufferInput describes one embedded terminal box: the header row's chips and hints, the box size, and the window of a captured buffer to draw in it.
Layout is passed in rather than computed here because it is the same value the host hit-tests against. A second derivation is how a click ends up landing on a different cell than the one the user aimed at.
type RowsInput ¶
type RowsInput struct {
Buffer *tty.OutputBuffer
Layout tty.Viewport
// AbsoluteBase lifts a window line index into the coordinates a selection,
// a search match and a link are all recorded in.
AbsoluteBase int
TabWidth int
Selection *ui.SelectionState
// Decorate is the host's own per-row decoration — links, search matches —
// applied after tab expansion and before the selection highlight, so a
// highlight always wins the cells it covers. Nil draws the row undecorated.
Decorate func(line string, absoluteLine int) string
// Truncate is the host's ANSI-aware truncation cache; nil uses TruncateANSI.
Truncate func(string, int) string
// PaneHeight, Interactive and Follow decide letterboxing and name the live
// grid the canvas background is measured over.
PaneHeight int
Interactive bool
Follow bool
// Pad right-pads every drawn row to the window width. A host that draws the
// rows into a filled box wants it; one that joins them against its own
// chrome does not.
Pad bool
}
RowsInput is one drawn window of a captured buffer: which lines, where they sit in the buffer's own coordinates, and the decoration each host adds on top.
type Split ¶
type Split struct {
SidebarWidth int // outer width of the sidebar panel; 0 when hidden
SidebarContentWidth int // sidebar width inside border + padding
PreviewX int // outer x of the preview panel
PreviewWidth int // outer width of the preview panel
ContentX int // x of the preview panel's first content column
ContentWidth int // preview width inside border + padding
}
Split is the horizontal split of a viewport into sidebar, divider, and preview panel, in surface-local columns.
func SplitFor ¶
func SplitFor(width int, cfg SplitConfig) Split
SplitFor computes the split for an explicit viewport width.
type SplitConfig ¶
type SplitConfig struct {
// SidebarVisible is false when the list is hidden and the preview owns the
// whole width.
SidebarVisible bool
// SidebarPercent is the requested sidebar share of the available columns.
SidebarPercent int
// DividerWidth is the columns between the two panes.
DividerWidth int
// PanelOverhead is the columns a panel's border and padding consume, and
// ContentInset the columns of that overhead on its left. A consumer that
// draws no panel chrome passes zero for both.
PanelOverhead int
ContentInset int
// SidebarMin / PreviewMin are the floors: the sidebar never shrinks past
// SidebarMin and never grows so far that the preview drops below PreviewMin.
SidebarMin, PreviewMin int
}
SplitConfig describes the outer two-pane split: a list on the left and a preview region on the right. It is deliberately the *outer* split only — how the preview region is subdivided afterwards (a pane tree in the project plugin, nothing at all in the global browser) is the consumer's business.
type Surface ¶
type Surface struct {
X int // first content column
Y int // first content row, one below HeaderY
// HeaderY is the row the chips-and-hints header is drawn on.
HeaderY int
Width int // terminal content columns
Height int // terminal content rows, header row excluded
OK bool
}
Surface locates one embedded terminal inside a box and reports the size of its viewport. It is the box minus its header row — no more, no less, so a caller cannot disagree with the layout about which row the terminal starts on.