Skip to content

Commit a32a242

Browse files
dnicolodirgommers
authored andcommitted
ENH: drop dependency on colorama on Windows
Simply enable ANSI escape sequences processing on the Windows console. This works only on Windows 10 and later, and colorama does the same thing when it can.
1 parent d2a4789 commit a32a242

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

mesonpy/__init__.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def strip(string: str) -> str:
156156

157157

158158
@functools.lru_cache()
159-
def _use_ansi_colors() -> bool:
160-
"""Determine whether logging should use ANSI color escapes."""
159+
def _use_ansi_escapes() -> bool:
160+
"""Determine whether logging should use ANSI escapes."""
161161

162162
# We print log messages and error messages that may contain file
163163
# names containing characters that cannot be represented in the
@@ -167,19 +167,17 @@ def _use_ansi_colors() -> bool:
167167

168168
if 'NO_COLOR' in os.environ:
169169
return False
170+
170171
if 'FORCE_COLOR' in os.environ or sys.stdout.isatty() and os.environ.get('TERM') != 'dumb':
171-
try:
172-
import colorama
173-
except ModuleNotFoundError:
174-
pass
175-
else:
176-
colorama.init()
172+
if sys.platform == 'win32' and not os.environ.get('ANSICON'):
173+
return mesonpy._util.setup_windows_console()
177174
return True
175+
178176
return False
179177

180178

181179
def _log(string: str , **kwargs: Any) -> None:
182-
if not _use_ansi_colors():
180+
if not _use_ansi_escapes():
183181
string = style.strip(string)
184182
print(string, **kwargs)
185183

mesonpy/_util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,23 @@ def update(self, description: str) -> None:
7171
def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None:
7272
if sys.stdout.isatty():
7373
print()
74+
75+
76+
def setup_windows_console() -> bool:
77+
from ctypes import byref, windll # type: ignore
78+
from ctypes.wintypes import DWORD
79+
80+
STD_OUTPUT_HANDLE = -11
81+
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
82+
83+
kernel = windll.kernel32
84+
stdout = kernel.GetStdHandle(STD_OUTPUT_HANDLE)
85+
mode = DWORD()
86+
87+
if not kernel.GetConsoleMode(stdout, byref(mode)):
88+
return False
89+
90+
if not kernel.SetConsoleMode(stdout, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING):
91+
return False
92+
93+
return True

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ classifiers = [
3232
]
3333

3434
dependencies = [
35-
'colorama; os_name == "nt"',
3635
'meson >= 0.63.3; python_version < "3.12"',
3736
'meson >= 1.2.3; python_version >= "3.12"',
3837
'pyproject-metadata >= 0.7.1',

tests/test_output.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
import mesonpy
88

99

10-
@pytest.mark.parametrize(
11-
('tty', 'env', 'colors'),
12-
[
13-
(False, {}, False),
14-
(True, {}, True),
15-
(False, {'NO_COLOR': ''}, False),
16-
(True, {'NO_COLOR': ''}, False),
17-
(False, {'FORCE_COLOR': ''}, True),
18-
(True, {'FORCE_COLOR': ''}, True),
19-
(True, {'FORCE_COLOR': '', 'NO_COLOR': ''}, False),
20-
(True, {'TERM': ''}, True),
21-
(True, {'TERM': 'dumb'}, False),
22-
],
23-
)
10+
@pytest.mark.parametrize(('tty', 'env', 'colors'), [
11+
(False, {}, False),
12+
(True, {}, True),
13+
(False, {'NO_COLOR': ''}, False),
14+
(True, {'NO_COLOR': ''}, False),
15+
(False, {'FORCE_COLOR': ''}, True),
16+
(True, {'FORCE_COLOR': ''}, True),
17+
(True, {'FORCE_COLOR': '', 'NO_COLOR': ''}, False),
18+
(True, {'TERM': ''}, True),
19+
(True, {'TERM': 'dumb'}, False),
20+
])
2421
def test_use_ansi_colors(mocker, monkeypatch, tty, env, colors):
2522
mocker.patch('sys.stdout.isatty', return_value=tty)
23+
mocker.patch('mesonpy._util.setup_windows_console', return_value=True)
2624
monkeypatch.delenv('NO_COLOR', raising=False)
2725
monkeypatch.delenv('FORCE_COLOR', raising=False)
2826
for key, value in env.items():

0 commit comments

Comments
 (0)