Colorize text, while stripping nested ANSI color sequences. Available text colors: red, green, yellow, blue, magenta, cyan, white. Available text highlights: on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white. Available attributes: bold, dark, und
(
text: str,
color: Optional[str] = None,
highlight: Optional[str] = None,
attrs: Optional[Iterable[str]] = None,
)
| 74 | |
| 75 | |
| 76 | def colored( |
| 77 | text: str, |
| 78 | color: Optional[str] = None, |
| 79 | highlight: Optional[str] = None, |
| 80 | attrs: Optional[Iterable[str]] = None, |
| 81 | ) -> str: |
| 82 | """Colorize text, while stripping nested ANSI color sequences. |
| 83 | Available text colors: |
| 84 | red, green, yellow, blue, magenta, cyan, white. |
| 85 | Available text highlights: |
| 86 | on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white. |
| 87 | Available attributes: |
| 88 | bold, dark, underline, blink, reverse, concealed. |
| 89 | Example: |
| 90 | colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink']) |
| 91 | colored('Hello, World!', 'green') |
| 92 | """ |
| 93 | |
| 94 | def terminal_supports_color() -> bool: |
| 95 | if os.getenv("NO_COLOR") is not None: |
| 96 | return False |
| 97 | return _is_stdout_a_tty() |
| 98 | |
| 99 | if not terminal_supports_color(): |
| 100 | return text |
| 101 | return format_colored(text, color, highlight, attrs) |
| 102 | |
| 103 | |
| 104 | def format_colored( |