rich.style

class rich.style.Style(*, color=None, bgcolor=None, bold=None, dim=None, italic=None, underline=None, blink=None, blink2=None, reverse=None, conceal=None, strike=None, underline2=None, frame=None, encircle=None, overline=None, link=None, meta=None)[source]

A terminal style.

A terminal style consists of a color (color), a background color (bgcolor), and a number of attributes, such as bold, italic etc. The attributes have 3 states: they can either be on (True), off (False), or not set (None).

Parameters
  • color (Union[Color, str], optional) – Color of terminal text. Defaults to None.

  • bgcolor (Union[Color, str], optional) – Color of terminal background. Defaults to None.

  • bold (bool, optional) – Enable bold text. Defaults to None.

  • dim (bool, optional) – Enable dim text. Defaults to None.

  • italic (bool, optional) – Enable italic text. Defaults to None.

  • underline (bool, optional) – Enable underlined text. Defaults to None.

  • blink (bool, optional) – Enabled blinking text. Defaults to None.

  • blink2 (bool, optional) – Enable fast blinking text. Defaults to None.

  • reverse (bool, optional) – Enabled reverse text. Defaults to None.

  • conceal (bool, optional) – Enable concealed text. Defaults to None.

  • strike (bool, optional) – Enable strikethrough text. Defaults to None.

  • underline2 (bool, optional) – Enable doubly underlined text. Defaults to None.

  • frame (bool, optional) – Enable framed text. Defaults to None.

  • encircle (bool, optional) – Enable encircled text. Defaults to None.

  • overline (bool, optional) – Enable overlined text. Defaults to None.

  • link (str, link) – Link URL. Defaults to None.

  • meta (Optional[Dict[str, Any]]) –

property background_style: Style

A Style with background only.

property bgcolor: Optional[Color]

The background color or None if it is not set.

classmethod chain(*styles)[source]

Combine styles from positional argument in to a single style.

Parameters

*styles (Iterable[Style]) – Styles to combine.

Returns

A new style instance.

Return type

Style

Get a copy of this style with link and meta information removed.

Returns

New style object.

Return type

Style

property color: Optional[Color]

The foreground color or None if it is not set.

classmethod combine(styles)[source]

Combine styles and get result.

Parameters

styles (Iterable[Style]) – Styles to combine.

Returns

A new style instance.

Return type

Style

copy()[source]

Get a copy of this style.

Returns

A new Style instance with identical attributes.

Return type

Style

classmethod from_color(color=None, bgcolor=None)[source]

Create a new style with colors and no attributes.

Returns

A (foreground) color, or None for no color. Defaults to None. bgcolor (Optional[Color]): A (background) color, or None for no color. Defaults to None.

Return type

color (Optional[Color])

Parameters
classmethod from_meta(meta)[source]

Create a new style with meta data.

Returns

A dictionary of meta data. Defaults to None.

Return type

meta (Optional[Dict[str, Any]])

Parameters

meta (Optional[Dict[str, Any]]) –

get_html_style(theme=None)[source]

Get a CSS style rule.

Parameters

theme (Optional[TerminalTheme]) –

Return type

str

Link text, if set.

Get a link id, used in ansi code for links.

property meta: Dict[str, Any]

Get meta information (can not be changed after construction).

classmethod normalize(style)[source]

Normalize a style definition so that styles with the same effect have the same string representation.

Parameters

style (str) – A style definition.

Returns

Normal form of style definition.

Return type

str

classmethod null()[source]

Create an ‘null’ style, equivalent to Style(), but more performant.

Return type

Style

classmethod on(meta=None, **handlers)[source]

Create a blank style with meta information.

Example

style = Style.on(click=self.on_click)

Parameters
  • meta (Optional[Dict[str, Any]], optional) – An optional dict of meta information.

  • **handlers (Any) – Keyword arguments are translated in to handlers.

Returns

A Style with meta information attached.

Return type

Style

classmethod parse(style_definition)[source]

Parse a style definition.

Parameters

style_definition (str) – A string containing a style.

Raises

errors.StyleSyntaxError – If the style definition syntax is invalid.

Returns

A Style instance.

Return type

Style

classmethod pick_first(*values)[source]

Pick first non-None style.

Parameters

values (Optional[Union[str, Style]]) –

Return type

Union[str, Style]

render(text='', *, color_system=ColorSystem.TRUECOLOR, legacy_windows=False)[source]

Render the ANSI codes for the style.

Parameters
  • text (str, optional) – A string to style. Defaults to “”.

  • color_system (Optional[ColorSystem], optional) – Color system to render to. Defaults to ColorSystem.TRUECOLOR.

  • legacy_windows (bool) –

Returns

A string containing ANSI style codes.

Return type

str

test(text=None)[source]

Write text with style directly to terminal.

This method is for testing purposes only.

Parameters

text (Optional[str], optional) – Text to style or None for style name.

Return type

None

property transparent_background: bool

Check if the style specified a transparent background.

Get a copy with a different value for link.

Parameters

link (str, optional) – New value for link. Defaults to None.

Returns

A new Style instance.

Return type

Style

property without_color: Style

Get a copy of the style with color removed.

class rich.style.StyleStack(default_style)[source]

A stack of styles.

Parameters

default_style (Style) –

property current: Style

Get the Style at the top of the stack.

pop()[source]

Pop last style and discard.

Returns

New current style (also available as stack.current)

Return type

Style

push(style)[source]

Push a new style on to the stack.

Parameters

style (Style) – New style to combine with current style.

Return type

None