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 (tuple of (int, int, int) or None, optional) – Foreground RGB color (0-255 each) or None for default terminal color

  • bg_color (tuple of (int, int, int) or None, optional) – Background RGB color (0-255 each) or None for default terminal color

  • bold (bool or None, optional) – Bold text attribute. None means unspecified (default: None)

  • italic (bool or None, optional) – Italic text attribute. None means unspecified (default: None)

  • underline (bool or None, optional) – Underline text attribute. None means unspecified (default: None)

  • dim (bool or None, optional) – Dim/faint text attribute. None means unspecified (default: None)

  • reverse (bool or None, optional) – Reverse video (swap fg/bg colors) attribute. None means unspecified (default: None)

Variables:
  • fg_color (tuple of (int, int, int) or None) – Foreground color in RGB

  • bg_color (tuple of (int, int, int) or None) – Background color in RGB

  • bold (bool or None) – Bold attribute (None = unspecified)

  • italic (bool or None) – Italic attribute (None = unspecified)

  • underline (bool or None) – Underline attribute (None = unspecified)

  • dim (bool or None) – Dim attribute (None = unspecified)

  • reverse (bool or None) – 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)
Parameters:
Return type:

None

Methods

__init__([fg_color, bg_color, bold, italic, ...])

merge(other)

Merge another style on top of this one (CSS cascade).

to_cell_attrs()

Convert style to Cell constructor attributes.

Attributes

bg_color

bold

dim

fg_color

italic

reverse

underline

fg_color: tuple[int, int, int] | None = None
bg_color: tuple[int, int, int] | None = None
bold: bool | None = None
italic: bool | None = None
underline: bool | None = None
dim: bool | None = None
reverse: bool | None = None
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:

Style

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:

dict

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:

bool

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