Skip to content

jmwallet.cli.ansi

jmwallet.cli.ansi

Shared ANSI color helpers for CLI output.

Centralized so every CLI view (wallet info views, renderers, selectors) gates colors the same way. Colors are only emitted on interactive terminals; piped or redirected output stays plain (see :func:color_enabled).

Attributes

ANSI_BOLD_CYAN = '\x1b[1;36m' module-attribute

ANSI_BOLD_RED = '\x1b[1;31m' module-attribute

ANSI_BOLD_WHITE = '\x1b[1;37m' module-attribute

ANSI_BOLD_YELLOW = '\x1b[1;33m' module-attribute

ANSI_CYAN = '\x1b[0;36m' module-attribute

ANSI_RESET = '\x1b[0m' module-attribute

Functions:

color_enabled() -> bool

Whether ANSI colors should be emitted to stdout.

Colors are only safe on an interactive terminal. When stdout is piped or redirected (jm-wallet info --extended > file / | less / CI logs), raw escape codes corrupt the output and break downstream parsing, so they are suppressed. The NO_COLOR convention (https://no-color.org) is also honored. This mirrors the TTY guards already used elsewhere in the CLI (e.g. the freeze manager and the interactive UTXO selector).

Source code in jmwallet/src/jmwallet/cli/ansi.py
24
25
26
27
28
29
30
31
32
33
34
35
36
def color_enabled() -> bool:
    """Whether ANSI colors should be emitted to stdout.

    Colors are only safe on an interactive terminal. When stdout is piped or
    redirected (``jm-wallet info --extended > file`` / ``| less`` / CI logs),
    raw escape codes corrupt the output and break downstream parsing, so they
    are suppressed. The ``NO_COLOR`` convention (https://no-color.org) is also
    honored. This mirrors the TTY guards already used elsewhere in the CLI
    (e.g. the freeze manager and the interactive UTXO selector).
    """
    if os.environ.get("NO_COLOR") is not None:
        return False
    return sys.stdout.isatty()

colorize(text: str, code: str) -> str

Wrap text in an ANSI code when colors are enabled, else return it unchanged.

Source code in jmwallet/src/jmwallet/cli/ansi.py
39
40
41
42
43
def colorize(text: str, code: str) -> str:
    """Wrap ``text`` in an ANSI ``code`` when colors are enabled, else return it unchanged."""
    if not color_enabled():
        return text
    return f"{code}{text}{ANSI_RESET}"