module RatatuiRuby::Buffer
Buffer primitives for terminal cell inspection.
Widgets render to an intermediate buffer, not directly to the terminal. Testing and debugging require access to buffer state.
This module mirrors ratatui::buffer and provides query methods for inspecting buffer contents, converting between coordinates and indices, and retrieving individual cells.
Use it in tests to verify rendered output or in debugging to inspect state.
Public Class Methods
Source
# File lib/ratatui_ruby/buffer.rb, line 130 def content area = RatatuiRuby._get_terminal_area width = area["width"] height = area["height"] cells = [] #: Array[Buffer::Cell] (0...height).each do |y| (0...width).each do |x| cells << RatatuiRuby.get_cell_at(x, y) end end cells end
Returns all cells in the buffer as a flat array.
Snapshot testing compares entire buffer states. Manually iterating coordinates is verbose and error-prone.
This method returns every cell, ordered row by row (top to bottom, left to right).
Example
cells = Buffer.content cells.each { |cell| puts cell.char }
Returns an Array of Buffer::Cell objects.
Source
# File lib/ratatui_ruby/buffer.rb, line 104 def get(x, y) RatatuiRuby.get_cell_at(x, y) end
Returns the Cell at the specified position.
Tests assert on cell contents. This method provides direct access without iterating the entire buffer.
Delegates to RatatuiRuby.get_cell_at.
Example
cell = Buffer.get(0, 0) assert_equal "H", cell.char
- x
-
Column (0-indexed from left).
- y
-
Row (0-indexed from top).
Returns a Buffer::Cell containing the character and style at that position.
Source
# File lib/ratatui_ruby/buffer.rb, line 46 def index_of(x, y) area = RatatuiRuby._get_terminal_area (y * area["width"]) + x end
Converts a position to a linear buffer index.
Buffers store cells in a flat array, row by row. Widget code works with (x, y) coordinates. Bridging these representations requires index translation.
The index is calculated as y * width + x.
Example
# In a 10-wide buffer, position (3, 2) maps to index 23 Buffer.index_of(3, 2) # => 23
- x
-
Column (0-indexed from left).
- y
-
Row (0-indexed from top).
Returns the linear index (Integer).
Source
# File lib/ratatui_ruby/buffer.rb, line 72 def pos_of(index) area = RatatuiRuby._get_terminal_area width = area["width"] x = index % width y = index / width [x, y] end
Converts a linear buffer index to position coordinates.
Inverse of index_of. When iterating over buffer content by index, use this to recover the original coordinates.
Example
# In a 10-wide buffer, index 23 maps to position (3, 2) Buffer.pos_of(23) # => [3, 2]
- index
-
Linear buffer index (Integer).
Returns [x, y] coordinates.