Skip to content

Rule Reference

Bases: Gradient

A Rich Rule that supports gradients via the Gradient base class.

Parameters:

Name Type Description Default
title Optional[str]

Text shown in the middle of the rule, e.g. "Section 2". Defaults to None.

None
title_style StyleType

Style layered over the gradient on the title. A style string or rich.style.Style, e.g. "dim italic". Unlike style, a color here is shown, drawn on top of the gradient. Defaults to "bold".

'bold'
colors Optional[Sequence[ColorType]]

Foreground color stops. Each stop may be a color name (Rich/ANSI names such as "red" keep Rich's meaning; CSS names such as "tomato" also work), a hex string ("#f00", "#ff0000"), an "rgb(255, 0, 0)" string, an (r, g, b) tuple, or a rich.color.Color, e.g. ["#f00", "orange", (0, 128, 255)]. If omitted and rainbow is False, hues colors are taken from the spectrum palette. Defaults to None.

None
bg_colors Optional[Sequence[ColorType]]

Background color stops, in the same formats as colors, e.g. ["#000033", "#330033"]. If omitted, no background gradient is applied. Defaults to None.

None
rainbow bool

Use the full 17-color spectrum palette, ignoring colors and hues. Defaults to False.

False
hues int

Number of colors to take from the spectrum palette when colors is omitted (a run of consecutive hues with a random starting point). Valid values are 2 to 17. Defaults to 17.

17
thickness int

Line weight: 0 (─), 1 (━), 2 (═), or 3 (█). Ignored when characters is given. Defaults to 1.

1
characters Optional[str]

A single line character, e.g. "─", "━", "═", "█", or "*". None picks it from thickness. Defaults to None.

None
style StyleType

Base style of the line, under the gradient. A style string or rich.style.Style, e.g. "dim italic". A foreground color in this style is replaced by the gradient; a background color is kept unless bg_colors is set. Defaults to "".

''
end str

String printed after the rule. Defaults to a newline.

'\n'
align AlignMethod

Alignment of the title within the rule: "left", "center", or "right". Defaults to "center".

'center'
console Optional[Console]

Console used for rendering and measurement. Defaults to rich.get_console().

None
animate bool

Mark the content as changing between renders, so highlight matches are recomputed every frame instead of cached. Static renderables never animate on their own; use the Animated* classes for live animation. Defaults to False.

False
Source code in src/rich_gradient/rule.py
class Rule(Gradient):
    """A Rich Rule that supports gradients via the Gradient base class.

    Args:
        title (Optional[str]): Text shown in the middle of the rule, e.g.
            ``"Section 2"``. Defaults to None.
        title_style (StyleType): Style layered over the gradient on the title. A style
            string or ``rich.style.Style``, e.g. ``"dim italic"``. Unlike ``style``, a
            color here is shown, drawn on top of the gradient. Defaults to ``"bold"``.
        colors (Optional[Sequence[ColorType]]): Foreground color stops. Each stop may be
            a color name (Rich/ANSI names such as ``"red"`` keep Rich's meaning; CSS
            names such as ``"tomato"`` also work), a hex string (``"#f00"``,
            ``"#ff0000"``), an ``"rgb(255, 0, 0)"`` string, an ``(r, g, b)`` tuple, or a
            ``rich.color.Color``, e.g. ``["#f00", "orange", (0, 128, 255)]``. If omitted
            and ``rainbow`` is False, ``hues`` colors are taken from the spectrum
            palette. Defaults to None.
        bg_colors (Optional[Sequence[ColorType]]): Background color stops, in the same
            formats as ``colors``, e.g. ``["#000033", "#330033"]``. If omitted, no
            background gradient is applied. Defaults to None.
        rainbow (bool): Use the full 17-color spectrum palette, ignoring ``colors`` and
            ``hues``. Defaults to False.
        hues (int): Number of colors to take from the spectrum palette when ``colors``
            is omitted (a run of consecutive hues with a random starting point). Valid
            values are 2 to 17. Defaults to 17.
        thickness (int): Line weight: ``0`` (``─``), ``1`` (``━``), ``2`` (``═``), or
            ``3`` (``█``). Ignored when ``characters`` is given. Defaults to 1.
        characters (Optional[str]): A single line character, e.g. ``"─"``, ``"━"``,
            ``"═"``, ``"█"``, or ``"*"``. ``None`` picks it from ``thickness``.
            Defaults to None.
        style (StyleType): Base style of the line, under the gradient. A style string or
            ``rich.style.Style``, e.g. ``"dim italic"``. A foreground color in this
            style is replaced by the gradient; a background color is kept unless
            ``bg_colors`` is set. Defaults to ``""``.
        end (str): String printed after the rule. Defaults to a newline.
        align (AlignMethod): Alignment of the title within the rule: ``"left"``,
            ``"center"``, or ``"right"``. Defaults to ``"center"``.
        console (Optional[Console]): Console used for rendering and measurement.
            Defaults to ``rich.get_console()``.
        animate (bool): Mark the content as changing between renders, so highlight
            matches are recomputed every frame instead of cached. Static renderables
            never animate on their own; use the ``Animated*`` classes for live
            animation. Defaults to False.
    """

    def __init__(
        self,
        title: str | None = None,
        title_style: StyleType = "bold",
        colors: Sequence[ColorType] | None = None,
        bg_colors: Sequence[ColorType] | None = None,
        *,
        rainbow: bool = False,
        hues: int = 17,
        thickness: int = 1,
        characters: str | None = None,
        style: StyleType = "",
        end: str = "\n",
        align: AlignMethod = "center",
        console: Console | None = None,
        animate: bool = False,
    ) -> None:
        """Initialize the gradient rule; see the class docstring for arguments.

        Raises:
            ValueError: If a color is invalid, ``thickness``/``characters`` is not
                supported, or ``hues`` is out of range.
        """
        self.title = title or ""
        self.title_style = title_style
        self.thickness = thickness
        self.characters = characters or CHARACTER_MAP.get(thickness, CHARACTER_MAP[2])

        # Build the underlying Rich Rule renderable
        base_rule = RichRule(
            title=title or "",
            characters=self.characters,
            style=style,
            end=end,
            align=align,
        )

        try:
            if self.title:
                highlight_words = {title or "": title_style or "bold"}
            else:
                highlight_words = None

            super().__init__(
                base_rule,
                colors=list(colors) if colors is not None else None,
                bg_colors=list(bg_colors) if bg_colors is not None else None,
                console=console,
                hues=hues,
                rainbow=rainbow,
                vertical_justify="middle",
                highlight_words=highlight_words,
                animate=animate,
            )
        except (ColorParseError, TypeError) as err:
            # Bad color inputs raise ColorParseError (unparseable strings) or
            # TypeError (unsupported types). Normalize those into ValueError so
            # Rule callers get one exception type for invalid colors. Other
            # errors, such as an invalid ``hues``, propagate unchanged.
            raise ValueError(f"Invalid color for Rule: {err}") from err

    @property
    def thickness(self) -> int:
        """The line weight, 0-3 (``─``, ``━``, ``═``, ``█``)."""
        if hasattr(self, "_thickness"):
            return self._thickness
        for thickness, char in CHARACTER_MAP.items():
            if char == getattr(self, "_characters", CHARACTER_MAP[2]):
                return thickness
        return 2  # Default

    @thickness.setter
    def thickness(self, value: int) -> None:
        """Set the thickness of the Rule.

        Args:
            value: Thickness as an integer between 0 and 3 (inclusive).

        Raises:
            ValueError: If the value is not an integer between 0 and 3.
        """
        if isinstance(value, int) and 0 <= value <= 3:
            self._thickness = value
            self._characters = CHARACTER_MAP[value]
            return
        raise ValueError(
            f"thickness must be an integer between 0 and 3 (inclusive), got {value!r}"
        )

    @property
    def characters(self) -> str:
        """The character used to draw the rule line.

        Raises:
            ValueError: If no character is set and ``thickness`` is invalid.
            TypeError: If no character is set and ``thickness`` is not an int.
        """
        if self._characters:
            return self._characters

        # Validate thickness
        if not isinstance(self.thickness, int):
            raise TypeError(
                f"thickness must be an integer, received {type(self.thickness).__name__}"
            )
        if not 0 <= self.thickness <= 3:
            raise ValueError("thickness must be an integer between 0 and 3 (inclusive)")

        return CHARACTER_MAP.get(self.thickness, CHARACTER_MAP[2])

    @characters.setter
    def characters(self, value: str) -> None:
        """Set the character used to draw the rule line.

        Args:
            value: A single character, e.g. ``"─"`` or ``"*"``.

        Raises:
            ValueError: If ``value`` is not a one-character string.
        """
        if not isinstance(value, str) or len(value) != 1:
            raise ValueError("characters must be a single character string")
        self._characters = value

    @property
    def title(self) -> TextType | None:
        """The title text of the rule, or ``None``."""
        return self._title or None

    @title.setter
    def title(self, value: TextType | None) -> None:
        """Set the title of the rule.

        Args:
            value: The new title, or ``None`` to clear it.

        Raises:
            TypeError: If ``value`` is not a ``str``, Rich ``Text``, or ``Text``.
        """
        if value is not None and not isinstance(value, (str, RichText, Text)):
            raise TypeError(
                f"title must be str, RichText, or Text, got {type(value).__name__}"
            )
        self._title = value

    @property
    def title_style(self) -> StyleType | None:
        """The style applied to the title, or ``None``."""
        return self._title_style or None

    @title_style.setter
    def title_style(self, value: StyleType | None) -> None:
        """Set the style applied to the title.

        Args:
            value: A style string such as ``"bold"``, a ``Style``, or ``None``.

        Raises:
            TypeError: If ``value`` is not a ``str`` or ``Style``.
        """
        if value is not None and not isinstance(value, (str, Style)):
            raise TypeError(
                f"title_style must be str or Style, got {type(value).__name__}"
            )
        self._title_style = Style.parse(str(value)) if value is not None else None

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        """Render the rule at full width with gradient coloring.

        This overrides the base Gradient alignment wrapper to ensure the
        RichRule expands to the console width (so the line isn't collapsed
        to a single character when aligned/padded externally).

        Args:
            console: The console being rendered to.
            options: The console options for the current render.

        Yields:
            Styled segments for the rule line.
        """
        # Render underlying content directly (no Align wrapper)
        content = self.renderables[0] if self.renderables else RichText("")

        yield from self._render_styled_lines(
            console,
            options,
            content,
            trailing_newline=self.trailing_newline,
        )

characters property writable

The character used to draw the rule line.

Raises:

Type Description
ValueError

If no character is set and thickness is invalid.

TypeError

If no character is set and thickness is not an int.

thickness property writable

The line weight, 0-3 (─, ━, ═, █).

title property writable

The title text of the rule, or None.

title_style property writable

The style applied to the title, or None.

__init__(title=None, title_style='bold', colors=None, bg_colors=None, *, rainbow=False, hues=17, thickness=1, characters=None, style='', end='\n', align='center', console=None, animate=False)

Initialize the gradient rule; see the class docstring for arguments.

Raises:

Type Description
ValueError

If a color is invalid, thickness/characters is not supported, or hues is out of range.

Source code in src/rich_gradient/rule.py
def __init__(
    self,
    title: str | None = None,
    title_style: StyleType = "bold",
    colors: Sequence[ColorType] | None = None,
    bg_colors: Sequence[ColorType] | None = None,
    *,
    rainbow: bool = False,
    hues: int = 17,
    thickness: int = 1,
    characters: str | None = None,
    style: StyleType = "",
    end: str = "\n",
    align: AlignMethod = "center",
    console: Console | None = None,
    animate: bool = False,
) -> None:
    """Initialize the gradient rule; see the class docstring for arguments.

    Raises:
        ValueError: If a color is invalid, ``thickness``/``characters`` is not
            supported, or ``hues`` is out of range.
    """
    self.title = title or ""
    self.title_style = title_style
    self.thickness = thickness
    self.characters = characters or CHARACTER_MAP.get(thickness, CHARACTER_MAP[2])

    # Build the underlying Rich Rule renderable
    base_rule = RichRule(
        title=title or "",
        characters=self.characters,
        style=style,
        end=end,
        align=align,
    )

    try:
        if self.title:
            highlight_words = {title or "": title_style or "bold"}
        else:
            highlight_words = None

        super().__init__(
            base_rule,
            colors=list(colors) if colors is not None else None,
            bg_colors=list(bg_colors) if bg_colors is not None else None,
            console=console,
            hues=hues,
            rainbow=rainbow,
            vertical_justify="middle",
            highlight_words=highlight_words,
            animate=animate,
        )
    except (ColorParseError, TypeError) as err:
        # Bad color inputs raise ColorParseError (unparseable strings) or
        # TypeError (unsupported types). Normalize those into ValueError so
        # Rule callers get one exception type for invalid colors. Other
        # errors, such as an invalid ``hues``, propagate unchanged.
        raise ValueError(f"Invalid color for Rule: {err}") from err

__rich_console__(console, options)

Render the rule at full width with gradient coloring.

This overrides the base Gradient alignment wrapper to ensure the RichRule expands to the console width (so the line isn't collapsed to a single character when aligned/padded externally).

Parameters:

Name Type Description Default
console Console

The console being rendered to.

required
options ConsoleOptions

The console options for the current render.

required

Yields:

Type Description
RenderResult

Styled segments for the rule line.

Source code in src/rich_gradient/rule.py
def __rich_console__(
    self, console: Console, options: ConsoleOptions
) -> RenderResult:
    """Render the rule at full width with gradient coloring.

    This overrides the base Gradient alignment wrapper to ensure the
    RichRule expands to the console width (so the line isn't collapsed
    to a single character when aligned/padded externally).

    Args:
        console: The console being rendered to.
        options: The console options for the current render.

    Yields:
        Styled segments for the rule line.
    """
    # Render underlying content directly (no Align wrapper)
    content = self.renderables[0] if self.renderables else RichText("")

    yield from self._render_styled_lines(
        console,
        options,
        content,
        trailing_newline=self.trailing_newline,
    )