topmark.cli.console.color¶
topmark / cli / console / color
Color-resolution helpers for TopMark CLI console output.
This module is intentionally independent from Click and concrete console instances. It resolves whether ANSI color should be enabled based on CLI options, environment variables, terminal state, and selected output format.
ColorMode ¶
Bases: str, Enum
User intent for colorized terminal output.
Attributes:
| Name | Type | Description |
|---|---|---|
AUTO |
Enable color only when appropriate (typically when stdout is a TTY). |
|
ALWAYS |
Force-enable color regardless of TTY status. |
|
NEVER |
Disable color entirely. |
Typical usage
- Parse
--color=auto|always|neverasColorMode. - Pass the parsed value to
resolve_color_mode()along with the current output format (e.g.,"json"or"ndjson") to obtain a finalboolindicating whether to emit ANSI styles.
Example
resolve_color_mode(color_mode_override=ColorMode.AUTO, output_format=None) True # on an interactive terminal resolve_color_mode(color_mode_override=ColorMode.AUTO, output_format="json") False # machine-readable formats are always colorless
resolve_color_mode ¶
Determine whether color output should be enabled.
Decision precedence
- Machine-readable formats: If
output_formatis"json"or"ndjson", return False. - CLI override: If
color_mode_overrideisALWAYS→ True; ifNEVER→ False. - Environment:
FORCE_COLOR(set and not equal to"0") → TrueNO_COLOR(set to any value) → False
- Auto: If none of the above decide, return
stdout.isatty().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_mode_override
|
ColorMode | None
|
Parsed |
required |
output_format
|
str | None
|
Structured output mode; |
required |
stdout_isatty
|
bool | None
|
Optional override for TTY detection. When |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if ANSI color should be enabled; False otherwise. |
Examples:
>>> resolve_color_mode(color_mode_override=ColorMode.NEVER, output_format=None)
False
>>> resolve_color_mode(color_mode_override=None, output_format="ndjson")
False
>>> resolve_color_mode(color_mode_override=None, output_format=None, stdout_isatty=True)
True