Class: RatatuiRuby::Cell

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#bgObject (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

#fgObject (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

#symbolObject (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_colorObject (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

.defaultObject

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

.emptyObject

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.

Returns:

  • (Boolean)


133
134
135
# File 'lib/ratatui_ruby/cell.rb', line 133

def bold?
  modifiers.include?("bold")
end

#charObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


168
169
170
# File 'lib/ratatui_ruby/cell.rb', line 168

def hidden?
  modifiers.include?("hidden")
end

#inspectObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


153
154
155
# File 'lib/ratatui_ruby/cell.rb', line 153

def slow_blink?
  modifiers.include?("slow_blink")
end

#to_sObject

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.

Returns:

  • (Boolean)


148
149
150
# File 'lib/ratatui_ruby/cell.rb', line 148

def underlined?
  modifiers.include?("underlined")
end