Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 1 | """Color printing module.""" |
| 2 | # Copyright (c) 2020-2023, ARM Limited. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | from enum import Enum |
| 5 | from enum import unique |
| 6 | |
| 7 | color_printing = True |
| 8 | |
| 9 | |
| 10 | @unique |
| 11 | class LogColors(Enum): |
| 12 | """Shell escape sequence colors for logging.""" |
| 13 | |
| 14 | NONE = "\u001b[0m" |
| 15 | GREEN = "\u001b[32;1m" |
| 16 | RED = "\u001b[31;1m" |
| 17 | YELLOW = "\u001b[33;1m" |
| 18 | BOLD_WHITE = "\u001b[1m" |
| 19 | |
| 20 | |
| 21 | def set_print_in_color(enabled): |
| 22 | """Set color printing to enabled or disabled.""" |
| 23 | global color_printing |
| 24 | color_printing = enabled |
| 25 | |
| 26 | |
| 27 | def print_color(color, msg): |
| 28 | """Print color status messages if enabled.""" |
| 29 | global color_printing |
| 30 | if not color_printing: |
| 31 | print(msg) |
| 32 | else: |
| 33 | print("{}{}{}".format(color.value, msg, LogColors.NONE.value)) |