Class: RatatuiRuby::Buffer::Cell

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

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, 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

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

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

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

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

.defaultObject

Returns a default cell (alias for empty).

Example

– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++

Buffer::Cell.default # => #<RatatuiRuby::Buffer::Cell char=" ">

– SPDX-SnippetEnd ++



89
90
91
# File 'lib/ratatui_ruby/buffer/cell.rb', line 89

def self.default
  empty
end

.emptyObject

Returns an empty cell (space character, no styles).

Example

– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++

Buffer::Cell.empty # => #<RatatuiRuby::Buffer::Cell char=" ">

– SPDX-SnippetEnd ++



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

Returns a cell with a specific character and no styles.

symbol

String (single character).

Example

– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++

Buffer::Cell.symbol("X") # => #<RatatuiRuby::Buffer::Cell symbol="X">

– SPDX-SnippetEnd ++



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.

Returns:

  • (Boolean)


136
137
138
# File 'lib/ratatui_ruby/buffer/cell.rb', line 136

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.



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


171
172
173
# File 'lib/ratatui_ruby/buffer/cell.rb', line 171

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

#inspectObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


156
157
158
# File 'lib/ratatui_ruby/buffer/cell.rb', line 156

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

#to_sObject

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.

Returns:

  • (Boolean)


151
152
153
# File 'lib/ratatui_ruby/buffer/cell.rb', line 151

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