|
41 | 41 | # Color Infrastructure for CLI Output |
42 | 42 | # ============================================================================= |
43 | 43 |
|
| 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 | + |
44 | 100 | class Color(Enum): |
45 | 101 | """ANSI color codes with brightness variants for tense distinction. |
46 | 102 | |
|
0 commit comments