| 49 | |
| 50 | @final |
| 51 | class TerminalWriter: |
| 52 | _esctable = dict( |
| 53 | black=30, |
| 54 | red=31, |
| 55 | green=32, |
| 56 | yellow=33, |
| 57 | blue=34, |
| 58 | purple=35, |
| 59 | cyan=36, |
| 60 | white=37, |
| 61 | Black=40, |
| 62 | Red=41, |
| 63 | Green=42, |
| 64 | Yellow=43, |
| 65 | Blue=44, |
| 66 | Purple=45, |
| 67 | Cyan=46, |
| 68 | White=47, |
| 69 | bold=1, |
| 70 | light=2, |
| 71 | blink=5, |
| 72 | invert=7, |
| 73 | ) |
| 74 | |
| 75 | def __init__(self, file: TextIO | None = None) -> None: |
| 76 | if file is None: |
| 77 | file = sys.stdout |
| 78 | if hasattr(file, "isatty") and file.isatty() and sys.platform == "win32": |
| 79 | try: |
| 80 | import colorama |
| 81 | except ImportError: |
| 82 | pass |
| 83 | else: |
| 84 | file = colorama.AnsiToWin32(file).stream |
| 85 | assert file is not None |
| 86 | self._file = file |
| 87 | self.hasmarkup = should_do_markup(file) |
| 88 | self._current_line = "" |
| 89 | self._terminal_width: int | None = None |
| 90 | self.code_highlight = True |
| 91 | |
| 92 | @property |
| 93 | def fullwidth(self) -> int: |
| 94 | if self._terminal_width is not None: |
| 95 | return self._terminal_width |
| 96 | return get_terminal_width() |
| 97 | |
| 98 | @fullwidth.setter |
| 99 | def fullwidth(self, value: int) -> None: |
| 100 | self._terminal_width = value |
| 101 | |
| 102 | @property |
| 103 | def width_of_current_line(self) -> int: |
| 104 | """Return an estimate of the width so far in the current line.""" |
| 105 | return wcswidth(self._current_line) |
| 106 | |
| 107 | def markup(self, text: str, **markup: bool) -> str: |
| 108 | for name in markup: |
no outgoing calls
searching dependent graphs…