buffer

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertBufferAreaEq

func AssertBufferAreaEq(buf *Buffer, area layout.Rect, expected []string) error

AssertBufferAreaEq asserts that the given area of the buffer matches the expected content. The expected content is a slice of strings where each string represents a row. Only the symbol is checked, not the style.

func AssertBufferEq

func AssertBufferEq(expected, actual *Buffer) error

AssertBufferEq asserts that two buffers are equal, returning an error describing the first difference found, or nil if they are equal.

func AssertCellStyle

func AssertCellStyle(buf *Buffer, x, y uint16, fg, bg style.Color, mod style.Modifier) error

AssertCellStyle asserts that the cell at (x,y) has the expected foreground, background, and modifier.

func BufferToString

func BufferToString(buf *Buffer) string

BufferToString returns a string representation of the buffer for debugging. Each row is on a separate line, showing only the symbols.

func RuneWidth

func RuneWidth(r rune) int

RuneWidth returns the display width of a rune in terminal cells. Handles half-width katakana combining marks (U+FF9E, U+FF9F) as width 0 since they combine with the preceding character in some terminals.

Box Drawing (U+2500–U+257F), Block Elements (U+2580–U+259F), and Geometric Shapes (U+25A0–U+25FF) are forced to width 1 because go-runewidth treats them as ambiguous-width (width 2) under CJK locale, but all modern terminal emulators consistently render them as single-width.

func StringWidth

func StringWidth(s string) int

StringWidth returns the display width of a string in terminal cells. Wide characters (CJK etc.) count as 2 cells. 逐 rune 用 RuneWidth 累加而非直通 runewidth.StringWidth: RuneWidth 对 Box Drawing/Block Elements 等强制宽度 1(CJK locale 下 runewidth 按环境变量启用 EastAsianWidth 会计 2),测量与渲染必须 共用同一宽度事实来源,否则对齐与光标计算在中文环境下漂移。

func StringWidthTruncated

func StringWidthTruncated(s string, maxBytes int) int

StringWidthTruncated returns the display width of s up to maxBytes bytes.

Types

type Buffer

type Buffer struct {
	Area    layout.Rect
	Content []Cell
}

Buffer represents a 2D grid of cells that maps to the terminal screen.

func Empty

func Empty() Buffer

Empty creates a zero-sized buffer.

func NewBuffer

func NewBuffer(area layout.Rect) Buffer

NewBuffer creates a new buffer with the given area, filled with blank cells.

func (*Buffer) CellAt

func (b *Buffer) CellAt(x, y uint16) *Cell

CellAt returns a pointer to the cell at the given position.

func (*Buffer) Clear

func (b *Buffer) Clear()

Clear resets all cells in the buffer.

func (*Buffer) Diff

func (b *Buffer) Diff(previous *Buffer) []CellDiff

Diff computes the differences between two buffers. 每次调用都会分配新的切片;高频渲染场景应优先使用 DiffInto 复用底层数组。

func (*Buffer) DiffInto

func (b *Buffer) DiffInto(previous *Buffer, dst []CellDiff) []CellDiff

DiffInto computes the differences between two buffers, appending to dst and returning the resulting slice. 传入 dst[:0] 可复用底层容量,避免每帧分配。 dst 为 nil 时等价于 Diff。 宽字符 follower cell(WideChar=true)跳过:leader 输出后终端自动覆盖 follower 区域,backend 同样会忽略它们,进入 diffs 只浪费拷贝。

func (*Buffer) DiffIter

func (b *Buffer) DiffIter(previous *Buffer) DiffIter

DiffIter returns a zero-allocation diff iterator. Usage:

it := b.DiffIter(previous)
for it.Next() {
    x, y, cell := it.Cell()
    // process diff
}

func (*Buffer) IndexOf

func (b *Buffer) IndexOf(x, y uint16) int

IndexOf returns the index in the content array for the given position.

func (*Buffer) Resize

func (b *Buffer) Resize(area layout.Rect)

Resize resizes the buffer to the given area.

func (*Buffer) SetCell

func (b *Buffer) SetCell(x, y uint16, symbol string, sty style.Style)

SetCell sets a single cell at the given position.

func (*Buffer) SetLine

func (b *Buffer) SetLine(x, y uint16, s string, sty style.Style)

SetLine writes a string starting at (x, y), wrapping to next lines if needed. Handles wide characters correctly.

func (*Buffer) SetString

func (b *Buffer) SetString(x, y uint16, s string, sty style.Style)

SetString writes a string at the given position with the given style. Handles multi-byte UTF-8 and wide characters (e.g. CJK) correctly. Wide characters occupy 2 cells; the second cell is reset to hide overlap.

func (*Buffer) SetStringn

func (b *Buffer) SetStringn(x, y uint16, s string, maxWidth uint16, sty style.Style) uint16

SetStringn writes a string at the given position with the given style, limited to at most maxWidth display cells. If maxWidth is 0, no limit is applied. Returns the x position after the last written cell.

type Cell

type Cell struct {
	Symbol   string
	Fg       style.Color
	Bg       style.Color
	Modifier style.Modifier
	WideChar bool   // true if this cell is the second (hidden) half of a wide character
	Skip     bool   // if true, skip this cell during diff rendering
	Link     string // OSC 8 hyperlink URL (empty = no link)
	LinkID   string // OSC 8 hyperlink ID (optional, for grouping)
}

Cell represents a single cell in the terminal buffer.

func NewCell

func NewCell(symbol string) Cell

NewCell creates a new Cell with the given symbol and default style.

func (Cell) DiffOption

func (c Cell) DiffOption() CellDiffOption

DiffOption returns the CellDiffOption for this cell.

func (c Cell) HasLink() bool

HasLink returns true if this cell has an OSC 8 hyperlink.

func (*Cell) Reset

func (c *Cell) Reset()

Reset resets the cell to default (space with reset style).

func (c *Cell) SetLink(url string, id string)

SetLink sets the OSC 8 hyperlink for this cell.

func (*Cell) SetStyle

func (c *Cell) SetStyle(s style.Style)

SetStyle sets the cell's style by patching it with the given style.

func (*Cell) SetSymbol

func (c *Cell) SetSymbol(s string)

SetSymbol sets the cell's symbol.

func (Cell) Style

func (c Cell) Style() style.Style

Style returns the cell's current style.

type CellDiff

type CellDiff struct {
	X    uint16
	Y    uint16
	Cell Cell
}

Diff computes the diff between this buffer and a previous buffer, returning only the cells that changed.

type CellDiffOption

type CellDiffOption int

CellDiffOption controls how a cell is handled during diff computation.

const (
	// CellDiffNone means the cell is included in diff normally.
	CellDiffNone CellDiffOption = iota
	// CellDiffSkip means the cell is always skipped during diff.
	CellDiffSkip
	// CellDiffAlwaysUpdate means the cell is always included in diff, even if unchanged.
	CellDiffAlwaysUpdate
	// CellDiffForcedWidth means the cell is included in diff to force a width update
	// (e.g. after a wide character boundary change).
	CellDiffForcedWidth
)

type DiffIter

type DiffIter struct {
	// contains filtered or unexported fields
}

DiffIter is a zero-allocation iterator over buffer diffs. It walks the buffer cells on demand without creating a slice.

func (*DiffIter) Cell

func (it *DiffIter) Cell() (uint16, uint16, *Cell)

Cell returns the position and cell at the current iterator position.

func (*DiffIter) Next

func (it *DiffIter) Next() bool

Next advances the iterator to the next diff cell. Returns false when there are no more diffs.

Jump to

Keyboard shortcuts

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