Skip to main content

Overview

The OptimizedBuffer class provides a high-performance, double-buffered rendering system for terminal UIs. It manages character cells, colors, attributes, and supports advanced features like scissor rects, opacity, and various drawing primitives.

Creating a Buffer

OptimizedBuffer.create()

Create a new optimized buffer.
number
Width in terminal cells
number
Height in terminal cells
'unicode' | 'wcwidth'
Character width calculation method
boolean
default:false
Whether to respect alpha channel in colors
string
Optional identifier for debugging
OptimizedBuffer
The created buffer instance

Example

Properties

number
Buffer width in cells (read-only)
number
Buffer height in cells (read-only)
WidthMethod
Character width calculation method (read-only)
boolean
Whether alpha blending is enabled
string
Buffer identifier

Basic Drawing Methods

clear()

Clear the entire buffer with a background color.
RGBA
default:"RGBA.fromValues(0, 0, 0, 1)"
Background color

Example

setCell()

Set a single cell’s character, colors, and attributes.
number
X coordinate (column)
number
Y coordinate (row)
string
Character to display (single character or grapheme cluster)
RGBA
Foreground color
RGBA
Background color
number
default:0
Text attributes (bold, italic, underline, etc.)

setCellWithAlphaBlending()

Set a cell with alpha blending.
Parameters are the same as setCell(), but colors are blended with existing colors based on alpha values.

drawText()

Draw text at a position.
string
Text to draw
number
X coordinate (starting column)
number
Y coordinate (row)
RGBA
Foreground color
RGBA
Background color (optional)
number
default:0
Text attributes
object
Optional selection range to highlight

Example

fillRect()

Fill a rectangular area with a color.
number
X coordinate
number
Y coordinate
number
Rectangle width
number
Rectangle height
RGBA
Fill color

drawBox()

Draw a bordered box with optional title.
number
X coordinate
number
Y coordinate
number
Box width
number
Box height
'single' | 'double' | 'rounded' | 'bold' | 'double-single' | 'single-double'
Border style preset
boolean | ('top' | 'right' | 'bottom' | 'left')[]
Which borders to draw (true = all sides)
RGBA
Border color
RGBA
Background color
boolean
default:false
Whether to fill the box interior
string
Optional title text
'left' | 'center' | 'right'
default:"'left'"
Title alignment

Example

Advanced Drawing

drawFrameBuffer()

Draw another buffer onto this buffer (compositing).
number
Destination X coordinate
number
Destination Y coordinate
OptimizedBuffer
Source buffer to draw
number
Optional source X (for partial copy)
number
Optional source Y
number
Optional source width
number
Optional source height

drawGrid()

Draw a grid with borders.
Uint32Array
Border characters (9 elements: TL, T, TR, L, C, R, BL, B, BR)
RGBA
Border foreground color
RGBA
Border background color
Int32Array
Array of column X positions
Int32Array
Array of row Y positions
boolean
Draw inner grid lines
boolean
Draw outer border

Clipping and Opacity

pushScissorRect()

Push a clipping rectangle (subsequent draws are clipped to this region).
number
X coordinate
number
Y coordinate
number
Rectangle width
number
Rectangle height

popScissorRect()

Remove the most recent scissor rectangle.

clearScissorRects()

Remove all scissor rectangles.

pushOpacity()

Push an opacity level (0 = transparent, 1 = opaque). Opacity levels are multiplied.
number
Opacity value (0-1)

popOpacity()

Remove the most recent opacity level.

getCurrentOpacity()

Get the current composite opacity.
number
Current opacity value

clearOpacity()

Remove all opacity levels.

Buffer Management

resize()

Resize the buffer.
number
New width
number
New height

destroy()

Destroy the buffer and free resources.

setRespectAlpha()

Enable or disable alpha blending.
boolean
Whether to respect alpha channel

Advanced Features

getSpanLines()

Get buffer content as structured spans (for text extraction, testing).
CapturedLine[]
Array of lines with styled spans

CapturedLine Structure

getRealCharBytes()

Get the raw character data as UTF-8 bytes.
boolean
default:false
Whether to add line breaks between rows
Uint8Array
UTF-8 encoded character data

Raw Buffer Access

buffers

Access raw buffer arrays (advanced use only).
Uint32Array
Character codepoints (with flags)
Float32Array
Foreground colors (RGBA, 4 floats per cell)
Float32Array
Background colors (RGBA, 4 floats per cell)
Uint32Array
Text attributes (bold, italic, etc.)

Example Usage