Apply color and bold font to terminal output. This currently only works on Linux and Mac.
| 604 | |
| 605 | |
| 606 | class FancyFormatter: |
| 607 | """Apply color and bold font to terminal output. |
| 608 | |
| 609 | This currently only works on Linux and Mac. |
| 610 | """ |
| 611 | |
| 612 | def __init__( |
| 613 | self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool, hide_success: bool = False |
| 614 | ) -> None: |
| 615 | self.hide_error_codes = hide_error_codes |
| 616 | self.hide_success = hide_success |
| 617 | |
| 618 | # Check if we are in a human-facing terminal on a supported platform. |
| 619 | if sys.platform not in ("linux", "darwin", "win32", "emscripten"): |
| 620 | self.dummy_term = True |
| 621 | return |
| 622 | if not should_force_color() and (not f_out.isatty() or not f_err.isatty()): |
| 623 | self.dummy_term = True |
| 624 | return |
| 625 | if sys.platform == "win32": |
| 626 | self.dummy_term = not self.initialize_win_colors() |
| 627 | elif sys.platform == "emscripten": |
| 628 | self.dummy_term = not self.initialize_vt100_colors() |
| 629 | else: |
| 630 | self.dummy_term = not self.initialize_unix_colors() |
| 631 | if not self.dummy_term: |
| 632 | self.colors = { |
| 633 | "red": self.RED, |
| 634 | "green": self.GREEN, |
| 635 | "blue": self.BLUE, |
| 636 | "yellow": self.YELLOW, |
| 637 | "none": "", |
| 638 | } |
| 639 | |
| 640 | def initialize_vt100_colors(self) -> bool: |
| 641 | """Return True if initialization was successful and we can use colors, False otherwise""" |
| 642 | # Windows and Emscripten can both use ANSI/VT100 escape sequences for color |
| 643 | assert sys.platform in ("win32", "emscripten") |
| 644 | self.BOLD = "\033[1m" |
| 645 | self.UNDER = "\033[4m" |
| 646 | self.BLUE = "\033[94m" |
| 647 | self.GREEN = "\033[92m" |
| 648 | self.RED = "\033[91m" |
| 649 | self.YELLOW = "\033[93m" |
| 650 | self.NORMAL = "\033[0m" |
| 651 | self.DIM = "\033[2m" |
| 652 | return True |
| 653 | |
| 654 | def initialize_win_colors(self) -> bool: |
| 655 | """Return True if initialization was successful and we can use colors, False otherwise""" |
| 656 | # Windows ANSI escape sequences are only supported on Threshold 2 and above. |
| 657 | # we check with an assert at runtime and an if check for mypy, as asserts do not |
| 658 | # yet narrow platform |
| 659 | if sys.platform == "win32": # needed to find win specific sys apis |
| 660 | winver = sys.getwindowsversion() |
| 661 | if ( |
| 662 | winver.major < MINIMUM_WINDOWS_MAJOR_VT100 |
| 663 | or winver.build < MINIMUM_WINDOWS_BUILD_VT100 |
no outgoing calls
no test coverage detected
searching dependent graphs…