rich.style

class rich.style.Style(*, color: Union[rich.color.Color, str] = None, bgcolor: Union[rich.color.Color, str] = None, bold: bool = None, dim: bool = None, italic: bool = None, underline: bool = None, blink: bool = None, blink2: bool = None, reverse: bool = None, conceal: bool = None, strike: bool = None, underline2: bool = None, frame: bool = None, encircle: bool = None, overline: bool = None, link: str = 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.

property bgcolor

The background color or None if it is not set.

classmethod chain(*styles: rich.style.Style)rich.style.Style[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

property color

The foreground color or None if it is not set.

classmethod combine(styles: Iterable[Style])rich.style.Style[source]

Combine styles and get result.

Parameters

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

Returns

A new style instance.

Return type

Style

copy()rich.style.Style[source]

Get a copy of this style.

Returns

A new Style instance with identical attributes.

Return type

Style

classmethod empty()rich.style.Style[source]

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

get_html_style(theme: rich.terminal_theme.TerminalTheme = None) → str[source]

Get a CSS style rule.

Link text, if set.

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

classmethod normalize(style: str) → str[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 parse(style_definition: str)Style[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: Optional[Union[str, Style]]) → Union[str, rich.style.Style][source]

Pick first non-None style.

render(text: str = '', *, color_system: Optional[rich.color.ColorSystem] = <ColorSystem.TRUECOLOR: 3>, legacy_windows: bool = False) → str[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.

Returns

A string containing ANSI style codes.

Return type

str

test(text: Optional[str] = None) → 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.

Returns

Return type

None

property transaprent_background

Check if the style specified a transparent background.

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

A stack of styles.

property current

Get the Style at the top of the stack.

pop()rich.style.Style[source]

Pop last style and discard.

Returns

New current style (also available as stack.current)

Return type

Style

push(style: rich.style.Style) → None[source]

Push a new style on to the stack.

Parameters

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