Class: RatatuiRuby::Buffer::Cell
- Inherits:
-
Object
- Object
- RatatuiRuby::Buffer::Cell
- Defined in:
- lib/ratatui_ruby/buffer/cell.rb
Overview
Represents a single cell in the terminal buffer.
A terminal grid is made of cells. Each cell contains a character (symbol) and styling (colors, modifiers). When testing, you often need to verify that a specific cell renders correctly.
This object encapsulates that state. It provides predicate methods for modifiers, making assertions readable.
Use it to inspect the visual state of your application in tests.
Examples
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++
cell = RatatuiRuby.get_cell_at(0, 0)
cell.char # => "H"
cell.fg # => :red
cell.bold? # => true
– SPDX-SnippetEnd ++
Instance Attribute Summary collapse
-
#bg ⇒ Object
readonly
The background color of the cell (e.g., :black, nil).
-
#fg ⇒ Object
readonly
The foreground color of the cell (e.g., :red, :blue, “#ff0000”).
-
#modifiers ⇒ Object
readonly
The list of active modifiers (e.g., [“bold”, “italic”]).
-
#symbol ⇒ Object
readonly
The character displayed in the cell.
-
#underline_color ⇒ Object
readonly
The underline color of the cell.
Class Method Summary collapse
-
.char(char) ⇒ Object
Alias for Rubyists who prefer a shorter name.
-
.default ⇒ Object
Returns a default cell (alias for empty).
-
.empty ⇒ Object
Returns an empty cell (space character, no styles).
-
.symbol(symbol) ⇒ Object
Returns a cell with a specific character and no styles.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks equality with another Cell.
-
#bold? ⇒ Boolean
Returns true if the cell has the bold modifier.
-
#char ⇒ Object
The character displayed in the cell.
-
#crossed_out? ⇒ Boolean
Returns true if the cell has the crossed_out modifier.
-
#deconstruct_keys(keys) ⇒ Object
Support for pattern matching.
-
#dim? ⇒ Boolean
Returns true if the cell has the dim modifier.
-
#hidden? ⇒ Boolean
Returns true if the cell has the hidden modifier.
-
#initialize(symbol: nil, char: nil, fg: nil, bg: nil, underline_color: nil, modifiers: []) ⇒ Cell
constructor
Creates a new Cell.
-
#inspect ⇒ Object
Returns a string representation of the cell.
-
#italic? ⇒ Boolean
Returns true if the cell has the italic modifier.
-
#rapid_blink? ⇒ Boolean
Returns true if the cell has the rapid_blink modifier.
-
#reversed? ⇒ Boolean
Returns true if the cell has the reversed modifier.
-
#slow_blink? ⇒ Boolean
Returns true if the cell has the slow_blink modifier.
-
#to_s ⇒ Object
Returns the cell’s character.
-
#underlined? ⇒ Boolean
Returns true if the cell has the underlined modifier.
Constructor Details
#initialize(symbol: nil, char: nil, fg: nil, bg: nil, underline_color: nil, modifiers: []) ⇒ Cell
Creates a new Cell.
- symbol
-
String (single character). Aliased as
char:. - fg
-
Symbol or String (nullable).
- bg
-
Symbol or String (nullable).
- underline_color
-
Symbol or String (nullable).
- modifiers
-
Array of Strings, Symbols, or any object responding to to_sym or to_s. Normalized to Symbols for consistent output.
126 127 128 129 130 131 132 133 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 126 def initialize(symbol: nil, char: nil, fg: nil, bg: nil, underline_color: nil, modifiers: []) @symbol = (symbol || char || " ").freeze @fg = fg&.freeze @bg = bg&.freeze @underline_color = underline_color&.freeze @modifiers = modifiers.map { |m| m.respond_to?(:to_sym) ? m.to_sym : m.to_s.to_sym }.freeze freeze end |
Instance Attribute Details
#bg ⇒ Object (readonly)
The background color of the cell (e.g., :black, nil).
47 48 49 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 47 def bg @bg end |
#fg ⇒ Object (readonly)
The foreground color of the cell (e.g., :red, :blue, “#ff0000”).
44 45 46 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 44 def fg @fg end |
#modifiers ⇒ Object (readonly)
The list of active modifiers (e.g., [“bold”, “italic”]).
55 56 57 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 55 def modifiers @modifiers end |
#symbol ⇒ Object (readonly)
The character displayed in the cell.
Named to match Ratatui’s Cell::symbol() method.
38 39 40 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 38 def symbol @symbol end |
#underline_color ⇒ Object (readonly)
The underline color of the cell.
Distinct from foreground color. Some terminals support colored underlines.
52 53 54 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 52 def underline_color @underline_color end |
Class Method Details
.char(char) ⇒ Object
Alias for Rubyists who prefer a shorter name.
114 115 116 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 114 def self.char(char) symbol(char) end |
.default ⇒ Object
89 90 91 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 89 def self.default empty end |
.empty ⇒ Object
71 72 73 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 71 def self.empty new(symbol: " ", fg: nil, bg: nil, underline_color: nil, modifiers: []) end |
.symbol(symbol) ⇒ Object
109 110 111 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 109 def self.symbol(symbol) new(symbol:, fg: nil, bg: nil, underline_color: nil, modifiers: []) end |
Instance Method Details
#==(other) ⇒ Object
Checks equality with another Cell.
181 182 183 184 185 186 187 188 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 181 def ==(other) other.is_a?(Cell) && char == other.char && fg == other.fg && bg == other.bg && underline_color == other.underline_color && modifiers == other.modifiers end |
#bold? ⇒ Boolean
Returns true if the cell has the bold modifier.
136 137 138 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 136 def bold? modifiers.include?(:bold) end |
#char ⇒ Object
The character displayed in the cell.
Named to match Ratatui’s Cell::symbol() method. Alias for Rubyists who prefer a shorter name.
41 42 43 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 41 def symbol @symbol end |
#crossed_out? ⇒ Boolean
Returns true if the cell has the crossed_out modifier.
176 177 178 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 176 def crossed_out? modifiers.include?(:crossed_out) end |
#deconstruct_keys(keys) ⇒ Object
Support for pattern matching. Supports both :symbol and :char keys.
207 208 209 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 207 def deconstruct_keys(keys) { symbol:, char: symbol, fg:, bg:, underline_color:, modifiers: } end |
#dim? ⇒ Boolean
Returns true if the cell has the dim modifier.
141 142 143 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 141 def dim? modifiers.include?(:dim) end |
#hidden? ⇒ Boolean
Returns true if the cell has the hidden modifier.
171 172 173 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 171 def hidden? modifiers.include?(:hidden) end |
#inspect ⇒ Object
Returns a string representation of the cell.
191 192 193 194 195 196 197 198 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 191 def inspect parts = ["symbol=#{symbol.inspect}"] parts << "fg=#{fg.inspect}" if fg parts << "bg=#{bg.inspect}" if bg parts << "underline_color=#{underline_color.inspect}" if underline_color parts << "modifiers=#{modifiers.inspect}" unless modifiers.empty? "#<#{self.class} #{parts.join(' ')}>" end |
#italic? ⇒ Boolean
Returns true if the cell has the italic modifier.
146 147 148 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 146 def italic? modifiers.include?(:italic) end |
#rapid_blink? ⇒ Boolean
Returns true if the cell has the rapid_blink modifier.
161 162 163 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 161 def rapid_blink? modifiers.include?(:rapid_blink) end |
#reversed? ⇒ Boolean
Returns true if the cell has the reversed modifier.
166 167 168 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 166 def reversed? modifiers.include?(:reversed) end |
#slow_blink? ⇒ Boolean
Returns true if the cell has the slow_blink modifier.
156 157 158 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 156 def slow_blink? modifiers.include?(:slow_blink) end |
#to_s ⇒ Object
Returns the cell’s character.
201 202 203 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 201 def to_s symbol end |
#underlined? ⇒ Boolean
Returns true if the cell has the underlined modifier.
151 152 153 |
# File 'lib/ratatui_ruby/buffer/cell.rb', line 151 def underlined? modifiers.include?(:underlined) end |