class Termisu::Buffer

Overview

Buffer manages a 2D grid of cells with double buffering support.

Buffer maintains:

Performance Optimizations:

Example:

buffer = Termisu::Buffer.new(80, 24)
buffer.set_cell(10, 5, 'A', fg: Color.green, bg: Color.black)
buffer.set_cursor(10, 5)
buffer.render_to(renderer) # Only changed cells and cursor are redrawn

Defined in:

termisu/buffer.cr

Constant Summary

Log = Termisu::Logs::Buffer

Constructors

Instance Method Summary

Constructor Detail

def self.new(width : Int32, height : Int32) #

Creates a new Buffer with the specified dimensions.

Parameters:

  • width: Number of columns
  • height: Number of rows

[View source]

Instance Method Detail

def clear #

Clears the back buffer (fills with default cells).


[View source]
def cursor : Cursor #

[View source]
def get_cell(x : Int32, y : Int32) : Cell | Nil #

Gets a cell at the specified position from the back buffer.

Returns nil if coordinates are out of bounds.


[View source]
def height : Int32 #

[View source]
def hide_cursor #

Hides the cursor.


[View source]
def render_to(renderer : Renderer) #

Renders changes to the renderer by diffing front and back buffers.

Only cells that have changed are redrawn. After rendering, the back buffer becomes the new front buffer. Cursor position and visibility are also updated.

Optimizations applied:

  • Batches consecutive cells with same styling on same row
  • Only emits escape sequences when color/attribute changes
  • Minimizes cursor movement by tracking position

Parameters:

  • renderer: The renderer to render cells to

[View source]
def resize(new_width : Int32, new_height : Int32) #

Resizes the buffer to new dimensions.

Preserves existing content where possible. New cells are default. Clears both front and back buffers after resize.


[View source]
def set_cell(x : Int32, y : Int32, ch : Char, fg : Color = Color.white, bg : Color = Color.default, attr : Attribute = Attribute::None) : Bool #

Sets a cell at the specified position in the back buffer.

Parameters:

  • x: Column position (0-based)
  • y: Row position (0-based)
  • ch: Character to display
  • fg: Foreground color (default: white)
  • bg: Background color (default: default terminal color)
  • attr: Text attributes (default: None)

Returns false if coordinates are out of bounds.


[View source]
def set_cursor(x : Int32, y : Int32) #

Sets cursor position and makes it visible.

Coordinates are clamped to buffer bounds. Negative values are clamped to 0. Values exceeding buffer dimensions are clamped to max valid position.


[View source]
def show_cursor #

Shows the cursor at current position (or 0,0 if never positioned).


[View source]
def sync_to(renderer : Renderer) #

Forces a full redraw of all cells to the renderer, ignoring the diff.

Useful after terminal resize or corruption.


[View source]
def width : Int32 #

[View source]