buffer

package
v0.0.0-...-4a23534 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package buffer provides a 2D cell-based buffer for terminal rendering. Similar to Ratatui's buffer system, it allows precise character positioning and supports overlapping content with proper style handling.

Index

Constants

View Source
const (
	// Reset is the ANSI reset code.
	Reset   = "\033[0m"
	FgReset = "\033[39m"
	BgReset = "\033[49m"

	// BoldOn enables bold text.
	BoldOn           = "\033[1m"
	BoldOff          = "\033[22m"
	ItalicOn         = "\033[3m"
	ItalicOff        = "\033[23m"
	UnderlineOn      = "\033[4m"
	UnderlineOff     = "\033[24m"
	StrikethroughOn  = "\033[9m"
	StrikethroughOff = "\033[29m"

	// FgRGB is the ANSI foreground RGB format string.
	FgRGB = "\033[38;2;%d;%d;%dm"
	BgRGB = "\033[48;2;%d;%d;%dm"
)

ANSI escape code constants.

Variables

View Source
var TailwindColors = map[string]Color{}/* 244 elements not displayed */

TailwindColors maps Tailwind color names to hex values.

Functions

func RuneCount

func RuneCount(s string) int

RuneCount returns the number of runes in a string.

func RuneWidth

func RuneWidth(r rune) int

RuneWidth returns the visual width of a rune (1 for normal, 2 for wide chars).

func StringWidth

func StringWidth(s string) int

StringWidth returns the visual width of a string (handles wide chars).

Types

type BorderStyle

type BorderStyle int

BorderStyle specifies the visual appearance of box borders.

const (
	BorderNone    BorderStyle = iota // No border
	BorderNormal                     // Single-line border ┌─┐
	BorderRounded                    // Rounded corners ╭─╮
	BorderDouble                     // Double-line border ╔═╗
	BorderThick                      // Thick border ┏━┓
	BorderBlock                      // Block border ███
	BorderDotted                     // Dotted lines ┈┊
)

Border style constants.

func (BorderStyle) BorderChars

func (b BorderStyle) BorderChars() (tl, tr, bl, br, h, v rune)

BorderChars returns the six Unicode glyphs for drawing this border style.

type Buffer

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

Buffer is a 2D grid of cells representing terminal content.

func NewBuffer

func NewBuffer(width, height int) *Buffer

NewBuffer creates a new buffer filled with empty cells.

func (*Buffer) Clear

func (b *Buffer) Clear()

Clear resets all cells to default (space, no style).

func (*Buffer) Debug

func (b *Buffer) Debug() string

Debug returns a debug representation showing cell boundaries.

func (*Buffer) DrawBorder

func (b *Buffer) DrawBorder(area Rect, border BorderStyle, fg, bg Color)

DrawBorder draws a border around an area.

func (*Buffer) Fill

func (b *Buffer) Fill(area Rect, cell Cell)

Fill fills a rectangular area with a cell.

func (*Buffer) FillStyle

func (b *Buffer) FillStyle(area Rect, style Style)

FillStyle applies a style to all cells in an area without changing characters.

func (*Buffer) Get

func (b *Buffer) Get(x, y int) Cell

Get returns the cell at (x, y). Returns empty cell if out of bounds.

func (*Buffer) Height

func (b *Buffer) Height() int

Height returns the buffer height.

func (*Buffer) Merge

func (b *Buffer) Merge(x, y int, src *Buffer)

Merge overlays another buffer onto this one at position (x, y). Only non-space characters from src are copied (transparent overlay).

func (*Buffer) Rect

func (b *Buffer) Rect() Rect

Rect returns a Rect covering the entire buffer.

func (*Buffer) Render

func (b *Buffer) Render() string

Render converts the buffer to an ANSI-escaped string. Optimizes output by only emitting escape codes when style changes.

func (*Buffer) RenderArea

func (b *Buffer) RenderArea(area Rect) string

RenderArea renders only a specific rectangular area of the buffer.

func (*Buffer) RenderCompact

func (b *Buffer) RenderCompact() string

RenderCompact renders the buffer, trimming trailing spaces and empty lines.

func (*Buffer) Resize

func (b *Buffer) Resize(width, height int) *Buffer

Resize creates a new buffer with the given dimensions. Content from the old buffer is preserved where it fits.

func (*Buffer) Set

func (b *Buffer) Set(x, y int, cell Cell)

Set sets the cell at (x, y). Does nothing if out of bounds.

func (*Buffer) SetChar

func (b *Buffer) SetChar(x, y int, ch rune)

SetChar sets only the character at (x, y), preserving style.

func (*Buffer) SetString

func (b *Buffer) SetString(x, y int, s string, style Style) int

SetString writes a string starting at (x, y) with the given style. Handles wide characters (CJK) by using placeholder cells. Returns the number of columns consumed.

func (*Buffer) SetStringSimple

func (b *Buffer) SetStringSimple(x, y int, s string) int

SetStringSimple writes a string with no styling.

func (*Buffer) SetStyle

func (b *Buffer) SetStyle(x, y int, fg, bg Color, bold, italic, underline, strike bool)

SetStyle applies style attributes to the cell at (x, y).

func (*Buffer) String

func (b *Buffer) String() string

String implements fmt.Stringer for easy printing.

func (*Buffer) Width

func (b *Buffer) Width() int

Width returns the buffer width.

type Cell

type Cell struct {
	Char          rune
	Fg            Color
	Bg            Color
	Bold          bool
	Italic        bool
	Underline     bool
	Strikethrough bool
}

Cell represents a single character cell in the terminal.

func DefaultCell

func DefaultCell() Cell

DefaultCell returns a cell with a space character and no styling.

func (*Cell) Reset

func (c *Cell) Reset()

Reset clears the cell to default state (space, no colors, no decorations).

type Color

type Color struct {
	R, G, B uint8 // Red, Green, Blue components (0-255)
	Set     bool  // true if color was explicitly set
}

Color represents an RGB color with explicit set tracking. The Set field distinguishes between "not specified" and "set to black (0,0,0)".

A zero-value Color has Set=false, indicating no color was specified. Use NewColor to create a Color with Set=true.

func Hex

func Hex(hex string) Color

Hex creates a Color from a 6-digit hex string without validation.

func NewColor

func NewColor(r, g, b uint8) Color

NewColor creates a new Color with the given RGB values and Set=true.

func ParseBgColor

func ParseBgColor(class string) (Color, bool)

ParseBgColor parses bg-{color} classes for background color.

func ParseBorderColor

func ParseBorderColor(class string) (Color, bool)

ParseBorderColor parses border-{color} classes for border color.

func ParseColorValue

func ParseColorValue(value string) (Color, bool)

ParseColorValue parses a color value from Tailwind name or arbitrary hex.

func ParseHex

func ParseHex(hex string) (Color, bool)

ParseHex parses a 6-digit hex color string into RGB components.

func ParseTextColor

func ParseTextColor(class string) (Color, bool)

ParseTextColor parses text-{color} classes for foreground color.

type Direction

type Direction int

Direction specifies the axis for splitting.

const (
	Horizontal Direction = iota // Split into columns (side by side)
	Vertical                    // Split into rows (stacked)
)

Direction constants for splitting rects.

type Rect

type Rect struct {
	X, Y          int
	Width, Height int
}

Rect defines a rectangular area in the terminal.

func NewRect

func NewRect(x, y, width, height int) Rect

NewRect creates a new Rect.

func (Rect) Area

func (r Rect) Area() int

Area returns the total number of cells.

func (Rect) Bottom

func (r Rect) Bottom() int

Bottom returns the bottom edge Y coordinate (exclusive).

func (Rect) Clamp

func (r Rect) Clamp(bounds Rect) Rect

Clamp ensures the rect fits within another rect.

func (Rect) Contains

func (r Rect) Contains(x, y int) bool

Contains returns true if the point (x, y) is inside the rect.

func (Rect) Inner

func (r Rect) Inner(margin int) Rect

Inner returns a rect reduced by the given margin on all sides.

func (Rect) InnerMargins

func (r Rect) InnerMargins(top, right, bottom, left int) Rect

InnerMargins returns a rect reduced by different margins on each side. Order: top, right, bottom, left (CSS style).

func (Rect) Intersection

func (r Rect) Intersection(other Rect) Rect

Intersection returns the overlapping region of two rects. Returns an empty rect if they don't intersect.

func (Rect) Intersects

func (r Rect) Intersects(other Rect) bool

Intersects returns true if this rect overlaps with another.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

IsEmpty returns true if the rect has zero area.

func (Rect) Left

func (r Rect) Left() int

Left returns the left edge X coordinate.

func (Rect) Offset

func (r Rect) Offset(dx, dy int) Rect

Offset moves the rect by (dx, dy).

func (Rect) Right

func (r Rect) Right() int

Right returns the right edge X coordinate (exclusive).

func (Rect) Split

func (r Rect) Split(dir Direction, sizes []int) []Rect

Split divides the rect into multiple parts based on sizes. For Horizontal: sizes are widths. For Vertical: sizes are heights. Negative sizes mean "remaining space" (like flex-grow).

func (Rect) SplitEqual

func (r Rect) SplitEqual(dir Direction, n int) []Rect

SplitEqual divides the rect into n equal parts.

func (Rect) Top

func (r Rect) Top() int

Top returns the top edge Y coordinate.

type Style

type Style struct {
	Fg            Color
	Bg            Color
	Bold          bool
	Italic        bool
	Underline     bool
	Strikethrough bool
}

Style holds styling attributes for text rendering.

func (Style) WithBg

func (s Style) WithBg(r, g, b uint8) Style

WithBg returns a new Style with background color set.

func (Style) WithBold

func (s Style) WithBold() Style

WithBold returns a new Style with bold enabled.

func (Style) WithFg

func (s Style) WithFg(r, g, b uint8) Style

WithFg returns a new Style with foreground color set.

func (Style) WithItalic

func (s Style) WithItalic() Style

WithItalic returns a new Style with italic enabled.

func (Style) WithStrikethrough

func (s Style) WithStrikethrough() Style

WithStrikethrough returns a new Style with strikethrough enabled.

func (Style) WithUnderline

func (s Style) WithUnderline() Style

WithUnderline returns a new Style with underline enabled.

Jump to

Keyboard shortcuts

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