Skip to content

xnano.styles

xnano.styles

xnano.styles

Colors, styles and styled text.

A Style holds colors and text modifiers; styled text is built from spans and lines that each carry their own style.

Classes:

  • Color –

    A terminal color: a named palette color, a palette index or a true color.

  • Style –

    A foreground color, a background color and text modifiers.

  • StyledText –

    Text made of differently styled spans.

  • TextLine –

    One wrapped line of a TextLayout.

  • TextSpan –

    A run of text with one style.

Attributes:

ColorName module-attribute

ColorName: TypeAlias = Literal['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'gray', 'grey', 'dark_gray', 'dark_grey', 'darkgray', 'darkgrey', 'light_black', 'light_red', 'bright_red', 'light_green', 'bright_green', 'light_yellow', 'bright_yellow', 'light_blue', 'bright_blue', 'light_magenta', 'bright_magenta', 'light_cyan', 'bright_cyan', 'white', 'reset']

A named ANSI terminal color.

gray and grey are the same color, and so are light_ and bright_. dark_gray is the dim gray. reset restores the terminal default.

ColorValue module-attribute

ColorValue: TypeAlias = 'Color | ColorName | str | tuple[int, int, int]'

A color, an ANSI color name, a '#rrggbb' string, or a (red, green, blue) tuple.

A Tailwind name such as 'slate-400' is a TailwindColorBinding. It is not parsed by Color.

TextContent module-attribute

TextContent: TypeAlias = 'str | StyledText | Sequence[str | StyledText]'

Text accepted by writing operations and components.

Color

Color(value: ColorName | str | tuple[int, int, int])

A terminal color: a named palette color, a palette index or a true color.

Maps to ratatui::style::Color.

Parameters:

  • value (ColorName | str | tuple[int, int, int]) –

    A color name such as 'red' or 'light_blue', a hex string such as '#ff8800', a palette index string such as '4', or a (red, green, blue) tuple.

Attributes:

Raises:

Examples:

assert Color('#ff8800').to_rgb() == (255, 136, 0)
assert Color((1, 2, 3)) == Color.from_rgb(1, 2, 3)
assert Color('red').to_rgb() is None

Create a Color.

Parameters:

Methods:

  • parse –

    Parse a color name, hex string or palette index string.

  • from_tailwind –

    A true color from a Tailwind palette and shade.

  • indexed –

    A color from the 256-color palette.

  • from_rgb –

    A true color from red, green and blue values between 0 and 255.

  • to_rgb –

    The (red, green, blue) values of a true color, or None for named and indexed colors.

  • to_hex –

    The color as a '#rrggbb' string.

  • from_hsl –

    A true color from hue, saturation and lightness.

  • from_hsv –

    A true color from hue, saturation and value.

  • random –

    A random true color, or a random shade of base.

  • to_hsl –

    The hue (degrees), saturation and lightness (percentages) of the color.

  • to_hsv –

    The hue (degrees), saturation and value (percentages) of the color.

  • lerp –

    The color between this one and other.

  • lighten –

    A lighter color for a positive amount, a darker one for a negative amount.

  • darken –

    A darker color.

  • inverse –

    The color that has each red, green and blue channel inverted.

  • saturate –

    The color with its saturation scaled.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, value: ColorName | str | tuple[int, int, int]) -> None:
    """
    Create a Color.

    Args:
        value: ``ColorName | str | tuple[int, int, int]``.
    """

black class-attribute

black: Color

The black terminal color.

red class-attribute

red: Color

The red terminal color.

green class-attribute

green: Color

The green terminal color.

yellow class-attribute

yellow: Color

The yellow terminal color.

blue class-attribute

blue: Color

The blue terminal color.

magenta class-attribute

magenta: Color

The magenta terminal color.

cyan class-attribute

cyan: Color

The cyan terminal color.

gray class-attribute

gray: Color

The gray terminal color.

dark_gray class-attribute

dark_gray: Color

The dark gray terminal color.

light_red class-attribute

light_red: Color

The light red terminal color.

light_green class-attribute

light_green: Color

The light green terminal color.

light_yellow class-attribute

light_yellow: Color

The light yellow terminal color.

light_blue class-attribute

light_blue: Color

The light blue terminal color.

light_magenta class-attribute

light_magenta: Color

The light magenta terminal color.

light_cyan class-attribute

light_cyan: Color

The light cyan terminal color.

white class-attribute

white: Color

The white terminal color.

reset class-attribute

reset: Color

The reset terminal color, which follows the terminal theme.

is_reset property

is_reset: bool

Whether this is the reset color, which follows the terminal's theme.

parse classmethod

parse(value: ColorName | str) -> Color

Parse a color name, hex string or palette index string.

Examples:

assert Color.parse('light_blue') == Color('light_blue')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def parse(cls, value: ColorName | str) -> Color:
    """
    Parse a color name, hex string or palette index string.

    Examples:
        ```python
        assert Color.parse('light_blue') == Color('light_blue')
        ```
    """

from_tailwind classmethod

from_tailwind(palette: TailwindColorName, shade: TailwindColorShade) -> Color

A true color from a Tailwind palette and shade.

Color does not read a name such as 'slate-400'. Pass the palette and the shade here.

Parameters:

Raises:

  • ValueError –

    palette is not a palette, or shade is not a shade.

Examples:

assert Color.from_tailwind('red', 500).to_rgb() == (239, 68, 68)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def from_tailwind(
    cls, palette: TailwindColorName, shade: TailwindColorShade
) -> Color:
    """
    A true color from a Tailwind palette and shade.

    ``Color`` does not read a name such as ``'slate-400'``. Pass the palette and the
    shade here.

    Args:
        palette: A palette such as ``'slate'`` or ``'red'``.
        shade: ``50``, ``100`` through ``900``, or ``950``.

    Raises:
        ValueError: ``palette`` is not a palette, or ``shade`` is not a shade.

    Examples:
        ```python
        assert Color.from_tailwind('red', 500).to_rgb() == (239, 68, 68)
        ```
    """

indexed classmethod

indexed(index: int) -> Color

A color from the 256-color palette.

Examples:

color = Color.indexed(202)
assert color.to_rgb() is None
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def indexed(cls, index: int) -> Color:
    """
    A color from the 256-color palette.

    Examples:
        ```python
        color = Color.indexed(202)
        assert color.to_rgb() is None
        ```
    """

from_rgb classmethod

from_rgb(red: int, green: int, blue: int) -> Color

A true color from red, green and blue values between 0 and 255.

Examples:

assert Color.from_rgb(255, 0, 0).to_rgb() == (255, 0, 0)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def from_rgb(cls, red: int, green: int, blue: int) -> Color:
    """
    A true color from red, green and blue values between 0 and 255.

    Examples:
        ```python
        assert Color.from_rgb(255, 0, 0).to_rgb() == (255, 0, 0)
        ```
    """

to_rgb

to_rgb() -> tuple[int, int, int] | None

The (red, green, blue) values of a true color, or None for named and indexed colors.

Examples:

assert Color('#010203').to_rgb() == (1, 2, 3)
assert Color.indexed(4).to_rgb() is None
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def to_rgb(self) -> tuple[int, int, int] | None:
    """
    The ``(red, green, blue)`` values of a true color, or ``None`` for named and indexed colors.

    Examples:
        ```python
        assert Color('#010203').to_rgb() == (1, 2, 3)
        assert Color.indexed(4).to_rgb() is None
        ```
    """

to_hex

to_hex() -> str

The color as a '#rrggbb' string.

Only a true color has one, as with to_rgb.

Raises:

  • ValueError –

    The color is named, indexed or reset, which the terminal's theme chooses.

Examples:

assert Color((255, 136, 0)).to_hex() == '#ff8800'
assert Color('#FF8800').to_hex() == '#ff8800'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def to_hex(self) -> str:
    """
    The color as a ``'#rrggbb'`` string.

    Only a true color has one, as with [`to_rgb`][xnano_core._xnano_core.Color.to_rgb].

    Raises:
        ValueError: The color is named, indexed or ``reset``, which the terminal's theme
            chooses.

    Examples:
        ```python
        assert Color((255, 136, 0)).to_hex() == '#ff8800'
        assert Color('#FF8800').to_hex() == '#ff8800'
        ```
    """

from_hsl classmethod

from_hsl(hue: float, saturation: float, lightness: float) -> Color

A true color from hue, saturation and lightness.

Parameters:

  • hue (float) –

    Degrees from 0 to 360.

  • saturation (float) –

    A percentage from 0 to 100.

  • lightness (float) –

    A percentage from 0 to 100.

Raises:

Examples:

assert Color.from_hsl(0, 100, 50).to_hex() == '#ff0000'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def from_hsl(cls, hue: float, saturation: float, lightness: float) -> Color:
    """
    A true color from hue, saturation and lightness.

    Args:
        hue: Degrees from 0 to 360.
        saturation: A percentage from 0 to 100.
        lightness: A percentage from 0 to 100.

    Raises:
        ValueError: A value is out of range.

    Examples:
        ```python
        assert Color.from_hsl(0, 100, 50).to_hex() == '#ff0000'
        ```
    """

from_hsv classmethod

from_hsv(hue: float, saturation: float, value: float) -> Color

A true color from hue, saturation and value.

Parameters:

  • hue (float) –

    Degrees from 0 to 360.

  • saturation (float) –

    A percentage from 0 to 100.

  • value (float) –

    A percentage from 0 to 100.

Raises:

Examples:

assert Color.from_hsv(120, 100, 100).to_hex() == '#00ff00'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def from_hsv(cls, hue: float, saturation: float, value: float) -> Color:
    """
    A true color from hue, saturation and value.

    Args:
        hue: Degrees from 0 to 360.
        saturation: A percentage from 0 to 100.
        value: A percentage from 0 to 100.

    Raises:
        ValueError: A value is out of range.

    Examples:
        ```python
        assert Color.from_hsv(120, 100, 100).to_hex() == '#00ff00'
        ```
    """

random classmethod

random(base: Color | ColorName | str | tuple[int, int, int] | None = None) -> Color

A random true color, or a random shade of base.

Without base every red, green and blue channel is picked at random. With base the color keeps its hue and saturation and takes a random lightness from 15 to 85 percent, so every color it returns is a lighter or darker shade of base. The colors come from Python's random module, so random.seed(...) repeats them.

Parameters:

Raises:

  • ValueError –

    base is Color.reset, or not a color.

Examples:

import random

random.seed(7)
first = Color.random()
random.seed(7)
assert Color.random() == first

hue, saturation, _ = Color.random('#3366cc').to_hsl()
assert round(hue) == 220 and round(saturation) == 60
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def random(
    cls, base: Color | ColorName | str | tuple[int, int, int] | None = None
) -> Color:
    """
    A random true color, or a random shade of ``base``.

    Without ``base`` every red, green and blue channel is picked at random. With ``base``
    the color keeps its hue and saturation and takes a random lightness from 15 to 85
    percent, so every color it returns is a lighter or darker shade of ``base``. The
    colors come from Python's [`random`](https://docs.python.org/3/library/random.html)
    module, so ``random.seed(...)`` repeats them.

    Args:
        base: The color to pick shades of, or ``None`` for any color.

    Raises:
        ValueError: ``base`` is ``Color.reset``, or not a color.

    Examples:
        ```python
        import random

        random.seed(7)
        first = Color.random()
        random.seed(7)
        assert Color.random() == first

        hue, saturation, _ = Color.random('#3366cc').to_hsl()
        assert round(hue) == 220 and round(saturation) == 60
        ```
    """

to_hsl

to_hsl() -> tuple[float, float, float]

The hue (degrees), saturation and lightness (percentages) of the color.

Named and indexed colors use their standard values.

Raises:

Examples:

hue, saturation, lightness = Color('#ff0000').to_hsl()
assert round(hue) == 0
assert round(saturation) == 100
assert round(lightness) == 50
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def to_hsl(self) -> tuple[float, float, float]:
    """
    The hue (degrees), saturation and lightness (percentages) of the color.

    Named and indexed colors use their standard values.

    Raises:
        ValueError: The color is ``reset``.

    Examples:
        ```python
        hue, saturation, lightness = Color('#ff0000').to_hsl()
        assert round(hue) == 0
        assert round(saturation) == 100
        assert round(lightness) == 50
        ```
    """

to_hsv

to_hsv() -> tuple[float, float, float]

The hue (degrees), saturation and value (percentages) of the color.

Named and indexed colors use their standard values.

Raises:

Examples:

hue, saturation, value = Color('#00ff00').to_hsv()
assert round(hue) == 120
assert round(saturation) == 100
assert round(value) == 100
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def to_hsv(self) -> tuple[float, float, float]:
    """
    The hue (degrees), saturation and value (percentages) of the color.

    Named and indexed colors use their standard values.

    Raises:
        ValueError: The color is ``reset``.

    Examples:
        ```python
        hue, saturation, value = Color('#00ff00').to_hsv()
        assert round(hue) == 120
        assert round(saturation) == 100
        assert round(value) == 100
        ```
    """

lerp

lerp(other: Color | ColorName | str | tuple[int, int, int], ratio: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl') -> Color

The color between this one and other.

Parameters:

  • other (Color | ColorName | str | tuple[int, int, int]) –

    The color to blend toward.

  • ratio (float) –

    0.0 is this color and 1.0 is other.

  • color_space (Literal['rgb', 'hsl', 'hsv'], default: 'hsl' ) –

    The space the blend runs through. 'hsl' and 'hsv' keep colors vivid in the middle; 'rgb' is the straight average.

Raises:

  • ValueError –

    A color is reset, or color_space is not a color space.

Examples:

middle = Color('#000000').lerp('#ffffff', 0.5, color_space='rgb')
red, green, blue = middle.to_rgb()
assert red == green == blue
assert 126 <= red <= 129
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def lerp(
    self,
    other: Color | ColorName | str | tuple[int, int, int],
    ratio: float,
    *,
    color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl',
) -> Color:
    """
    The color between this one and ``other``.

    Args:
        other: The color to blend toward.
        ratio: ``0.0`` is this color and ``1.0`` is ``other``.
        color_space: The space the blend runs through. ``'hsl'`` and ``'hsv'`` keep colors
            vivid in the middle; ``'rgb'`` is the straight average.

    Raises:
        ValueError: A color is ``reset``, or ``color_space`` is not a color space.

    Examples:
        ```python
        middle = Color('#000000').lerp('#ffffff', 0.5, color_space='rgb')
        red, green, blue = middle.to_rgb()
        assert red == green == blue
        assert 126 <= red <= 129
        ```
    """

lighten

lighten(amount: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl') -> Color

A lighter color for a positive amount, a darker one for a negative amount.

Parameters:

  • amount (float) –

    A fraction from -1.0 (black) to 1.0 (white).

  • color_space (Literal['rgb', 'hsl', 'hsv'], default: 'hsl' ) –

    The space the change runs through.

Raises:

  • ValueError –

    The color is reset, or color_space is not a color space.

Examples:

base = Color('#336699')
assert base.lighten(0.2).to_hsl()[2] > base.to_hsl()[2]
assert base.lighten(-0.2).to_hsl()[2] < base.to_hsl()[2]
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def lighten(
    self, amount: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl'
) -> Color:
    """
    A lighter color for a positive ``amount``, a darker one for a negative ``amount``.

    Args:
        amount: A fraction from -1.0 (black) to 1.0 (white).
        color_space: The space the change runs through.

    Raises:
        ValueError: The color is ``reset``, or ``color_space`` is not a color space.

    Examples:
        ```python
        base = Color('#336699')
        assert base.lighten(0.2).to_hsl()[2] > base.to_hsl()[2]
        assert base.lighten(-0.2).to_hsl()[2] < base.to_hsl()[2]
        ```
    """

darken

darken(amount: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl') -> Color

A darker color.

Parameters:

  • amount (float) –

    A fraction from 0.0 (unchanged) to 1.0 (black). Use lighten to make a color lighter.

  • color_space (Literal['rgb', 'hsl', 'hsv'], default: 'hsl' ) –

    The space the change runs through.

Returns:

  • Color –

    The darkened color; the original is unchanged.

Raises:

  • ValueError –

    amount is outside 0.0 to 1.0, the color is reset, or color_space is not a color space.

Examples:

assert Color('#808080').darken(1.0).to_rgb() == (0, 0, 0)
base = Color('#336699')
assert base.darken(0.2).to_hsl()[2] < base.to_hsl()[2]
assert base.darken(0.0) == base
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def darken(
    self, amount: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl'
) -> Color:
    """
    A darker color.

    Args:
        amount: A fraction from 0.0 (unchanged) to 1.0 (black). Use
            [`lighten`][xnano_core._xnano_core.Color.lighten] to make a color lighter.
        color_space: The space the change runs through.

    Returns:
        The darkened color; the original is unchanged.

    Raises:
        ValueError: ``amount`` is outside 0.0 to 1.0, the color is ``reset``, or
            ``color_space`` is not a color space.

    Examples:
        ```python
        assert Color('#808080').darken(1.0).to_rgb() == (0, 0, 0)
        base = Color('#336699')
        assert base.darken(0.2).to_hsl()[2] < base.to_hsl()[2]
        assert base.darken(0.0) == base
        ```
    """

inverse

inverse() -> Color

The color that has each red, green and blue channel inverted.

Named and indexed colors are inverted through their standard values, so the result is always a true color.

Returns:

  • Color –

    The inverted true color; the original is unchanged.

Raises:

  • ValueError –

    The color is reset, which has no fixed value.

Examples:

assert Color('#ff8000').inverse().to_hex() == '#007fff'
assert Color('#ff8000').inverse().inverse() == Color('#ff8000')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def inverse(self) -> Color:
    """
    The color that has each red, green and blue channel inverted.

    Named and indexed colors are inverted through their standard values, so the result
    is always a true color.

    Returns:
        The inverted true color; the original is unchanged.

    Raises:
        ValueError: The color is ``reset``, which has no fixed value.

    Examples:
        ```python
        assert Color('#ff8000').inverse().to_hex() == '#007fff'
        assert Color('#ff8000').inverse().inverse() == Color('#ff8000')
        ```
    """

saturate

saturate(factor: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl') -> Color

The color with its saturation scaled.

Parameters:

  • factor (float) –

    0.0 removes the saturation, 1.0 keeps it and more than 1.0 boosts it.

  • color_space (Literal['rgb', 'hsl', 'hsv'], default: 'hsl' ) –

    The space the change runs through.

Raises:

  • ValueError –

    The color is reset, or color_space is not a color space.

Examples:

gray = Color('#336699').saturate(0.0)
assert round(gray.to_hsl()[1]) == 0
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def saturate(
    self, factor: float, *, color_space: Literal['rgb', 'hsl', 'hsv'] = 'hsl'
) -> Color:
    """
    The color with its saturation scaled.

    Args:
        factor: ``0.0`` removes the saturation, ``1.0`` keeps it and more than ``1.0``
            boosts it.
        color_space: The space the change runs through.

    Raises:
        ValueError: The color is ``reset``, or ``color_space`` is not a color space.

    Examples:
        ```python
        gray = Color('#336699').saturate(0.0)
        assert round(gray.to_hsl()[1]) == 0
        ```
    """

Style

Style(foreground: ColorValue | None = None, background: ColorValue | None = None, *, bold: bool = False, dim: bool = False, italic: bool = False, underline: bool = False, blink: bool = False, reverse: bool = False, hidden: bool = False, crossed_out: bool = False, rapid_blink: bool = False, underline_color: ColorValue | None = None)

A foreground color, a background color and text modifiers.

Maps to ratatui::style::Style.

Parameters:

  • foreground (ColorValue | None, default: None ) –

    The text color.

  • background (ColorValue | None, default: None ) –

    The cell background color.

  • bold (bool, default: False ) –

    Whether text is bold.

  • dim (bool, default: False ) –

    Whether text is dimmed.

  • italic (bool, default: False ) –

    Whether text is italic.

  • underline (bool, default: False ) –

    Whether text is underlined.

  • blink (bool, default: False ) –

    Whether text blinks.

  • reverse (bool, default: False ) –

    Whether foreground and background swap.

  • hidden (bool, default: False ) –

    Whether text is hidden.

  • crossed_out (bool, default: False ) –

    Whether text is struck through.

Attributes:

Examples:

style = Style('red', 'black', bold=True)
assert style.foreground == Color('red')
assert 'bold' in style.modifiers

Create a Style.

Parameters:

  • foreground (ColorValue | None, default: None ) –

    ColorValue | None. Defaults to None.

  • background (ColorValue | None, default: None ) –

    ColorValue | None. Defaults to None.

  • bold (bool, default: False ) –

    bool. Defaults to False.

  • dim (bool, default: False ) –

    bool. Defaults to False.

  • italic (bool, default: False ) –

    bool. Defaults to False.

  • underline (bool, default: False ) –

    bool. Defaults to False.

  • blink (bool, default: False ) –

    bool. Defaults to False.

  • reverse (bool, default: False ) –

    bool. Defaults to False.

  • hidden (bool, default: False ) –

    bool. Defaults to False.

  • crossed_out (bool, default: False ) –

    bool. Defaults to False.

  • rapid_blink (bool, default: False ) –

    bool. Defaults to False.

  • underline_color (ColorValue | None, default: None ) –

    ColorValue | None. Defaults to None.

Methods:

  • patch –

    A style where every value other sets replaces this style's value.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(
    self,
    foreground: ColorValue | None = None,
    background: ColorValue | None = None,
    *,
    bold: bool = False,
    dim: bool = False,
    italic: bool = False,
    underline: bool = False,
    blink: bool = False,
    reverse: bool = False,
    hidden: bool = False,
    crossed_out: bool = False,
    rapid_blink: bool = False,
    underline_color: ColorValue | None = None,
) -> None:
    """
    Create a Style.

    Args:
        foreground: ``ColorValue | None``. Defaults to ``None``.
        background: ``ColorValue | None``. Defaults to ``None``.
        bold: ``bool``. Defaults to ``False``.
        dim: ``bool``. Defaults to ``False``.
        italic: ``bool``. Defaults to ``False``.
        underline: ``bool``. Defaults to ``False``.
        blink: ``bool``. Defaults to ``False``.
        reverse: ``bool``. Defaults to ``False``.
        hidden: ``bool``. Defaults to ``False``.
        crossed_out: ``bool``. Defaults to ``False``.
        rapid_blink: ``bool``. Defaults to ``False``.
        underline_color: ``ColorValue | None``. Defaults to ``None``.
    """

foreground property

foreground: Color | None

The text color, or None when unset.

background property

background: Color | None

The background color, or None when unset.

modifiers property

modifiers: frozenset[Modifier]

The enabled text modifiers.

patch

patch(other: Style) -> Style

A style where every value other sets replaces this style's value.

Examples:

base = Style('red', bold=True)
patched = base.patch(Style(italic=True))
assert patched.foreground == Color('red')
assert patched.modifiers == {'bold', 'italic'}
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def patch(self, other: Style) -> Style:
    """
    A style where every value ``other`` sets replaces this style's value.

    Examples:
        ```python
        base = Style('red', bold=True)
        patched = base.patch(Style(italic=True))
        assert patched.foreground == Color('red')
        assert patched.modifiers == {'bold', 'italic'}
        ```
    """

StyledText

StyledText(spans: Sequence[TextSpan])

Text made of differently styled spans.

Parameters:

Attributes:

Examples:

text = StyledText([TextSpan('error: ', style=Style('red')), TextSpan('disk full')])
assert str(text) == 'error: disk full'
assert len(text.spans) == 2

Create a StyledText.

Parameters:

Methods:

  • __str__ –

    The text without styles.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, spans: Sequence[TextSpan]) -> None:
    """
    Create a StyledText.

    Args:
        spans: ``Sequence[TextSpan]``.
    """

spans property

spans: tuple[TextSpan, ...]

The runs of text, in order.

__str__

__str__() -> str

The text without styles.

Examples:

assert str(StyledText([TextSpan('a'), TextSpan('b')])) == 'ab'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __str__(self) -> str:
    """
    The text without styles.

    Examples:
        ```python
        assert str(StyledText([TextSpan('a'), TextSpan('b')])) == 'ab'
        ```
    """

TextLine

One wrapped line of a TextLayout.

Attributes:

  • text (str) –

    The text of the line.

  • start_offset (int) –

    Where the line starts in the original text, counted in Python string offsets.

  • end_offset (int) –

    Where the line ends in the original text, counted in Python string offsets.

  • width (int) –

    The number of cells the line occupies.

Examples:

layout = layout_text('hello world', width=6)
line = layout.lines[0]
assert line.text == 'hello '
assert (line.start_offset, line.end_offset, line.width) == (0, 6, 6)

text property

text: str

The text of the line.

start_offset property

start_offset: int

Where the line starts in the original text, counted in Python string offsets.

end_offset property

end_offset: int

Where the line ends in the original text, counted in Python string offsets.

width property

width: int

The number of cells the line occupies.

TextSpan

TextSpan(text: str, *, style: Style | None = None)

A run of text with one style.

Maps to ratatui::text::Span.

Parameters:

  • text (str) –

    The text.

  • style (Style | None, default: None ) –

    The style of the whole run.

Attributes:

Examples:

span = TextSpan('warning', style=Style('yellow', bold=True))
assert span.text == 'warning'
assert 'bold' in span.style.modifiers

Create a TextSpan.

Parameters:

  • text (str) –

    str.

  • style (Style | None, default: None ) –

    Style | None. Defaults to None.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, text: str, *, style: Style | None = None) -> None:
    """
    Create a TextSpan.

    Args:
        text: ``str``.
        style: ``Style | None``. Defaults to ``None``.
    """

text property

text: str

The text of the run.

style property

style: Style

The style of the run.