mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #819 from jeffreyrack/fix-windows-reporting
Don't print bogus characters on windows terminals that don't support colors
This commit is contained in:
commit
cd116977cc
2 changed files with 15 additions and 4 deletions
|
|
@ -3,12 +3,13 @@ from __future__ import unicode_literals
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
terminal_supports_color = True
|
||||||
if os.name == 'nt': # pragma: no cover (windows)
|
if os.name == 'nt': # pragma: no cover (windows)
|
||||||
from pre_commit.color_windows import enable_virtual_terminal_processing
|
from pre_commit.color_windows import enable_virtual_terminal_processing
|
||||||
try:
|
try:
|
||||||
enable_virtual_terminal_processing()
|
enable_virtual_terminal_processing()
|
||||||
except WindowsError:
|
except WindowsError:
|
||||||
pass
|
terminal_supports_color = False
|
||||||
|
|
||||||
RED = '\033[41m'
|
RED = '\033[41m'
|
||||||
GREEN = '\033[42m'
|
GREEN = '\033[42m'
|
||||||
|
|
@ -47,4 +48,7 @@ def use_color(setting):
|
||||||
if setting not in COLOR_CHOICES:
|
if setting not in COLOR_CHOICES:
|
||||||
raise InvalidColorSetting(setting)
|
raise InvalidColorSetting(setting)
|
||||||
|
|
||||||
return setting == 'always' or (setting == 'auto' and sys.stdout.isatty())
|
return (
|
||||||
|
setting == 'always' or
|
||||||
|
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -35,9 +35,16 @@ def test_use_color_no_tty():
|
||||||
assert use_color('auto') is False
|
assert use_color('auto') is False
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_tty():
|
def test_use_color_tty_with_color_support():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||||
assert use_color('auto') is True
|
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||||
|
assert use_color('auto') is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_color_tty_without_color_support():
|
||||||
|
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||||
|
with mock.patch('pre_commit.color.terminal_supports_color', False):
|
||||||
|
assert use_color('auto') is False
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_raises_if_given_shenanigans():
|
def test_use_color_raises_if_given_shenanigans():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue