Skip to content

Commit 30a36a8

Browse files
authored
Merge pull request #1382 from pre-commit/better_windows_color
support colors on windows during git better
2 parents 34e9d11 + 9fc5a93 commit 30a36a8

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

pre_commit/color.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def _enable() -> None:
1111
from ctypes.wintypes import DWORD
1212
from ctypes.wintypes import HANDLE
1313

14-
STD_OUTPUT_HANDLE = -11
14+
STD_ERROR_HANDLE = -12
1515
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
1616

1717
def bool_errcheck(result, func, args):
@@ -40,9 +40,9 @@ def bool_errcheck(result, func, args):
4040
#
4141
# More info on the escape sequences supported:
4242
# https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
43-
stdout = GetStdHandle(STD_OUTPUT_HANDLE)
44-
flags = GetConsoleMode(stdout)
45-
SetConsoleMode(stdout, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
43+
stderr = GetStdHandle(STD_ERROR_HANDLE)
44+
flags = GetConsoleMode(stderr)
45+
SetConsoleMode(stderr, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
4646

4747
try:
4848
_enable()
@@ -90,7 +90,7 @@ def use_color(setting: str) -> bool:
9090
return (
9191
setting == 'always' or (
9292
setting == 'auto' and
93-
sys.stdout.isatty() and
93+
sys.stderr.isatty() and
9494
terminal_supports_color and
9595
os.getenv('TERM') != 'dumb'
9696
)

tests/color_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ def test_use_color_always():
2929

3030

3131
def test_use_color_no_tty():
32-
with mock.patch.object(sys.stdout, 'isatty', return_value=False):
32+
with mock.patch.object(sys.stderr, 'isatty', return_value=False):
3333
assert use_color('auto') is False
3434

3535

3636
def test_use_color_tty_with_color_support():
37-
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
37+
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
3838
with mock.patch('pre_commit.color.terminal_supports_color', True):
3939
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
4040
assert use_color('auto') is True
4141

4242

4343
def test_use_color_tty_without_color_support():
44-
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
44+
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
4545
with mock.patch('pre_commit.color.terminal_supports_color', False):
4646
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
4747
assert use_color('auto') is False
4848

4949

5050
def test_use_color_dumb_term():
51-
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
51+
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
5252
with mock.patch('pre_commit.color.terminal_supports_color', True):
5353
with envcontext.envcontext((('TERM', 'dumb'),)):
5454
assert use_color('auto') is False

0 commit comments

Comments
 (0)