Raxol.Protocols.BufferOperations protocol (Raxol v2.0.1)

View Source

Protocol for buffer operations in terminal emulation.

This protocol provides a unified interface for different types of buffers (screen buffers, scrollback buffers, overlay buffers) to implement their own strategies for writing, reading, and clearing data.

Examples

defimpl Raxol.Protocols.BufferOperations, for: MyBuffer do
  def write(buffer, {x, y}, data, style) do
    # Implementation for writing to buffer
  end

  def read(buffer, {x, y}, length) do
    # Implementation for reading from buffer
  end

  def clear(buffer, :all) do
    # Implementation for clearing buffer
  end
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Clears the buffer or a specific region.

Gets the dimensions of the buffer.

Reads data from the buffer at the specified position.

Scrolls the buffer content.

Writes data to the buffer at the specified position.

Types

position()

@type position() :: {non_neg_integer(), non_neg_integer()}

region()

@type region() ::
  :all
  | :line
  | :screen
  | {:rect, position(), position()}
  | {:lines, non_neg_integer(), non_neg_integer()}

style()

@type style() :: map() | nil

t()

@type t() :: term()

All the types that implement this protocol.

Functions

clear(buffer, region \\ :all)

@spec clear(t(), region()) :: t()

Clears the buffer or a specific region.

Parameters

  • buffer - The buffer to clear
  • region - The region to clear:
    • :all - Clear entire buffer
    • :line - Clear current line
    • :screen - Clear visible screen
    • {:rect, {x1, y1}, {x2, y2}} - Clear rectangular region
    • {:lines, start, end} - Clear lines from start to end

Returns

The updated buffer.

dimensions(buffer)

@spec dimensions(t()) :: {non_neg_integer(), non_neg_integer()}

Gets the dimensions of the buffer.

Returns

A tuple {width, height} representing the buffer dimensions.

read(buffer, position, length \\ 1)

@spec read(t(), position(), non_neg_integer()) :: binary() | nil

Reads data from the buffer at the specified position.

Parameters

  • buffer - The buffer to read from
  • position - A tuple {x, y} specifying the starting position
  • length - Number of characters to read (default: 1)

Returns

The data at the specified position, or nil if position is out of bounds.

scroll(buffer, direction, lines \\ 1)

@spec scroll(t(), :up | :down, non_neg_integer()) :: t()

Scrolls the buffer content.

Parameters

  • buffer - The buffer to scroll
  • direction - :up or :down
  • lines - Number of lines to scroll

Returns

The updated buffer.

write(buffer, position, data, style \\ nil)

@spec write(t(), position(), binary() | char(), style()) :: t()

Writes data to the buffer at the specified position.

Parameters

  • buffer - The buffer to write to
  • position - A tuple {x, y} specifying the position
  • data - The data to write (string or character)
  • style - Optional style map containing attributes like color, bold, etc.

Returns

The updated buffer.