|
28 | 28 | except ImportError: |
29 | 29 | CURSES_ENABLED = False |
30 | 30 |
|
| 31 | +IS_WIN: Final = sys.platform == "win32" |
| 32 | + |
31 | 33 | T = TypeVar("T") |
32 | 34 |
|
33 | 35 | TYPESHED_DIR: Final = str(importlib_resources.files("mypy") / "typeshed") |
@@ -571,8 +573,7 @@ def hash_digest(data: bytes) -> str: |
571 | 573 |
|
572 | 574 | def parse_gray_color(cup: bytes) -> str: |
573 | 575 | """Reproduce a gray color in ANSI escape sequence""" |
574 | | - if sys.platform == "win32": |
575 | | - assert False, "curses is not available on Windows" |
| 576 | + assert sys.platform != "win32", "curses is not available on Windows" |
576 | 577 | set_color = "".join([cup[:-1].decode(), "m"]) |
577 | 578 | gray = curses.tparm(set_color.encode("utf-8"), 1, 9).decode() |
578 | 579 | return gray |
@@ -605,7 +606,7 @@ def __init__( |
605 | 606 | if not should_force_color() and (not f_out.isatty() or not f_err.isatty()): |
606 | 607 | self.dummy_term = True |
607 | 608 | return |
608 | | - if sys.platform == "win32": |
| 609 | + if IS_WIN: |
609 | 610 | self.dummy_term = not self.initialize_win_colors() |
610 | 611 | elif sys.platform == "emscripten": |
611 | 612 | self.dummy_term = not self.initialize_vt100_colors() |
@@ -639,34 +640,32 @@ def initialize_win_colors(self) -> bool: |
639 | 640 | # Windows ANSI escape sequences are only supported on Threshold 2 and above. |
640 | 641 | # we check with an assert at runtime and an if check for mypy, as asserts do not |
641 | 642 | # yet narrow platform |
642 | | - assert sys.platform == "win32" |
643 | | - if sys.platform == "win32": |
644 | | - winver = sys.getwindowsversion() |
645 | | - if ( |
646 | | - winver.major < MINIMUM_WINDOWS_MAJOR_VT100 |
647 | | - or winver.build < MINIMUM_WINDOWS_BUILD_VT100 |
648 | | - ): |
649 | | - return False |
650 | | - import ctypes |
651 | | - |
652 | | - kernel32 = ctypes.windll.kernel32 |
653 | | - ENABLE_PROCESSED_OUTPUT = 0x1 |
654 | | - ENABLE_WRAP_AT_EOL_OUTPUT = 0x2 |
655 | | - ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4 |
656 | | - STD_OUTPUT_HANDLE = -11 |
657 | | - kernel32.SetConsoleMode( |
658 | | - kernel32.GetStdHandle(STD_OUTPUT_HANDLE), |
659 | | - ENABLE_PROCESSED_OUTPUT |
660 | | - | ENABLE_WRAP_AT_EOL_OUTPUT |
661 | | - | ENABLE_VIRTUAL_TERMINAL_PROCESSING, |
662 | | - ) |
663 | | - self.initialize_vt100_colors() |
664 | | - return True |
665 | | - return False |
| 643 | + assert IS_WIN, "Running not on Windows" |
| 644 | + winver = sys.getwindowsversion() |
| 645 | + if ( |
| 646 | + winver.major < MINIMUM_WINDOWS_MAJOR_VT100 |
| 647 | + or winver.build < MINIMUM_WINDOWS_BUILD_VT100 |
| 648 | + ): |
| 649 | + return False |
| 650 | + import ctypes |
| 651 | + |
| 652 | + kernel32 = ctypes.windll.kernel32 |
| 653 | + ENABLE_PROCESSED_OUTPUT = 0x1 |
| 654 | + ENABLE_WRAP_AT_EOL_OUTPUT = 0x2 |
| 655 | + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4 |
| 656 | + STD_OUTPUT_HANDLE = -11 |
| 657 | + kernel32.SetConsoleMode( |
| 658 | + kernel32.GetStdHandle(STD_OUTPUT_HANDLE), |
| 659 | + ENABLE_PROCESSED_OUTPUT |
| 660 | + | ENABLE_WRAP_AT_EOL_OUTPUT |
| 661 | + | ENABLE_VIRTUAL_TERMINAL_PROCESSING, |
| 662 | + ) |
| 663 | + self.initialize_vt100_colors() |
| 664 | + return True |
666 | 665 |
|
667 | 666 | def initialize_unix_colors(self) -> bool: |
668 | 667 | """Return True if initialization was successful and we can use colors, False otherwise""" |
669 | | - if sys.platform == "win32" or not CURSES_ENABLED: |
| 668 | + if IS_WIN or not CURSES_ENABLED: |
670 | 669 | return False |
671 | 670 | try: |
672 | 671 | # setupterm wants a fd to potentially write an "initialization sequence". |
|
0 commit comments