Shared formatting utilities for MADSci CLI and TUI.
Provides centralised status styling, timestamp/duration formatting, and text truncation helpers. Used by both CLI commands and TUI screens to ensure consistent visual presentation.
Functions¶
build_ownership_section(data: dict) ‑> list[tuple[str, str]]Build ownership detail section items from a data dict.
Extracts ownership-related fields and formats them as (label, value) tuples suitable for a DetailPanel section.
Args: data: Data dictionary that may contain an
"ownership_info"key with a nested dict of ownership fields.Returns: List of
(label, value)tuples for the non-empty ownership fields. Returns an empty list if no ownership info is present.format_duration(seconds: float | None) ‑> strFormat a duration in seconds as a human-readable string.
Output forms:
Xh XXm XXswhen hours > 0XXm XXsotherwise"-"forNoneor negative values
Args: seconds: Duration in seconds.
Returns: Formatted duration string.
format_status_colored(status: str, text: str | None = None) ‑> strReturn Rich markup for coloured status text.
If text is
None, the status string itself is used as the display text.Args: status: Status string used for colour lookup. text: Optional override text to colour.
Returns: Rich markup string.
format_status_icon(status: str) ‑> strReturn Rich markup for a status icon.
Example output:
"[green]\u25cf[/green]".Args: status: Status string.
Returns: Rich markup string.
format_timestamp(ts: Any, short: bool = False) ‑> strFormat a timestamp for display.
Handles
datetimeobjects, ISO-format strings (with optionalZsuffix), and arbitrary objects (viastr()fallback).Args: ts: Timestamp value. short: If
True, useHH:MM:SS.mmmformat.Returns: Formatted timestamp string or
"-"forNoneinputs.get_status_style(status: str) ‑> tuple[str, str]Get the
(icon, colour)tuple for a status string.Performs a case-insensitive lookup against :data:
STATUS_STYLES. Returns theunknownentry for unrecognised statuses.Args: status: Status string (e.g.
"healthy","running").Returns:
(icon_char, rich_colour_name)tuple.truncate(text: str, max_len: int = 50) ‑> strTruncate text with an ellipsis if it exceeds max_len.
Args: text: Input text. max_len: Maximum allowed length (including the ellipsis).
Returns: Truncated string.