Raxol.Core.Runtime.Rendering.Buffer (Raxol v0.3.0)

View Source

Provides a screen buffer implementation for efficient rendering in Raxol applications.

This module is responsible for:

  • Maintaining the screen buffer state
  • Calculating diffs between buffer states
  • Optimizing rendering by only updating changed cells

Summary

Types

A cell represents a single character position on the screen.

t()

A buffer is a map containing cells indexed by their position.

Functions

Clears the buffer by removing all cells.

Computes the diff between two buffers.

Builds a buffer from a list of cells.

Gets the cell at the specified position.

Merges another buffer's cells into this buffer.

Creates a new empty buffer with the given dimensions.

Resizes the buffer to new dimensions.

Converts the buffer to a flat list of cells.

Types

cell()

@type cell() :: {integer(), integer(), String.t(), term(), term(), list()}

A cell represents a single character position on the screen.

It is represented as a tuple with the following elements:

  • x: X coordinate (column)
  • y: Y coordinate (row)
  • ch: Character to display
  • fg: Foreground color
  • bg: Background color
  • attrs: List of attributes (e.g., :bold, :underline)

t()

@type t() :: %Raxol.Core.Runtime.Rendering.Buffer{
  cells: %{
    required({integer(), integer()}) => {String.t(), term(), term(), list()}
  },
  dirty: boolean(),
  height: integer(),
  width: integer()
}

A buffer is a map containing cells indexed by their position.

The buffer also contains metadata about its dimensions.

Functions

clear(buffer)

Clears the buffer by removing all cells.

Parameters

  • buffer: The buffer to clear

Returns

Cleared buffer.

diff(old_buffer, new_buffer)

Computes the diff between two buffers.

The diff contains only the cells that need to be updated.

Parameters

  • old_buffer: Previous buffer state
  • new_buffer: Current buffer state

Returns

List of changed cells.

from_cells(cells, width, height)

Builds a buffer from a list of cells.

Parameters

  • cells: List of cells
  • width: Width of the buffer in columns
  • height: Height of the buffer in rows

Returns

A new buffer containing the cells.

get_cell(buffer, x, y)

Gets the cell at the specified position.

Parameters

  • buffer: The buffer to query
  • x: X coordinate
  • y: Y coordinate

Returns

The cell tuple if found, nil otherwise.

merge(buffer, other, offset_x \\ 0, offset_y \\ 0)

Merges another buffer's cells into this buffer.

Cells from the other buffer overwrite cells in this buffer.

Parameters

  • buffer: The destination buffer
  • other: The source buffer
  • offset_x: X offset to apply when merging
  • offset_y: Y offset to apply when merging

Returns

Updated buffer.

new(width, height)

Creates a new empty buffer with the given dimensions.

Parameters

  • width: Width of the buffer in columns
  • height: Height of the buffer in rows

Returns

A new buffer struct.

resize(buffer, width, height)

Resizes the buffer to new dimensions.

Cells outside the new dimensions are discarded.

Parameters

  • buffer: The buffer to resize
  • width: New width
  • height: New height

Returns

Resized buffer.

set_cell(buffer, x, y, ch, fg, bg, attrs \\ [])

Sets a cell in the buffer.

Parameters

  • buffer: The buffer to modify
  • x: X coordinate
  • y: Y coordinate
  • ch: Character
  • fg: Foreground color
  • bg: Background color
  • attrs: Attributes list

Returns

Updated buffer.

to_cells(buffer)

Converts the buffer to a flat list of cells.

Parameters

  • buffer: The buffer to convert

Returns

List of cells.