Coverage for / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / color.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-01-26 17:59 +0000
« prev ^ index » next coverage.py v7.13.2, created at 2026-01-26 17:59 +0000
1# SPDX-License-Identifier: Apache-2.0
2# Copyright © 2025 Bijan Mousavi
4"""Centralized color mode handling for CLI output."""
6from __future__ import annotations
8from bijux_cli.core.enums import ColorMode, OutputFormat
9from bijux_cli.core.precedence import GlobalCLIConfig
11_COLOR_MODE = ColorMode.AUTO
14def set_color_mode(mode: ColorMode) -> None:
15 """Set the global color mode for Click/Typer output."""
16 global _COLOR_MODE
17 _COLOR_MODE = mode
20def get_color_mode() -> ColorMode:
21 """Return the current global color mode."""
22 return _COLOR_MODE
25def resolve_click_color(*, quiet: bool, fmt: OutputFormat | None = None) -> bool | None:
26 """Resolve Click/Typer color usage for the current mode and output."""
27 if quiet:
28 return False
29 if fmt in (OutputFormat.JSON, OutputFormat.YAML):
30 return False
31 if _COLOR_MODE is ColorMode.NEVER:
32 return False
33 if _COLOR_MODE is ColorMode.ALWAYS:
34 return True
35 return None
38def resolve_color_mode(
39 config: GlobalCLIConfig,
40 tty: bool,
41 no_color: bool,
42) -> ColorMode:
43 """Resolve the effective color mode for CLI rendering."""
44 if no_color:
45 return ColorMode.NEVER
46 mode = config.flags.color or ColorMode.AUTO
47 if mode is ColorMode.AUTO and not tty:
48 return ColorMode.NEVER
49 return mode
52__all__ = [
53 "get_color_mode",
54 "resolve_click_color",
55 "resolve_color_mode",
56 "set_color_mode",
57]