Colorize text. 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, underline, blink, reverse, concealed. Ex
(text, color=None, on_color=None, attrs=None)
| 89 | |
| 90 | |
| 91 | def colored(text, color=None, on_color=None, attrs=None): |
| 92 | """Colorize text. |
| 93 | |
| 94 | Available text colors: |
| 95 | red, green, yellow, blue, magenta, cyan, white. |
| 96 | |
| 97 | Available text highlights: |
| 98 | on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white. |
| 99 | |
| 100 | Available attributes: |
| 101 | bold, dark, underline, blink, reverse, concealed. |
| 102 | |
| 103 | Example: |
| 104 | colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink']) |
| 105 | colored('Hello, World!', 'green') |
| 106 | """ |
| 107 | if os.getenv('ANSI_COLORS_DISABLED') is None: |
| 108 | fmt_str = '\033[%dm%s' |
| 109 | if color is not None: |
| 110 | text = fmt_str % (COLORS[color], text) |
| 111 | |
| 112 | if on_color is not None: |
| 113 | text = fmt_str % (HIGHLIGHTS[on_color], text) |
| 114 | |
| 115 | if attrs is not None: |
| 116 | for attr in attrs: |
| 117 | text = fmt_str % (ATTRIBUTES[attr], text) |
| 118 | |
| 119 | text += RESET |
| 120 | return text |
| 121 | |
| 122 | |
| 123 | def cprint(text, color=None, on_color=None, attrs=None, **kwargs): |