Skip to content

Commit 4a88906

Browse files
committed
Make color output an opt-in on Windows
There are simply to much different ways for this to go wrong...
1 parent fd2104f commit 4a88906

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

cli_ui/__init__.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
# and over again:
4141
_ENABLE_XTERM_TITLE = None
4242

43-
# should we call colorama.init()?
44-
_INITIALIZED = False
43+
colorama.init()
4544

4645

4746
# Tokens can be strings, or Color, UnicodeSequence or Symbol instances,
@@ -155,39 +154,21 @@ def __repr__(self) -> str:
155154
return f"Symbol({self.as_string})"
156155

157156

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-
169157
def config_color(fileobj: FileObj) -> bool:
170158
if CONFIG["color"] == "never":
171159
return False
172160
if CONFIG["color"] == "always":
173161
return True
174162
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
181166
else:
182167
return fileobj.isatty()
183168

184169

185170
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):
191172
return
192173
mystr = "\x1b]0;%s\x07" % mystr
193174
fileobj.write(mystr)
@@ -262,17 +243,13 @@ def message(
262243
""" Helper method for error, warning, info, debug
263244
264245
"""
265-
if using_colorama():
266-
global _INITIALIZED
267-
if not _INITIALIZED:
268-
colorama.init()
269-
_INITIALIZED = True
246+
should_use_colors = config_color(fileobj)
270247
with_color, without_color = process_tokens(tokens, end=end, sep=sep)
271248
if CONFIG["record"]:
272249
_MESSAGES.append(without_color)
273250
if update_title and with_color:
274251
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
276253
write_and_flush(fileobj, to_write)
277254

278255

@@ -670,7 +647,7 @@ def main_demo() -> None:
670647

671648
def new_message(*args: Any, **kwargs: Any) -> None:
672649
old_message(*args, **kwargs)
673-
time.sleep(1)
650+
time.sleep(0.5)
674651

675652
message = new_message
676653

0 commit comments

Comments
 (0)