wijjit.terminal.cell.Cell
- class wijjit.terminal.cell.Cell(char, fg_color=None, bg_color=None, bold=False, italic=False, underline=False, reverse=False, dim=False, _style_mask=0)[source]
A single terminal cell with character and styling attributes.
This represents one character position in the terminal with associated colors and text attributes. Forms the foundation of the cell-based rendering system.
Wide characters
A width-2 glyph (most CJK, many emoji) is stored as two cells: a head cell holding the full glyph and a following continuation cell whose
charisCONTINUATION_CHAR(the empty string) carrying the same style attributes as the head. Useis_continuation()to detect the trailing cell. Emitters skip continuation cells because printing the head glyph already advances the terminal by two columns. Zero-width combining marks (e.g. an NFD-decomposed accent) are folded onto the base glyph as a single multi-code-pointcharon one cell.- param char:
Single character or empty string
- type char:
- param fg_color:
Foreground RGB color (0-255 each) or None for default terminal color
- type fg_color:
- param bg_color:
Background RGB color (0-255 each) or None for default terminal color
- type bg_color:
- param bold:
Bold text attribute (default: False)
- type bold:
bool, optional- param italic:
Italic text attribute (default: False)
- type italic:
bool, optional- param underline:
Underline text attribute (default: False)
- type underline:
bool, optional- param reverse:
Reverse video (swap fg/bg colors) attribute (default: False)
- type reverse:
bool, optional- param dim:
Dim/faint text attribute (default: False)
- type dim:
bool, optional- ivar char:
The character to display
- vartype char:
- ivar fg_color:
Foreground color in RGB
- vartype fg_color:
- ivar bg_color:
Background color in RGB
- vartype bg_color:
- ivar bold:
Bold attribute
- vartype bold:
- ivar italic:
Italic attribute
- vartype italic:
- ivar underline:
Underline attribute
- vartype underline:
- ivar reverse:
Reverse video attribute
- vartype reverse:
- ivar dim:
Dim attribute
- vartype dim:
- ivar _style_mask:
Pre-computed bitmask of style attributes for fast equality comparison. Computed in __post_init__. Bit layout: bold=1, italic=2, underline=4, reverse=8, dim=16.
- vartype _style_mask:
Notes
RGB color values should be in the range 0-255. The terminal emulator will handle conversion to its native color format.
The _style_mask field is an optimization for equality comparison. Instead of comparing 5 boolean fields individually, we compare a single integer. This reduces comparison overhead in hot rendering paths.
Examples
Create a simple cell with a character:
>>> cell = Cell('A') >>> cell.char 'A'
Create a styled cell with color and bold:
>>> cell = Cell('X', fg_color=(255, 0, 0), bold=True) >>> cell.fg_color (255, 0, 0) >>> cell.bold True
- __init__(char, fg_color=None, bg_color=None, bold=False, italic=False, underline=False, reverse=False, dim=False, _style_mask=0)
Methods
__init__(char[, fg_color, bg_color, bold, ...])clone()Create a deep copy of this cell.
Get ANSI style codes for this cell without character or reset.
to_ansi()Convert cell to ANSI escape sequence string.
Attributes
- __eq__(other)[source]
Check equality between two cells.
- Parameters:
other (
object) – Object to compare against- Returns:
True if cells have identical content and styling
- Return type:
Notes
This optimized comparison uses pre-computed style masks to reduce the number of comparisons from 8 to 4 (char, style_mask, fg, bg). This is critical for diff rendering performance as it’s called for every cell during buffer comparison.
Comparison order is optimized for early exit: 1. char - most likely to differ between cells 2. _style_mask - single int comparison for 5 boolean fields 3. fg_color - often differs for styled content 4. bg_color - least likely to differ
- to_ansi()[source]
Convert cell to ANSI escape sequence string.
- Returns:
Character with ANSI styling codes
- Return type:
Notes
This generates the ANSI sequence needed to render this cell in a terminal. Used by the diff renderer to output styled text.
When
NO_COLORis in effect, foreground and background colors are omitted while text attributes (bold, underline, reverse, …) are kept. Reverse video in particular is how focus stays visible without color.Examples
>>> cell = Cell('A', fg_color=(255, 0, 0), bold=True) >>> ansi = cell.to_ansi() >>> '\x1b[' in ansi # Contains ANSI codes True
- get_style_codes()[source]
Get ANSI style codes for this cell without character or reset.
- Returns:
ANSI escape sequence for styling (without character or reset)
- Return type:
Notes
This is used by the diff renderer to emit style changes without resetting after each cell. The reset is handled at end of styled regions or end of lines.
Colors are omitted when
NO_COLORis in effect; seeto_ansi().Examples
>>> cell = Cell('A', fg_color=(255, 0, 0), bold=True) >>> codes = cell.get_style_codes() >>> codes '\x1b[1;38;2;255;0;0m'