Patch module - rich-color-ext
Author: Max Ludden
Description: Extend Rich with CSS color names and 3-digit hex parsing

Skip to content

# Patch module

## Examples

Patch Rich's `Color.parse` at runtime to add CSS name and 3-digit hex support:

    from rich_color_ext.patch import install, uninstall, is_installed

    install()
    assert is_installed()

    # ... use Rich with CSS colours here ...

    uninstall()
    assert not is_installed()

## API reference

Monkey-patching support for rich.color.Color.parse.

## `install()`

Install the monkey patch. After this call, rich.color.Color.parse will support 3‐digit hex and CSS colour names. Safe to call multiple times.

Source code in `src/rich_color_ext/patch.py`

    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    def install() -> None:
        """
        Install the monkey patch. After this call, rich.color.Color.parse will
        support 3‐digit hex and CSS colour names. Safe to call multiple times.
        """
        global INSTALLED  # pylint: disable=global-statement
        if INSTALLED:
            return
        Color.parse = _patched_parse  # type: ignore[assignment]
        INSTALLED = True

## `is_installed()`

Return True if the monkey patch is currently installed.

Source code in `src/rich_color_ext/patch.py`

    68
    69
    70
    71
    72
    def is_installed() -> bool:
        """
        Return True if the monkey patch is currently installed.
        """
        return INSTALLED

## `uninstall()`

Uninstall the monkey patch, restoring the original rich.color.Color.parse. Safe to call multiple times.

Source code in `src/rich_color_ext/patch.py`

    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    def uninstall() -> None:
        """
        Uninstall the monkey patch, restoring the original rich.color.Color.parse.
        Safe to call multiple times.
        """
        global INSTALLED  # pylint: disable=global-statement
        if not INSTALLED:
            return
        Color.parse = _ORIGINAL_PARSE  # type: ignore[assignment]
        INSTALLED = False