struct
Termisu::Cell
- Termisu::Cell
- Struct
- Value
- Object
Overview
Cell represents a single character cell in the terminal buffer.
Cell contains:
- grapheme: The Unicode grapheme cluster (single grapheme for leading cells)
- width: Display column width (0, 1, or 2)
- continuation: True for trailing cell of a wide grapheme
- fg: Foreground color (supports ANSI-8, ANSI-256, and RGB)
- bg: Background color (supports ANSI-8, ANSI-256, and RGB)
- attr: Text attributes (bold, underline, etc.)
Grapheme and Continuation Cells
Wide characters (CJK, emoji) occupy 2 columns. The Cell model represents this:
- Leading cell:
continuation = false,width = 2,#graphemecontains the full grapheme - Trailing cell:
continuation = true,width = 0,#graphemeis empty
Example:
# Leading cell for "中" (width auto-calculated as 2)
lead = Termisu::Cell.new("中")
lead.grapheme # => "中"
lead.width # => 2
lead.continuation? # => false
# Trailing continuation cell
trail = Termisu::Cell.continuation
trail.grapheme # => ""
trail.width # => 0
trail.continuation? # => true
Compatibility (Public API)
The #ch property provides backward-compatible access:
cell = Termisu::Cell.new('A')
cell.ch # => 'A' (first codepoint of grapheme)
continuation = Termisu::Cell.continuation
continuation.ch # => ' ' (space for continuation cells)
Defined in:
termisu/cell.crConstructors
-
.continuation : Cell
Creates a continuation cell for wide graphemes.
-
.default : Cell
Creates a default empty cell (space with default colors, width 1, not continuation).
-
.new(grapheme : String = " ", continuation : Bool = false, fg : Color = Color.white, bg : Color = Color.default, attr : Attribute = Attribute::None)
Creates a new Cell with the specified grapheme and colors.
-
.new(ch : Char, fg : Color = Color.white, bg : Color = Color.default, attr : Attribute = Attribute::None) : self
Creates a Cell from a single character (compatibility constructor).
Instance Method Summary
-
#==(other : Cell) : Bool
Checks if this cell equals another cell.
- #attr : Attribute
- #attr=(attr : Attribute)
- #bg : Color
- #bg=(bg : Color)
-
#ch : Char
Gets the first character of the grapheme (compatibility property).
-
#ch=(value : Char)
Sets the cell to a single character (compatibility setter).
- #continuation? : Bool
-
#default_state? : Bool
Returns true when this cell is the canonical default blank cell.
- #fg : Color
- #fg=(fg : Color)
- #grapheme : String
-
#reset
Resets the cell to default state (space, white on default background, no attributes, width 1, not continuation).
- #width : UInt8
Constructor Detail
Creates a continuation cell for wide graphemes.
Continuation cells represent the trailing column occupied by a wide character. They have empty grapheme, width 0, and are never rendered directly.
trail = Termisu::Cell.continuation
trail.continuation? # => true
trail.width # => 0
trail.grapheme # => ""
Creates a default empty cell (space with default colors, width 1, not continuation).
Creates a new Cell with the specified grapheme and colors.
Parameters:
- grapheme: Unicode grapheme cluster to display (if multi-grapheme string is passed, only the first grapheme cluster is stored)
- continuation: True if this is a trailing cell of a wide grapheme
- fg: Foreground color (default: white)
- bg: Background color (default: default terminal color)
- attr: Text attributes (default: None)
Note: Width is derived from grapheme content to ensure consistency. Continuation cells always have empty grapheme and width 0.
Occupancy invariants enforced:
- Continuation cells: always empty grapheme, width 0
- Empty non-continuation: normalized to default space cell (width 1)
- Leading cells: width derived via grapheme_width (handles VS16, ZWJ, flags)
- Multi-grapheme strings: only first grapheme is stored; debug log warns of truncation
Creates a Cell from a single character (compatibility constructor).
This constructor maintains backward compatibility with the Char-based API. Width is auto-calculated from the character's codepoint.
Note: Control characters (C0/C1) produce width 0 non-continuation cells. This is permitted for internal sentinel usage (e.g., Buffer#invalidate uses NUL cells as invalid markers). The public write path (Buffer#set_cell) guards against control characters before reaching this constructor.
Instance Method Detail
Checks if this cell equals another cell.
Two cells are equal if all fields match: grapheme, width, continuation, fg, bg, attr.
Gets the first character of the grapheme (compatibility property).
Returns:
- First codepoint of grapheme for leading cells
- Space (' ') for continuation cells or empty grapheme
This provides backward compatibility with Char-based API.
Sets the cell to a single character (compatibility setter).
This rewrites the cell to narrow grapheme mode with width calculated from the character's codepoint.
Provides backward compatibility with Char-based API.
Returns true when this cell is the canonical default blank cell.
Used by Buffer hot paths (clear/dirtiness accounting) to avoid expensive full-buffer work when rows are already blank.
Resets the cell to default state (space, white on default background, no attributes, width 1, not continuation).