class
Termisu::Buffer
- Termisu::Buffer
- Reference
- Object
Overview
Buffer manages a 2D grid of cells with double buffering support.
Buffer maintains:
- Front buffer: What's currently displayed on screen
- Back buffer: Where new content is written
- Diff algorithm: Only redraws cells that have changed
- Cursor position and visibility
- Render state tracking for escape sequence optimization
Performance Optimizations:
- Only emits color/attribute escape sequences when they change
- Batches consecutive cells on the same row with the same styling
- Tracks cursor position to minimize move_cursor calls
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.crConstant Summary
-
Log =
Termisu::Logs::Buffer
Constructors
-
.new(width : Int32, height : Int32)
Creates a new Buffer with the specified dimensions.
Instance Method Summary
-
#clear
Clears the back buffer (fills with default cells).
- #cursor : Cursor
-
#get_cell(x : Int32, y : Int32) : Cell | Nil
Gets a cell at the specified position from the back buffer.
- #height : Int32
-
#hide_cursor
Hides the cursor.
-
#render_to(renderer : Renderer)
Renders changes to the renderer by diffing front and back buffers.
-
#resize(new_width : Int32, new_height : Int32)
Resizes the buffer to new dimensions.
-
#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.
-
#set_cursor(x : Int32, y : Int32)
Sets cursor position and makes it visible.
-
#show_cursor
Shows the cursor at current position (or 0,0 if never positioned).
-
#sync_to(renderer : Renderer)
Forces a full redraw of all cells to the renderer, ignoring the diff.
- #width : Int32
Constructor Detail
Creates a new Buffer with the specified dimensions.
Parameters:
- width: Number of columns
- height: Number of rows
Instance Method Detail
Gets a cell at the specified position from the back buffer.
Returns nil if coordinates are out of bounds.
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
Resizes the buffer to new dimensions.
Preserves existing content where possible. New cells are default. Clears both front and back buffers after resize.
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.
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.
Forces a full redraw of all cells to the renderer, ignoring the diff.
Useful after terminal resize or corruption.