Skip to content

Commit aa76bfc

Browse files
feat(cli): add true color terminal detection
1 parent 0c7a744 commit aa76bfc

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

hatch/cli/cli_utils.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,62 @@
4141
# Color Infrastructure for CLI Output
4242
# =============================================================================
4343

44+
import os as _os
45+
46+
47+
def _supports_truecolor() -> bool:
48+
"""Detect if terminal supports 24-bit true color.
49+
50+
Checks environment variables and terminal identifiers to determine
51+
if the terminal supports true color (24-bit RGB) output.
52+
53+
Reference: R12 §3.1 (12-enhancing_colors_v0.md)
54+
55+
Detection Logic:
56+
1. COLORTERM='truecolor' or '24bit' → True
57+
2. TERM contains 'truecolor' or '24bit' → True
58+
3. TERM_PROGRAM in known true color terminals → True
59+
4. WT_SESSION set (Windows Terminal) → True
60+
5. Otherwise → False (fallback to 16-color)
61+
62+
Returns:
63+
bool: True if terminal supports true color, False otherwise.
64+
65+
Example:
66+
>>> if _supports_truecolor():
67+
... # Use 24-bit RGB color codes
68+
... color = "\\033[38;2;128;201;144m"
69+
... else:
70+
... # Use 16-color ANSI codes
71+
... color = "\\033[92m"
72+
"""
73+
# Check COLORTERM for 'truecolor' or '24bit'
74+
colorterm = _os.environ.get('COLORTERM', '')
75+
if colorterm in ('truecolor', '24bit'):
76+
return True
77+
78+
# Check TERM for truecolor indicators
79+
term = _os.environ.get('TERM', '')
80+
if 'truecolor' in term or '24bit' in term:
81+
return True
82+
83+
# Check TERM_PROGRAM for known true color terminals
84+
term_program = _os.environ.get('TERM_PROGRAM', '')
85+
if term_program in ('iTerm.app', 'Apple_Terminal', 'vscode', 'Hyper'):
86+
return True
87+
88+
# Check WT_SESSION for Windows Terminal
89+
if _os.environ.get('WT_SESSION'):
90+
return True
91+
92+
return False
93+
94+
95+
# Module-level constant for true color support detection
96+
# Evaluated once at module load time
97+
TRUECOLOR = _supports_truecolor()
98+
99+
44100
class Color(Enum):
45101
"""ANSI color codes with brightness variants for tense distinction.
46102

0 commit comments

Comments
 (0)