A mutable grid of Cells covering area, the in-memory render target for one frame.
x/y arguments are absolute terminal coordinates (the same space as area's offset), not area-relative — widgets receive a Rect positioned in absolute space and write to the buffer at those coordinates. Writes outside area are silently clipped, never errors; reads outside area return Cell.Empty.
Mutability is an implementation detail of the render loop — it never escapes through Widget.render's contract.
'''Not thread-safe.''' The grid is a plain array with no synchronization, so a Buffer is owned by whichever thread renders into it — in a running application that is the runner's render thread, the same thread Signal writes are pinned to. Staging content on a background thread is safe only if the buffer is fully written before the reference is published to the render thread (RenderThread.capture in tui-runtime is how background work hands its continuation back) and is never touched again afterwards. Concurrent writes, or reads racing a write, produce a torn frame or a dropped diff rather than an error.
Attributes
- Companion
- object
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
Members list
Value members
Concrete methods
Copies region of source into this buffer with the region's top-left landing at at.
Copies region of source into this buffer with the region's top-left landing at at.
Writes outside this buffer's area are clipped like any other write — this is how offscreen-rendered content (scroll views, overlays) lands on the frame. A region reaching past source's own area is trimmed to it, and the landing point moves by however much the trim shifted the region's origin, so the surviving cells keep their position relative to at instead of sliding onto the ones that were dropped.
A cell's DiffDirective travels with it, so a fragment that reserved columns for an out-of-band painter keeps that reservation once it is composited into the frame.
A two-column grapheme that the window or the destination edge cuts in half follows the rule set and fill use for a glyph with no second column, not the one setString uses: the half is written as a blank in the glyph's own style, never skipped and never blanked in Style.Default. A text writer owns no background, so it can leave a half-fitting cluster's column alone; a blit carries whole cells, and dropping the style would punch a default-coloured hole into the edge of whatever coloured panel is being scrolled.
Blitting a buffer onto itself works on a snapshot: without it, overlapping rows would read cells this call has already overwritten and smear them across the region.
Both buffers must belong to the calling thread for the duration of the call: source is read cell by cell with no synchronization, so another thread writing into it mid-blit copies half of one frame and half of the next.
Attributes
Copies all of source into this buffer at at.
Copies all of source into this buffer at at.
Attributes
Overwrites this buffer's contents with source's, allocating nothing.
Overwrites this buffer's contents with source's, allocating nothing.
The in-place counterpart of snapshot. A backend that keeps a baseline of the frame it last flushed needs a private copy of it, and taking that copy with snapshot allocates a fresh Buffer plus two grid-sized arrays on every frame — 10 000 cells on a 200x50 screen, at the tick rate. Recycling one buffer with this method gives the same private copy and allocates nothing at all.
Both buffers must cover the same area and be owned by the calling thread, for the same reason snapshot must be taken on the owning thread: the copy is unsynchronized, so another thread writing into source part-way through leaves this buffer holding half of one frame and half of the next. Copying a buffer onto itself is a no-op rather than an error.
Attributes
- Throws
-
IllegalArgumentException
if
source.area != area. That is a programmer error, not a condition to recover from: the grids have different shapes, so there is no correct thing to copy. A caller that can be handed a differently sized buffer — anything that survives a terminal resize — must allocate a new buffer for the new area instead.
Whether something was drawn over placement after it was placed: any of its cells no longer holds what it held at that moment. A placement this buffer does not hold whole counts as covered.
Whether something was drawn over placement after it was placed: any of its cells no longer holds what it held at that moment. A placement this buffer does not hold whole counts as covered.
Attributes
The cells that changed going from this buffer (the previous frame) to next (the frame to display).
The cells that changed going from this buffer (the previous frame) to next (the frame to display).
This is what a terminal backend flushes each frame instead of redrawing everything. Positions covered by the continuation cell of a wide grapheme in next are never emitted — flushing the wide cell itself repaints both columns. The two buffers must start at the same origin and be the same width; see emitAll for the frame after a resize, where the previous frame is unusable and there is nothing to compare against.
One column is emitted even when its content did not change: a column that was the right half of a wide grapheme in this (previous) frame, is not one in next, and whose grapheme painted a style that shows through a blank — see visibleOnBlank. Both frames hold Cell.Empty there, so a plain content compare calls it unchanged, yet the terminal is still painting the old glyph's background across that half-cell.
next's DiffDirectives override the content comparison, and are read from next alone — the request belongs to the frame being drawn, not to the one being replaced. A DiffDirective.Skip position is never emitted; a DiffDirective.AlwaysUpdate one always is; and a position next no longer skips is emitted even if its content matches, because while it was skipped this buffer's memory of it was never flushed to the terminal at all.
Attributes
diff without the intermediate objects: calls emit(x, y, cell) for each changed cell, in row-major order.
diff without the intermediate objects: calls emit(x, y, cell) for each changed cell, in row-major order.
This is what backends use on the hot path — it allocates nothing per cell (no Position, no tuple, no iterator state), which matters because a 200x50 frame is 10 000 cells and runs at the tick rate. The walk itself is FrameDiff's.
Attributes
diff with a workaround for terminals that draw an emoji presentation sequence in one column.
diff with a workaround for terminals that draw an emoji presentation sequence in one column.
A cluster containing U+FE0F — the variation selector that asks for a character's colourful emoji form — is two columns wide by the Unicode rules this toolkit measures with, so the buffer reserves the column to its right as a continuation and never emits that column: painting the glyph paints both halves. Several terminals draw such a sequence in a single column anyway, and then the second column is never repainted at all, so whatever stood there in an earlier frame stays on screen next to the emoji.
With TrailingCellPolicy.Clear, a changed cell holding such a cluster is followed by a blank emitted into its reserved column, carrying the owning cell's style so a background fill stays continuous across the pair.
TrailingCellPolicy.Keep is the default, and picking between the two is a backend's decision rather than the buffer's, because the opposite artifact is just as real: on a terminal that does draw both columns, that blank lands on the right half of the glyph and clips it. Only the code that knows which terminal it is talking to can pick the lesser of the two.
The extra emission costs one enum test per changed cell — the reserved-column flag is an array read and is checked before the cluster is scanned for the selector — so a frame with no emoji in it pays nothing measurable.
Attributes
Drops the placements keep rejects, handing their cells back to ordinary diffing. The partner of covers for a backend that cannot draw a picture, or must take one down while something covers it.
Drops the placements keep rejects, handing their cells back to ordinary diffing. The partner of covers for a backend that cannot draw a picture, or must take one down while something covers it.
Attributes
Emits every cell of this buffer, in the same row-major order and with the same continuation rule as diff — the full repaint the first frame after a resize or a resume from suspend needs.
Emits every cell of this buffer, in the same row-major order and with the same continuation rule as diff — the full repaint the first frame after a resize or a resume from suspend needs.
This is the honest way to spell "there is no usable previous frame". diff used to answer that case itself, by quietly emitting everything whenever the two areas disagreed, which meant a caller who really had made a mistake — diffing an off-screen staging buffer against the frame, say — got a silent full repaint every frame instead of an error. Splitting the two makes the resize path something a test can name, and turns the mistake back into one.
The columns holding the right half of a two-column grapheme are skipped here as well: emitting the grapheme itself repaints both of its columns, and a terminal handed the continuation cell would draw a stray blank over the half already painted. So are the positions marked DiffDirective.Skip — a resize is exactly the moment a blind full repaint would erase an image somebody else painted, so the owner of such a region redraws it rather than this buffer.
Attributes
Content equality: the same area, the same Cell at every position, and the same continuation flags.
Content equality: the same area, the same Cell at every position, and the same continuation flags.
The continuation flags are part of the comparison because they are part of what gets drawn: two buffers whose cells all match but whose flags differ disagree about which columns a terminal is allowed to paint, and they produce different diffs.
'''A Buffer is mutable''', so this answer — and hashCode with it — changes as a frame is rendered into the buffer. Compare or hash one only while nothing is writing to it, and never use one as a key in a hash map: the key would move out from under the map on the next set.
Attributes
- Definition Classes
-
Any
Writes cell into every column of region that falls inside area; anything outside is clipped away, exactly as set clips a single write.
Writes cell into every column of region that falls inside area; anything outside is clipped away, exactly as set clips a single write.
This is the one owner of "blank a box". An overlay — a dialog, a popup, a menu — has to paint its whole rectangle in its own style so that whatever was drawn underneath cannot show through, and before this method every such widget wrote its own pair of while loops to do it, which is how one ends up a column short on one edge.
The cell's display width is measured once for the whole region instead of once per column, and a two-column symbol advances two columns at a time so that the continuation cell set reserves is not immediately painted over. If the region's width is not a whole multiple of that symbol's width, the leftover column at the right edge is stored as a blank in cell's style — that is set's existing rule for a wide symbol with no room to its right, and it keeps a background fill continuous to the edge.
Attributes
Applies visit(x, y, cell) to every cell of region that this buffer covers, in row-major order.
Applies visit(x, y, cell) to every cell of region that this buffer covers, in row-major order.
The traversal every reader of a finished frame — a golden-frame writer, a test assertion, an alternative encoder — would otherwise write for itself as a nested loop over get. Two reasons to have it here rather than there: the bounds check happens once, on region, instead of once per cell; and nothing is allocated per cell, no Position and no tuple, which matters because a 200x50 frame is 10 000 cells.
visit sees the raw grid, continuation cells included — they arrive as the Cell.Empty they hold. A caller rebuilding what a terminal displays must skip them with isContinuation rather than print their blank, or a row containing a two-column grapheme comes out one column too wide.
Writing into this buffer from inside visit is not supported: a write can move a neighbouring cell (see set) and the traversal has already decided which positions it will read.
Attributes
The cell at (x, y), or Cell.Empty when the coordinates fall outside area.
The cell at position, or Cell.Empty when it falls outside area.
The cell at position, or Cell.Empty when it falls outside area.
The same read as get(x, y), spelled for callers that already hold a Position — a mouse event's coordinate, a Rect's position, an entry from diff — so they need not unpack it into two arguments.
Attributes
The pictures placed into this frame by placeGraphic, in placement order.
Hashes the area and every cell. See equals for why a mutable buffer must not be used as a hash-map key.
Hashes the area and every cell. See equals for why a mutable buffer must not be used as a hash-map key.
Attributes
- Definition Classes
-
Any
Whether (x, y) is the second column of a two-column grapheme — the half a terminal paints when it draws the cluster in the cell to the left, which therefore must not be drawn a second time.
Whether (x, y) is the second column of a two-column grapheme — the half a terminal paints when it draws the cluster in the cell to the left, which therefore must not be drawn a second time.
The state is recorded by set, never re-measured here; see continuations for why measuring it was a bug. Coordinates outside area are never continuations.
Attributes
setStyle with the replacement derived from each cell's current style — a dim, a tint, a background swap that keeps the foreground it finds.
setStyle with the replacement derived from each cell's current style — a dim, a tint, a background swap that keeps the foreground it finds.
transform must be a pure function of the style it is handed, because it is not called once per cell: the pass runs through a MemoizedStyleTransform, so the transform re-runs only where the style actually changes. Without that, a full-frame pass would build a fresh Style for each of ten thousand cells to arrive at the same answer ten thousand times.
Attributes
A new buffer covering the union of this buffer's area and other's, holding this buffer's content with other's composited on top.
A new buffer covering the union of this buffer's area and other's, holding this buffer's content with other's composited on top.
The difference from blit is who decides the size. blit writes into a destination whose bounds the caller already fixed and throws away whatever falls outside them; this grows the result to the smallest rectangle covering both inputs, so nothing is lost. That is the operation for composing offscreen fragments whose extent is only known once they have been produced — a tooltip measured from its own text, say, laid over a panel.
Neither input is modified, and neither is resized: a Buffer's area is fixed for its lifetime because snapshot, diff and a backend's last-flushed frame all assume the grid they hold cannot change size underneath them. The result is a third buffer, owned by the caller.
Coordinates are absolute (see the class documentation), so each input lands at the position it already claims. Cells of the union that neither input covers stay Cell.Empty. Where the two overlap, other wins, and it wins as a whole grapheme: the copy goes through blit, so a two-column cluster cut by a seam is blanked rather than drawn torn.
Both buffers must belong to the calling thread for the duration of the call, for the same reason blit does.
Attributes
Asks an out-of-band painter to draw image scaled into region, and reserves region for it.
Asks an out-of-band painter to draw image scaled into region, and reserves region for it.
The cells of region are marked DiffDirective.Skip and the placement is recorded in graphics together with what those cells hold now — so draw the text fallback first and place the picture last: a later write over the region reads as the picture being covered (see covers). A region this buffer holds only part of — a picture in a scroll view's content, rendered into a buffer that holds just the window — is placed as a crop of the whole box (GraphicPlacement.crop), because the picture squeezed into the visible part would be drawn distorted; a region wholly outside the buffer is not placed.
Like directives, placements belong to the frame: reset forgets them, so a widget re-places its picture on every render. Whether anything is drawn is up to the backend — see GraphicPlacement. payload is the picture already encoded for an image protocol, when the caller encoded it ahead of time; see GraphicPayload.
Attributes
Resets every cell to Cell.Empty, recycling the buffer for the next frame.
Writes cell at position, with exactly the semantics of set(x, y, cell) including the clipping and the wide-grapheme bookkeeping described there.
Writes cell at position, with exactly the semantics of set(x, y, cell) including the clipping and the wide-grapheme bookkeeping described there.
Attributes
Writes cell at (x, y); writes outside area are silently clipped.
Writes cell at (x, y); writes outside area are silently clipped.
A write can also mutate the neighbouring cell, because a two-column grapheme owns two cells and a terminal cannot draw half of one: writing a wide cell reserves the cell to its right as its continuation; overwriting a wide cell releases the continuation it held; and writing over a continuation blanks the wide grapheme that owned it, since that grapheme can no longer draw across a column somebody else has claimed.
That last rule holds for every cell written, Cell.Empty included. A blank used to be exempt, on the theory that a caller writing one onto a reserved column was re-spelling the continuation the buffer had just created for it. Nothing in the toolkit spells a continuation that way — blit is the only caller that ever copies one, and it skips those columns because the wide cell it copied a moment earlier already reserved them — while the exemption made the commonest blank of all unable to erase. fill(region, Cell.Empty) and a default-styled overlay composited over CJK text both left the character on screen, one column inside the rectangle they had declared blank.
A wide cell aimed at the last column of a row is stored as a blank in cell's style rather than as itself: there is no column left to reserve, and a terminal handed a two-column glyph in a one-column slot wraps the row. The style is kept so a background fill stays continuous to the edge.
A cell whose symbol is the empty string is stored as the same styled blank. Cell.isBlank already says that is what it means; stored verbatim it would be a zero-width cell no backend ever paints, so the terminal kept the previous glyph in a column the buffer — and every assertion over it — called blank.
Attributes
Declares how diff must treat every position of region that falls inside area; the rest is clipped away, exactly as set clips a single write. A single cell is Rect(x, y, 1, 1).
Declares how diff must treat every position of region that falls inside area; the rest is clipped away, exactly as set clips a single write. A single cell is Rect(x, y, 1, 1).
This changes what gets flushed, never what the grid holds: the cells keep whatever set put in them. See DiffDirective for what each value means and why the two non-default ones are mutually exclusive.
Directives do not survive reset, so a widget that owns a region re-declares it on every render. That is the behaviour a frame loop wants: the frame that stops drawing an image is the frame whose cells must be flushed again, and a directive that outlived the widget that asked for it would freeze the region for the rest of the run. They are carried by snapshot and copyFrom, because those produce a record of the frame that was flushed, and the diff of the next frame against that record is exactly where the directives are read.
Attributes
Writes line's spans left to right from (x, y), sharing one budget of maxWidth columns between them, and answers how many columns were written in total.
Writes line's spans left to right from (x, y), sharing one budget of maxWidth columns between them, and answers how many columns were written in total.
Three style layers resolve here, in the order Line.styledGraphemes defines: baseStyle underneath, then the line's own Line.style, then each span's. That is the cascade a Line means everywhere else in the toolkit, so a line that carries a style of its own paints with it here too rather than only when a widget happens to patch it in by hand. The spans are written end to end with nothing inserted between them, and the budget is spent as it goes, so a line wider than its budget stops part way through whichever span reaches the edge instead of dropping that span and every one after it. Writing stops as soon as the budget is gone, so the tail of a very long line is never even measured.
Writing also stops at the first span the budget could not draw whole, even when columns are left over. A two-column cluster is dropped rather than split, so a span can end one column short of its budget — and letting the next span start in the column that cluster gave up would draw a character from later in the line as though it came first. A budget of 1 filled with 漢 then x writes nothing and reports 0; it does not draw x at column 0.
This places the line where it is told and nowhere else. Centring or right-aligning a row against a wider area is a decision about layout rather than about writing cells, and belongs to the widget making it — see io.worxbend.tui.core.Alignment for the arithmetic the widgets share.
Attributes
Writes span's content at (x, y) in at most maxWidth columns, and answers how many columns it wrote.
Writes span's content at (x, y) in at most maxWidth columns, and answers how many columns it wrote.
The span's own Style is layered over baseStyle — the argument supplies whatever the span says nothing about, which is how a theme colour reaches text that already chose to be bold. Clipping is setString's: the write stops at the area's right edge or at x + maxWidth, whichever comes first, and a two-column cluster that would only half-fit inside that bound is dropped whole rather than split, so the answer can be smaller than the budget with content left over.
A Span is a tui-core value, and so is the buffer it is drawn into; before this existed the code that put one into the other lived inside tui-widgets, out of reach of every other module. Nothing about it needs a widget.
Attributes
Writes text starting at (x, y), one grapheme cluster per cell, clipping at the area's right edge.
Writes text starting at (x, y), one grapheme cluster per cell, clipping at the area's right edge.
A wide (two-column) cluster occupies its cell plus a continuation cell to the right, which set reserves; a wide cluster that would only half-fit at the right edge is dropped entirely. Grapheme clusters that begin with a combining mark (no base character before them in text) are skipped.
A row outside area returns at once. That is the same clipping set already applies cell by cell, moved ahead of the work: without it a line drawn above or below the buffer still split itself into grapheme clusters and built one Cell per character, only for every one of them to be rejected. Scrolled content, where most rows are off-window by design, is what makes that the common case rather than a corner one.
Attributes
Writes text at (x, y) stopping after at most maxWidth columns, and answers how many columns it wrote.
Writes text at (x, y) stopping after at most maxWidth columns, and answers how many columns it wrote.
Two things this adds over the four-argument setString. The bound: the write stops at whichever comes first, the area's right edge or x + maxWidth, so a caller with a column budget no longer has to cut the string to size beforehand. And the answer: the returned column count is exactly how far the cursor advanced, so a caller laying a row out as a run of segments adds it to x instead of measuring the text a second time.
The count can be smaller than maxWidth even with text left over. A two-column cluster that would only half-fit inside the bound is dropped whole rather than split — a terminal handed half a wide glyph draws it across the column beyond the budget — so a budget of 3 filled with 漢字 writes 2 columns and reports 2. A negative maxWidth is treated as zero: nothing is written and the answer is 0. An arbitrarily large one is treated as "no limit of my own, stop at the area's edge": the bound x + maxWidth is added in Long and then clamped, because in Int that sum overflows for a budget near Int.MaxValue and the wrapped-negative bound stopped the write before it began. Int.MaxValue is the natural spelling for an uncapped budget — Constraint.Max already uses it for one — so it is the value that must not misbehave.
Attributes
Replaces the style of every cell of region that falls inside area, leaving the symbols exactly as they are.
Replaces the style of every cell of region that falls inside area, leaving the symbols exactly as they are.
This is the "patch over the top" write. A widget draws its content first, and then a caller tints the rectangle for a selection highlight, a disabled overlay or a focus ring without needing to know which symbols happen to be there. Neither set nor setString can do that: both take the symbol as an argument, so restyling through them means overwriting the content.
Blank cells are restyled too. That is deliberate — it is what makes a background tint reach the whole rectangle rather than only the columns that carry a glyph.
Continuation cells (the second column of a two-column grapheme) are left alone: the terminal paints the wide grapheme across both columns from the left cell's style, so restyling the right one changes nothing on screen while breaking the invariant that a flagged cell holds Cell.Empty — the test set uses to tell a caller's own continuation cell apart from real content landing on a reserved column.
Attributes
An independent copy of this buffer. Backends snapshot the frame they just flushed so later mutation of the caller's buffer cannot corrupt the next diff.
An independent copy of this buffer. Backends snapshot the frame they just flushed so later mutation of the caller's buffer cannot corrupt the next diff.
The copy is unsynchronized, so it must be taken on the thread that owns this buffer; the result is a fresh buffer owned by the caller, which is what makes it safe to hand to another thread once no further writes follow.
Attributes
A dump of the whole grid, for the one place a buffer is printed: the message of a failed test.
A dump of the whole grid, for the one place a buffer is printed: the message of a failed test.
The default toString of a class is its name and an object hash, which tells a reader nothing about why two frames differ. This prints three sections instead, following ratatui's Debug for Buffer:
content— one quoted string per row, every column included. Continuation cells hold Cell.Empty by construction, so they print as a space and the rows stay aligned in the dump even though the wide glyph before them takes two columns on a real terminal.styles— one line per change of style in row-major order, not one line per cell. A frame is usually a few long runs of one style, so listing the runs is what makes a wrong colour visible instead of drowned.hidden— the positions holding the second column of a two-column grapheme, which thecontentsection cannot show because they carry no symbol of their own.
Building the string walks every cell, so this is a debugging aid and not something to call per frame.
Attributes
- Definition Classes
-
Any