Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Buffer

Buffer.zig is a double-buffered cell grid for flicker-free terminal rendering.

How It Works

  1. Write to the current buffer (buf)
  2. Diff against the previous buffer (prev)
  3. Emit only changed cells to the terminal
  4. Swap buffers

This minimizes terminal I/O and eliminates flicker.

Color System

const Color = union(enum) {
    default,           // Terminal default
    basic: u8,         // 16-color (0-15)
    rgb: [3]u8,        // 24-bit RGB
};
 
// Comptime hex constructor
const green = Color.hex(0x4ade80);
const red = Color.hex(0xf87171);

Cell Style

const Style = struct {
    fg: Color = .default,
    bg: Color = .default,
    bold: bool = false,
    dim: bool = false,
};

Performance Stats

Every flush produces Buffer.Stats:

FieldDescription
cells_changedNumber of cells that differ from previous frame
cursor_movesNumber of cursor repositioning sequences emitted
style_emitsNumber of SGR (color/style) sequences emitted
write_bytesTotal bytes written to terminal
flush_nsTime taken for the flush operation

Synchronized Updates

Frames are wrapped in terminal synchronized update sequences:

\x1b[?2026h    ← begin synchronized update
... cell data ...
\x1b[?2026l    ← end synchronized update

This tells the terminal to batch all changes and display them atomically, preventing partial-frame artifacts.