_ansi.py 617 B

1234567891011121314151617181920212223242526272829
  1. from __future__ import annotations
  2. _ANSI_COLORS = {
  3. "black": 30,
  4. "red": 31,
  5. "green": 32,
  6. "yellow": 33,
  7. "blue": 34,
  8. "magenta": 35,
  9. "cyan": 36,
  10. "white": 37,
  11. "bright_black": 90,
  12. "bright_red": 91,
  13. "bright_green": 92,
  14. "bright_yellow": 93,
  15. "bright_blue": 94,
  16. "bright_magenta": 95,
  17. "bright_cyan": 96,
  18. "bright_white": 97,
  19. }
  20. def style(text: str, fg: str | None = None, bold: bool = False) -> str:
  21. prefix = ""
  22. if fg is not None:
  23. prefix += f"\033[{_ANSI_COLORS[fg]}m"
  24. if bold:
  25. prefix += "\033[1m"
  26. return f"{prefix}{text}\033[0m"