Class: RatatuiRuby::Cell
- Inherits:
-
Object
- Object
- RatatuiRuby::Cell
- Defined in:
- lib/ratatui_ruby/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: 2025 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").
-
#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.
123 124 125 126 127 128 129 130 |
# File 'lib/ratatui_ruby/cell.rb', line 123 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(&:freeze).freeze freeze end |
Instance Attribute Details
#bg ⇒ Object (readonly)
The background color of the cell (e.g., :black, nil).
46 47 48 |
# File 'lib/ratatui_ruby/cell.rb', line 46 def bg @bg end |
#fg ⇒ Object (readonly)
The foreground color of the cell (e.g., :red, :blue, "#ff0000").
43 44 45 |
# File 'lib/ratatui_ruby/cell.rb', line 43 def fg @fg end |
#symbol ⇒ Object (readonly)
The character displayed in the cell.
Named to match Ratatui's Cell::symbol() method.
37 38 39 |
# File 'lib/ratatui_ruby/cell.rb', line 37 def symbol @symbol end |
#underline_color ⇒ Object (readonly)
The underline color of the cell.
Distinct from foreground color. Some terminals support colored underlines.
51 52 53 |
# File 'lib/ratatui_ruby/cell.rb', line 51 def underline_color @underline_color end |
Class Method Details
.char(char) ⇒ Object
Alias for Rubyists who prefer a shorter name.
112 113 114 |
# File 'lib/ratatui_ruby/cell.rb', line 112 def self.char(char) symbol(char) end |
.default ⇒ Object
Returns a default cell (alias for empty).
Example
-- SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++ Cell.default # => #<RatatuiRuby::Cell char=" ">
-- SPDX-SnippetEnd ++
87 88 89 |
# File 'lib/ratatui_ruby/cell.rb', line 87 def self.default empty end |
.empty ⇒ Object
Returns an empty cell (space character, no styles).
Example
-- SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++ Cell.empty # => #<RatatuiRuby::Cell char=" ">
-- SPDX-SnippetEnd ++
69 70 71 |
# File 'lib/ratatui_ruby/cell.rb', line 69 def self.empty new(symbol: " ", fg: nil, bg: nil, underline_color: nil, modifiers: []) end |
.symbol(symbol) ⇒ Object
Returns a cell with a specific character and no styles.
[symbol] String (single character).
Example
-- SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++ Cell.symbol("X") # => #<RatatuiRuby::Cell symbol="X">
-- SPDX-SnippetEnd ++
107 108 109 |
# File 'lib/ratatui_ruby/cell.rb', line 107 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.
178 179 180 181 182 183 184 185 |
# File 'lib/ratatui_ruby/cell.rb', line 178 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.
133 134 135 |
# File 'lib/ratatui_ruby/cell.rb', line 133 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.
40 41 42 |
# File 'lib/ratatui_ruby/cell.rb', line 40 def symbol @symbol end |
#crossed_out? ⇒ Boolean
Returns true if the cell has the crossed_out modifier.
173 174 175 |
# File 'lib/ratatui_ruby/cell.rb', line 173 def crossed_out? modifiers.include?("crossed_out") end |
#deconstruct_keys(keys) ⇒ Object
Support for pattern matching. Supports both :symbol and :char keys.
204 205 206 |
# File 'lib/ratatui_ruby/cell.rb', line 204 def deconstruct_keys(keys) { symbol:, char: symbol, fg:, bg:, underline_color:, modifiers: } end |
#dim? ⇒ Boolean
Returns true if the cell has the dim modifier.
138 139 140 |
# File 'lib/ratatui_ruby/cell.rb', line 138 def dim? modifiers.include?("dim") end |
#hidden? ⇒ Boolean
Returns true if the cell has the hidden modifier.
168 169 170 |
# File 'lib/ratatui_ruby/cell.rb', line 168 def hidden? modifiers.include?("hidden") end |
#inspect ⇒ Object
Returns a string representation of the cell.
188 189 190 191 192 193 194 195 |
# File 'lib/ratatui_ruby/cell.rb', line 188 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.
143 144 145 |
# File 'lib/ratatui_ruby/cell.rb', line 143 def italic? modifiers.include?("italic") end |
#rapid_blink? ⇒ Boolean
Returns true if the cell has the rapid_blink modifier.
158 159 160 |
# File 'lib/ratatui_ruby/cell.rb', line 158 def rapid_blink? modifiers.include?("rapid_blink") end |
#reversed? ⇒ Boolean
Returns true if the cell has the reversed modifier.
163 164 165 |
# File 'lib/ratatui_ruby/cell.rb', line 163 def reversed? modifiers.include?("reversed") end |
#slow_blink? ⇒ Boolean
Returns true if the cell has the slow_blink modifier.
153 154 155 |
# File 'lib/ratatui_ruby/cell.rb', line 153 def slow_blink? modifiers.include?("slow_blink") end |
#to_s ⇒ Object
Returns the cell's character.
198 199 200 |
# File 'lib/ratatui_ruby/cell.rb', line 198 def to_s symbol end |
#underlined? ⇒ Boolean
Returns true if the cell has the underlined modifier.
148 149 150 |
# File 'lib/ratatui_ruby/cell.rb', line 148 def underlined? modifiers.include?("underlined") end |