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
- Variables
- func RuneCount(s string) int
- func RuneWidth(r rune) int
- func StringWidth(s string) int
- type BorderStyle
- type Buffer
- func (b *Buffer) Clear()
- func (b *Buffer) Debug() string
- func (b *Buffer) DrawBorder(area Rect, border BorderStyle, fg, bg Color)
- func (b *Buffer) Fill(area Rect, cell Cell)
- func (b *Buffer) FillStyle(area Rect, style Style)
- func (b *Buffer) Get(x, y int) Cell
- func (b *Buffer) Height() int
- func (b *Buffer) Merge(x, y int, src *Buffer)
- func (b *Buffer) Rect() Rect
- func (b *Buffer) Render() string
- func (b *Buffer) RenderArea(area Rect) string
- func (b *Buffer) RenderCompact() string
- func (b *Buffer) Resize(width, height int) *Buffer
- func (b *Buffer) Set(x, y int, cell Cell)
- func (b *Buffer) SetChar(x, y int, ch rune)
- func (b *Buffer) SetString(x, y int, s string, style Style) int
- func (b *Buffer) SetStringSimple(x, y int, s string) int
- func (b *Buffer) SetStyle(x, y int, fg, bg Color, bold, italic, underline, strike bool)
- func (b *Buffer) String() string
- func (b *Buffer) Width() int
- type Cell
- type Color
- type Direction
- type Rect
- func (r Rect) Area() int
- func (r Rect) Bottom() int
- func (r Rect) Clamp(bounds Rect) Rect
- func (r Rect) Contains(x, y int) bool
- func (r Rect) Inner(margin int) Rect
- func (r Rect) InnerMargins(top, right, bottom, left int) Rect
- func (r Rect) Intersection(other Rect) Rect
- func (r Rect) Intersects(other Rect) bool
- func (r Rect) IsEmpty() bool
- func (r Rect) Left() int
- func (r Rect) Offset(dx, dy int) Rect
- func (r Rect) Right() int
- func (r Rect) Split(dir Direction, sizes []int) []Rect
- func (r Rect) SplitEqual(dir Direction, n int) []Rect
- func (r Rect) Top() int
- type Style
Constants ¶
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 ¶
var TailwindColors = map[string]Color{}/* 244 elements not displayed */
TailwindColors maps Tailwind color names to hex values.
Functions ¶
func StringWidth ¶
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 (*Buffer) Clear ¶
func (b *Buffer) Clear()
Clear resets all cells to default (space, no style).
func (*Buffer) DrawBorder ¶
func (b *Buffer) DrawBorder(area Rect, border BorderStyle, fg, bg Color)
DrawBorder draws a border around an area.
func (*Buffer) FillStyle ¶
FillStyle applies a style to all cells in an area without changing characters.
func (*Buffer) Merge ¶
Merge overlays another buffer onto this one at position (x, y). Only non-space characters from src are copied (transparent overlay).
func (*Buffer) Render ¶
Render converts the buffer to an ANSI-escaped string. Optimizes output by only emitting escape codes when style changes.
func (*Buffer) RenderArea ¶
RenderArea renders only a specific rectangular area of the buffer.
func (*Buffer) RenderCompact ¶
RenderCompact renders the buffer, trimming trailing spaces and empty lines.
func (*Buffer) Resize ¶
Resize creates a new buffer with the given dimensions. Content from the old buffer is preserved where it fits.
func (*Buffer) SetString ¶
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 ¶
SetStringSimple writes a string with no styling.
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.
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 ParseBgColor ¶
ParseBgColor parses bg-{color} classes for background color.
func ParseBorderColor ¶
ParseBorderColor parses border-{color} classes for border color.
func ParseColorValue ¶
ParseColorValue parses a color value from Tailwind name or arbitrary hex.
func ParseTextColor ¶
ParseTextColor parses text-{color} classes for foreground color.
type Rect ¶
Rect defines a rectangular area in the terminal.
func (Rect) InnerMargins ¶
InnerMargins returns a rect reduced by different margins on each side. Order: top, right, bottom, left (CSS style).
func (Rect) Intersection ¶
Intersection returns the overlapping region of two rects. Returns an empty rect if they don't intersect.
func (Rect) Intersects ¶
Intersects returns true if this rect overlaps with another.
func (Rect) Split ¶
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 ¶
SplitEqual divides the rect into n equal parts.
type Style ¶
Style holds styling attributes for text rendering.
func (Style) WithItalic ¶
WithItalic returns a new Style with italic enabled.
func (Style) WithStrikethrough ¶
WithStrikethrough returns a new Style with strikethrough enabled.
func (Style) WithUnderline ¶
WithUnderline returns a new Style with underline enabled.