/ hermes_cli / colors.py
colors.py
1 """Shared ANSI color utilities for Hermes CLI modules.""" 2 3 import os 4 import sys 5 6 7 def should_use_color() -> bool: 8 """Return True when colored output is appropriate. 9 10 Respects the NO_COLOR environment variable (https://no-color.org/) 11 and TERM=dumb, in addition to the existing TTY check. 12 """ 13 if os.environ.get("NO_COLOR") is not None: 14 return False 15 if os.environ.get("TERM") == "dumb": 16 return False 17 if not sys.stdout.isatty(): 18 return False 19 return True 20 21 22 class Colors: 23 RESET = "\033[0m" 24 BOLD = "\033[1m" 25 DIM = "\033[2m" 26 RED = "\033[31m" 27 GREEN = "\033[32m" 28 YELLOW = "\033[33m" 29 BLUE = "\033[34m" 30 MAGENTA = "\033[35m" 31 CYAN = "\033[36m" 32 33 34 def color(text: str, *codes) -> str: 35 """Apply color codes to text (only when color output is appropriate).""" 36 if not should_use_color(): 37 return text 38 return "".join(codes) + text + Colors.RESET