Raxol.Protocols.BufferOperations protocol (Raxol v2.0.1)
View SourceProtocol 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
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
@type position() :: {non_neg_integer(), non_neg_integer()}
@type region() :: :all | :line | :screen | {:rect, position(), position()} | {:lines, non_neg_integer(), non_neg_integer()}
@type style() :: map() | nil
@type t() :: term()
All the types that implement this protocol.
Functions
Clears the buffer or a specific region.
Parameters
buffer- The buffer to clearregion- 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.
@spec dimensions(t()) :: {non_neg_integer(), non_neg_integer()}
Gets the dimensions of the buffer.
Returns
A tuple {width, height} representing the buffer dimensions.
@spec read(t(), position(), non_neg_integer()) :: binary() | nil
Reads data from the buffer at the specified position.
Parameters
buffer- The buffer to read fromposition- A tuple{x, y}specifying the starting positionlength- Number of characters to read (default: 1)
Returns
The data at the specified position, or nil if position is out of bounds.
@spec scroll(t(), :up | :down, non_neg_integer()) :: t()
Scrolls the buffer content.
Parameters
buffer- The buffer to scrolldirection-:upor:downlines- Number of lines to scroll
Returns
The updated buffer.
Writes data to the buffer at the specified position.
Parameters
buffer- The buffer to write toposition- A tuple{x, y}specifying the positiondata- The data to write (string or character)style- Optional style map containing attributes like color, bold, etc.
Returns
The updated buffer.