colors.py 744 B

123456789101112131415161718192021222324252627282930
  1. """Shared ANSI color utilities for Hermes CLI modules."""
  2. import os
  3. import sys
  4. def should_use_color() -> bool:
  5. """Return True when colored output is appropriate."""
  6. if os.environ.get("NO_COLOR") is not None or os.environ.get("TERM") == "dumb":
  7. return False
  8. return bool(sys.stdout.isatty())
  9. class Colors:
  10. RESET = "\033[0m"
  11. BOLD = "\033[1m"
  12. DIM = "\033[2m"
  13. RED = "\033[31m"
  14. GREEN = "\033[32m"
  15. YELLOW = "\033[33m"
  16. BLUE = "\033[34m"
  17. MAGENTA = "\033[35m"
  18. CYAN = "\033[36m"
  19. def color(text: str, *codes) -> str:
  20. """Apply color codes to text (only when color output is appropriate)."""
  21. if not should_use_color():
  22. return text
  23. return "".join(codes) + text + Colors.RESET