wijjit.styling.style.Style
- class wijjit.styling.style.Style(fg_color=None, bg_color=None, bold=None, italic=None, underline=None, dim=None, reverse=None)[source]
CSS-like style specification for terminal UI elements.
This class represents a complete style specification including colors and text attributes. Styles can be merged using CSS-like cascade rules.
- Parameters:
fg_color (
tupleof(int,int,int)orNone, optional) – Foreground RGB color (0-255 each) or None for default terminal colorbg_color (
tupleof(int,int,int)orNone, optional) – Background RGB color (0-255 each) or None for default terminal colorbold (
boolorNone, optional) – Bold text attribute. None means unspecified (default: None)italic (
boolorNone, optional) – Italic text attribute. None means unspecified (default: None)underline (
boolorNone, optional) – Underline text attribute. None means unspecified (default: None)dim (
boolorNone, optional) – Dim/faint text attribute. None means unspecified (default: None)reverse (
boolorNone, optional) – Reverse video (swap fg/bg colors) attribute. None means unspecified (default: None)
- Variables:
fg_color (
tupleof(int,int,int)orNone) – Foreground color in RGBbg_color (
tupleof(int,int,int)orNone) – Background color in RGBitalic (
boolorNone) – Italic attribute (None = unspecified)underline (
boolorNone) – Underline attribute (None = unspecified)reverse (
boolorNone) – Reverse video attribute (None = unspecified)
Examples
Create a basic style:
>>> style = Style(fg_color=(255, 0, 0), bold=True) >>> style.fg_color (255, 0, 0) >>> style.bold True
Merge two styles (CSS cascade):
>>> base = Style(fg_color=(255, 0, 0), bold=True) >>> override = Style(fg_color=(0, 255, 0)) # New color, keeps bold >>> merged = base.merge(override) >>> merged.fg_color (0, 255, 0) >>> merged.bold True
Disable attributes via merge:
>>> base = Style(bold=True, italic=True) >>> override = Style(bold=False) # Explicitly disable bold >>> merged = base.merge(override) >>> merged.bold False >>> merged.italic True
Convert to Cell attributes:
>>> style = Style(fg_color=(255, 0, 0), bold=True) >>> attrs = style.to_cell_attrs() >>> attrs['fg_color'] (255, 0, 0) >>> attrs['bold'] True
- __init__(fg_color=None, bg_color=None, bold=None, italic=None, underline=None, dim=None, reverse=None)
Methods
__init__([fg_color, bg_color, bold, italic, ...])merge(other)Merge another style on top of this one (CSS cascade).
Convert style to Cell constructor attributes.
Attributes
- merge(other)[source]
Merge another style on top of this one (CSS cascade).
- Parameters:
other (
Style) – Style to overlay on top of this style- Returns:
New style with other’s non-None values overriding self’s values
- Return type:
Notes
This implements CSS-like cascade behavior where properties from ‘other’ override properties from ‘self’. For color properties, None means “use default” while a color tuple overrides. For boolean attributes, True overrides False.
The merge is non-destructive - both input styles remain unchanged.
Examples
Merge colors:
>>> base = Style(fg_color=(255, 0, 0)) >>> override = Style(fg_color=(0, 255, 0)) >>> merged = base.merge(override) >>> merged.fg_color (0, 255, 0)
Merge attributes:
>>> base = Style(bold=True) >>> override = Style(italic=True) >>> merged = base.merge(override) >>> merged.bold True >>> merged.italic True
Override with None preserves base:
>>> base = Style(fg_color=(255, 0, 0)) >>> override = Style(bg_color=(0, 0, 255)) >>> merged = base.merge(override) >>> merged.fg_color (255, 0, 0) >>> merged.bg_color (0, 0, 255)
- to_cell_attrs()[source]
Convert style to Cell constructor attributes.
- Returns:
Dictionary of attributes suitable for
Cell(**attrs)- Return type:
Notes
This is the PRIMARY method for converting styles to cell attributes in the cell-based rendering system. Returns a dictionary that can be unpacked directly into Cell constructor.
Examples
Convert to cell attributes:
>>> style = Style(fg_color=(255, 0, 0), bold=True) >>> attrs = style.to_cell_attrs() >>> attrs {'fg_color': (255, 0, 0), 'bg_color': None, 'bold': True, 'italic': False, 'underline': False, 'reverse': False, 'dim': False}
Use with Cell:
>>> from wijjit.terminal.cell import Cell >>> style = Style(fg_color=(0, 255, 0)) >>> cell = Cell('A', **style.to_cell_attrs()) >>> cell.fg_color (0, 255, 0)
- __bool__()[source]
Check if style has any non-default properties.
- Returns:
True if any style property is set, False if default/empty
- Return type:
Notes
Useful for conditionally applying styles or checking if a style has any effect.
Examples
Empty style is falsy:
>>> style = Style() >>> bool(style) False
Styled is truthy:
>>> style = Style(bold=True) >>> bool(style) True