Parse ANSI formatting; the formatting passed in should be stripped of the control characters and ending character
(cls, formatting: str)
| 40 | |
| 41 | @classmethod |
| 42 | def parse_formatting(cls, formatting: str) -> Tuple[int, int, int]: |
| 43 | """Parse ANSI formatting; the formatting passed in should be |
| 44 | stripped of the control characters and ending character""" |
| 45 | fg_color = -1 # -1 default means "use default", not "use white/black" |
| 46 | bg_color = -1 |
| 47 | other = 0 |
| 48 | int_values = [int(value) for value in formatting.split(";") if value] |
| 49 | for code in int_values: |
| 50 | if cls.FOREGROUND_RANGE.bottom <= code <= cls.FOREGROUND_RANGE.top: |
| 51 | fg_color = code - cls.FOREGROUND_RANGE.bottom |
| 52 | elif cls.BACKGROUND_RANGE.bottom <= code <= cls.BACKGROUND_RANGE.top: |
| 53 | bg_color = code - cls.BACKGROUND_RANGE.bottom |
| 54 | elif code == cls.BOLD_ATTRIBUTE: |
| 55 | other = other | curses.A_BOLD |
| 56 | elif code == cls.UNDERLINE_ATTRIBUTE: |
| 57 | other = other | curses.A_UNDERLINE |
| 58 | |
| 59 | return fg_color, bg_color, other |
| 60 | |
| 61 | @classmethod |
| 62 | def get_sequence_for_attributes( |