|
40 | 40 | # and over again: |
41 | 41 | _ENABLE_XTERM_TITLE = None |
42 | 42 |
|
43 | | -# should we call colorama.init()? |
44 | | -_INITIALIZED = False |
| 43 | +colorama.init() |
45 | 44 |
|
46 | 45 |
|
47 | 46 | # Tokens can be strings, or Color, UnicodeSequence or Symbol instances, |
@@ -155,39 +154,21 @@ def __repr__(self) -> str: |
155 | 154 | return f"Symbol({self.as_string})" |
156 | 155 |
|
157 | 156 |
|
158 | | -def using_colorama() -> bool: |
159 | | - if os.name == "nt": |
160 | | - if "TERM" not in os.environ: |
161 | | - return True |
162 | | - if os.environ["TERM"] == "cygwin": |
163 | | - return True |
164 | | - return False |
165 | | - else: |
166 | | - return False |
167 | | - |
168 | | - |
169 | 157 | def config_color(fileobj: FileObj) -> bool: |
170 | 158 | if CONFIG["color"] == "never": |
171 | 159 | return False |
172 | 160 | if CONFIG["color"] == "always": |
173 | 161 | return True |
174 | 162 | if os.name == "nt": |
175 | | - # sys.isatty() is False on mintty, so |
176 | | - # let there be colors by default. (when running on windows, |
177 | | - # people can use --color=never) |
178 | | - # Note that on Windows, when run from cmd.exe, |
179 | | - # console.init() does the right thing if sys.stdout is redirected |
180 | | - return True |
| 163 | + # Color is always an opt-in on Windows, |
| 164 | + # because there are two many ways for this to go wrong |
| 165 | + return False |
181 | 166 | else: |
182 | 167 | return fileobj.isatty() |
183 | 168 |
|
184 | 169 |
|
185 | 170 | def write_title_string(mystr: str, fileobj: FileObj) -> None: |
186 | | - if using_colorama(): |
187 | | - # By-pass colorama bug: |
188 | | - # colorama/win32.py, line 154 |
189 | | - # return _SetConsoleTitleW(title) |
190 | | - # ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type |
| 171 | + if not config_color(fileobj): |
191 | 172 | return |
192 | 173 | mystr = "\x1b]0;%s\x07" % mystr |
193 | 174 | fileobj.write(mystr) |
@@ -262,17 +243,13 @@ def message( |
262 | 243 | """ Helper method for error, warning, info, debug |
263 | 244 |
|
264 | 245 | """ |
265 | | - if using_colorama(): |
266 | | - global _INITIALIZED |
267 | | - if not _INITIALIZED: |
268 | | - colorama.init() |
269 | | - _INITIALIZED = True |
| 246 | + should_use_colors = config_color(fileobj) |
270 | 247 | with_color, without_color = process_tokens(tokens, end=end, sep=sep) |
271 | 248 | if CONFIG["record"]: |
272 | 249 | _MESSAGES.append(without_color) |
273 | 250 | if update_title and with_color: |
274 | 251 | write_title_string(without_color, fileobj) |
275 | | - to_write = with_color if config_color(fileobj) else without_color |
| 252 | + to_write = with_color if should_use_colors else without_color |
276 | 253 | write_and_flush(fileobj, to_write) |
277 | 254 |
|
278 | 255 |
|
@@ -670,7 +647,7 @@ def main_demo() -> None: |
670 | 647 |
|
671 | 648 | def new_message(*args: Any, **kwargs: Any) -> None: |
672 | 649 | old_message(*args, **kwargs) |
673 | | - time.sleep(1) |
| 650 | + time.sleep(0.5) |
674 | 651 |
|
675 | 652 | message = new_message |
676 | 653 |
|
|
0 commit comments