|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -import os |
6 | 5 | import re |
7 | | -import shutil |
8 | | -import sys |
9 | | - |
10 | | -try: |
11 | | - # check if colorama is installed to support color on Windows |
12 | | - import colorama |
13 | | -except ImportError: |
14 | | - colorama = None |
15 | | - |
16 | | - |
17 | | -_ansi_re: re.Pattern = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm') |
18 | | -codes: dict[str, str] = {} |
19 | 6 |
|
20 | 7 |
|
21 | 8 | def terminal_safe(s: str) -> str: |
22 | 9 | """Safely encode a string for printing to the terminal.""" |
23 | 10 | return s.encode('ascii', 'backslashreplace').decode('ascii') |
24 | 11 |
|
25 | 12 |
|
26 | | -def get_terminal_width() -> int: |
27 | | - """Return the width of the terminal in columns.""" |
28 | | - return shutil.get_terminal_size().columns - 1 |
29 | | - |
30 | | - |
31 | | -_tw: int = get_terminal_width() |
32 | | - |
33 | | - |
34 | | -def term_width_line(text: str) -> str: |
35 | | - if not codes: |
36 | | - # if no coloring, don't output fancy backspaces |
37 | | - return text + '\n' |
38 | | - else: |
39 | | - # codes are not displayed, this must be taken into account |
40 | | - return text.ljust(_tw + len(text) - len(_ansi_re.sub('', text))) + '\r' |
41 | | - |
42 | | - |
43 | | -def color_terminal() -> bool: |
44 | | - if 'NO_COLOR' in os.environ: |
45 | | - return False |
46 | | - if sys.platform == 'win32' and colorama is not None: |
47 | | - colorama.init() |
48 | | - return True |
49 | | - if 'FORCE_COLOR' in os.environ: |
50 | | - return True |
51 | | - if not hasattr(sys.stdout, 'isatty'): |
52 | | - return False |
53 | | - if not sys.stdout.isatty(): |
54 | | - return False |
55 | | - if 'COLORTERM' in os.environ: |
56 | | - return True |
57 | | - term = os.environ.get('TERM', 'dumb').lower() |
58 | | - if term in ('xterm', 'linux') or 'color' in term: |
59 | | - return True |
60 | | - return False |
61 | | - |
62 | | - |
63 | | -def nocolor() -> None: |
64 | | - if sys.platform == 'win32' and colorama is not None: |
65 | | - colorama.deinit() |
66 | | - codes.clear() |
67 | | - |
68 | | - |
69 | | -def coloron() -> None: |
70 | | - codes.update(_orig_codes) |
71 | | - |
72 | | - |
73 | | -def colorize(name: str, text: str, input_mode: bool = False) -> str: |
74 | | - def escseq(name: str) -> str: |
75 | | - # Wrap escape sequence with ``\1`` and ``\2`` to let readline know |
76 | | - # it is non-printable characters |
77 | | - # ref: https://tiswww.case.edu/php/chet/readline/readline.html |
78 | | - # |
79 | | - # Note: This hack does not work well in Windows (see #5059) |
80 | | - escape = codes.get(name, '') |
81 | | - if input_mode and escape and sys.platform != 'win32': |
82 | | - return '\1' + escape + '\2' |
83 | | - else: |
84 | | - return escape |
85 | | - |
86 | | - return escseq(name) + text + escseq('reset') |
87 | | - |
88 | | - |
89 | 13 | def strip_colors(s: str) -> str: |
90 | 14 | return re.compile('\x1b.*?m').sub('', s) |
91 | | - |
92 | | - |
93 | | -def create_color_func(name: str) -> None: |
94 | | - def inner(text: str) -> str: |
95 | | - return colorize(name, text) |
96 | | - globals()[name] = inner |
97 | | - |
98 | | - |
99 | | -_attrs = { |
100 | | - 'reset': '39;49;00m', |
101 | | - 'bold': '01m', |
102 | | - 'faint': '02m', |
103 | | - 'standout': '03m', |
104 | | - 'underline': '04m', |
105 | | - 'blink': '05m', |
106 | | -} |
107 | | - |
108 | | -for _name, _value in _attrs.items(): |
109 | | - codes[_name] = '\x1b[' + _value |
110 | | - |
111 | | -_colors = [ |
112 | | - ('black', 'darkgray'), |
113 | | - ('darkred', 'red'), |
114 | | - ('darkgreen', 'green'), |
115 | | - ('brown', 'yellow'), |
116 | | - ('darkblue', 'blue'), |
117 | | - ('purple', 'fuchsia'), |
118 | | - ('turquoise', 'teal'), |
119 | | - ('lightgray', 'white'), |
120 | | -] |
121 | | - |
122 | | -for i, (dark, light) in enumerate(_colors, 30): |
123 | | - codes[dark] = '\x1b[%im' % i |
124 | | - codes[light] = '\x1b[%im' % (i + 60) |
125 | | - |
126 | | -_orig_codes = codes.copy() |
127 | | - |
128 | | -for _name in codes: |
129 | | - create_color_func(_name) |
0 commit comments