Skip to content

Commit 13fd92f

Browse files
authored
Merge pull request #65 from dmerejkowsky/windows-fixes
Windows fixes
2 parents fd2104f + cf87e36 commit 13fd92f

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

cli_ui/__init__.py

Lines changed: 10 additions & 33 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-
169-
def config_color(fileobj: FileObj) -> bool:
157+
def colors_enabled(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 colors_enabled(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 = colors_enabled(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

@@ -434,7 +411,7 @@ def info_table(
434411
plain_row.append(plain_str)
435412
colored_data.append(colored_row)
436413
plain_data.append(plain_row)
437-
if config_color(fileobj):
414+
if colors_enabled(fileobj):
438415
data_for_tabulate = colored_data
439416
else:
440417
data_for_tabulate = plain_data
@@ -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

cli_ui/tests/test_cli_ui.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import io
33
import operator
4+
import os
45
import re
56
from typing import Iterator
67
from unittest import mock
@@ -47,12 +48,19 @@ def dumb_tty() -> DumbTTY:
4748

4849
@pytest.fixture
4950
def toggle_timestamp() -> Iterator[None]:
50-
cli_ui.CONFIG["timestamp"] = True
51+
cli_ui.setup(timestamp=True)
5152
yield
52-
cli_ui.CONFIG["timestamp"] = False
53+
cli_ui.setup(timestamp=False)
5354

5455

55-
def test_info_stdout_is_a_tty(smart_tty: io.StringIO) -> None:
56+
@pytest.fixture
57+
def always_color() -> Iterator[None]:
58+
cli_ui.setup(color="always")
59+
yield
60+
cli_ui.setup(color="auto")
61+
62+
63+
def test_info_with_colors(always_color: None, smart_tty: io.StringIO) -> None:
5664
# fmt: off
5765
cli_ui.info(
5866
cli_ui.red, "this is red", cli_ui.reset,
@@ -67,7 +75,7 @@ def test_info_stdout_is_a_tty(smart_tty: io.StringIO) -> None:
6775
assert actual == expected
6876

6977

70-
def test_update_title(smart_tty: SmartTTY) -> None:
78+
def test_update_title(always_color: None, smart_tty: SmartTTY) -> None:
7179
# fmt: off
7280
cli_ui.info(
7381
"Something", cli_ui.bold, "bold",
@@ -83,7 +91,7 @@ def test_update_title(smart_tty: SmartTTY) -> None:
8391
assert actual == expected
8492

8593

86-
def test_info_stdout_is_not_a_tty(dumb_tty: DumbTTY) -> None:
94+
def test_info_stdout_no_colors(dumb_tty: DumbTTY) -> None:
8795
# fmt: off
8896
cli_ui.info(
8997
cli_ui.red, "this is red", cli_ui.reset,
@@ -96,12 +104,18 @@ def test_info_stdout_is_not_a_tty(dumb_tty: DumbTTY) -> None:
96104
assert actual == expected
97105

98106

99-
def test_info_characters(smart_tty: SmartTTY) -> None:
107+
def test_info_characters(always_color: None, smart_tty: SmartTTY) -> None:
100108
cli_ui.info(
101109
"Doing stuff", cli_ui.ellipsis, "sucess", cli_ui.check, fileobj=smart_tty
102110
)
103111
actual = smart_tty.getvalue()
104-
expected = f"Doing stuff {RESET_ALL}{RESET_ALL}{RESET_ALL}sucess {RESET_ALL}{GREEN}{RESET_ALL}\n{RESET_ALL}"
112+
if os.name == "nt":
113+
success = "ok"
114+
ellipsis = "..."
115+
else:
116+
success = "✓"
117+
ellipsis = "…"
118+
expected = f"Doing stuff {RESET_ALL}{RESET_ALL}{ellipsis} {RESET_ALL}sucess {RESET_ALL}{GREEN}{success} {RESET_ALL}\n{RESET_ALL}"
105119
assert actual == expected
106120

107121

@@ -150,7 +164,7 @@ def test_table_with_dict_no_color(dumb_tty: DumbTTY) -> None:
150164
assert actual == expected
151165

152166

153-
def test_table_with_dict_and_color(smart_tty: SmartTTY) -> None:
167+
def test_table_with_dict_and_color(always_color: None, smart_tty: SmartTTY) -> None:
154168
data = {
155169
(cli_ui.bold, "Name",): [(cli_ui.green, "Alice"), (cli_ui.green, "Bob")],
156170
(cli_ui.bold, "Age",): [(cli_ui.blue, 24), (cli_ui.blue, 9)],
@@ -168,7 +182,7 @@ def test_table_with_dict_and_color(smart_tty: SmartTTY) -> None:
168182
assert actual == expected
169183

170184

171-
def test_table_with_lists_with_color(smart_tty: SmartTTY) -> None:
185+
def test_table_with_lists_with_color(always_color: None, smart_tty: SmartTTY) -> None:
172186
headers = ["name", "score"]
173187
data = [
174188
[(cli_ui.bold, "John"), (cli_ui.green, 10)],

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Changelog
22
----------
33

4+
v0.14.0
5+
+++++++
6+
7+
* **breaking**: Remove buggy workarounds on Windows.
8+
Colors will now be off by default unless ``cli_setup()`` is called
9+
with ``color="always"``. Configurations that are known to work are
10+
``cmd.exe`` and ``git-bash`` when using ``mintty``.
11+
12+
* **breaking**: ``colorama.init()`` is called uncoditionaly when ``cli_ui`` is
13+
imported
14+
415
v0.13.0
516
+++++++
617

0 commit comments

Comments
 (0)