Module

theme

Theme system for styled CLI output.

Provides named styles that templates can reference via thestylefilter or thethemeglobal proxy::

{{ message | style("primary") }}
{{ theme.success }}Done!{{ theme.reset }}

CLI applications opt in by passing a theme dict toget_env()::

from milo.templates import get_env
from milo.theme import DEFAULT_THEME, ThemeStyle

env = get_env(theme={
    **DEFAULT_THEME,
    "brand": ThemeStyle(fg="magenta", bold=True),
})

Classes

ThemeStyle 6
A named style mapping to ANSI SGR attributes. The ``fg`` and ``bg`` fields accept three formats: …

A named style mapping to ANSI SGR attributes.

Thefg and bgfields accept three formats:

  • Named color (str):"red", "bright_cyan"— basic 16-color ANSI.
  • 256-color index (int):33— extended 256-color palette.
  • Truecolor hex (str):"#ff6600"— 24-bit RGB color.

Attributes

Name Type Description
fg str | int | None
bg str | int | None
bold bool
dim bool
italic bool

Methods

sgr_prefix 0 str
Return the SGR escape prefix for this style (no reset).
def sgr_prefix(self) -> str
Returns
str
ThemeProxy 2
Attribute-access proxy for use as a kida template global. In templates:: {{ theme.primary }}H…

Attribute-access proxy for use as a kida template global.

In templates::

{{ theme.primary }}Hello{{ theme.reset }}
{{ theme.error }}Oops{{ theme.reset }}

When color is disabled, all attributes return empty strings.

Methods

Internal Methods 2
__init__ 2
def __init__(self, theme: dict[str, ThemeStyle], *, color: bool = True) -> None
Parameters
Name Type Description
theme
color Default:True
__getattr__ 1 str
def __getattr__(self, name: str) -> str
Parameters
Name Type Description
name
Returns
str

Functions

_parse_hex 1 tuple[int, int, int]
Parse ``#rrggbb`` into an (r, g, b) tuple.
def _parse_hex(color: str) -> tuple[int, int, int]
Parameters
Name Type Description
color str
Returns
tuple[int, int, int]
_color_codes 2 str
Return the SGR parameter string for a foreground or background color.
def _color_codes(color: str | int, *, ground: str) -> str
Parameters
Name Type Description
color str | int

Named color, 256-color index (int), or#rrggbbhex string.

ground str

"fg" or "bg".

Returns
str
make_style_filter 2 Any
Create a ``style`` filter closure bound to a theme. Usage in templates:: …
def make_style_filter(theme: dict[str, ThemeStyle], *, color: bool = True) -> Any

Create astylefilter closure bound to a theme.

Usage in templates::

{{ "Error!" | style("error") }}
{{ count | style("primary") }}
Parameters
Name Type Description
theme dict[str, ThemeStyle]
color bool Default:True
Returns
Any