Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd11697

Browse files
authored
Merge pull request #819 from jeffreyrack/fix-windows-reporting
Don't print bogus characters on windows terminals that don't support colors
2 parents f042e30 + 710eef3 commit cd11697

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

pre_commit/color.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import os
44
import sys
55

6+
terminal_supports_color = True
67
if os.name == 'nt': # pragma: no cover (windows)
78
from pre_commit.color_windows import enable_virtual_terminal_processing
89
try:
910
enable_virtual_terminal_processing()
1011
except WindowsError:
11-
pass
12+
terminal_supports_color = False
1213

1314
RED = '\033[41m'
1415
GREEN = '\033[42m'
@@ -47,4 +48,7 @@ def use_color(setting):
4748
if setting not in COLOR_CHOICES:
4849
raise InvalidColorSetting(setting)
4950

50-
return setting == 'always' or (setting == 'auto' and sys.stdout.isatty())
51+
return (
52+
setting == 'always' or
53+
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
54+
)

tests/color_test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ def test_use_color_no_tty():
3535
assert use_color('auto') is False
3636

3737

38-
def test_use_color_tty():
38+
def test_use_color_tty_with_color_support():
3939
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
40-
assert use_color('auto') is True
40+
with mock.patch('pre_commit.color.terminal_supports_color', True):
41+
assert use_color('auto') is True
42+
43+
44+
def test_use_color_tty_without_color_support():
45+
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
46+
with mock.patch('pre_commit.color.terminal_supports_color', False):
47+
assert use_color('auto') is False
4148

4249

4350
def test_use_color_raises_if_given_shenanigans():

0 commit comments

Comments
 (0)