mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #1382 from pre-commit/better_windows_color
support colors on windows during git better
This commit is contained in:
commit
30a36a8a00
2 changed files with 9 additions and 9 deletions
|
|
@ -11,7 +11,7 @@ if sys.platform == 'win32': # pragma: no cover (windows)
|
||||||
from ctypes.wintypes import DWORD
|
from ctypes.wintypes import DWORD
|
||||||
from ctypes.wintypes import HANDLE
|
from ctypes.wintypes import HANDLE
|
||||||
|
|
||||||
STD_OUTPUT_HANDLE = -11
|
STD_ERROR_HANDLE = -12
|
||||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
|
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
|
||||||
|
|
||||||
def bool_errcheck(result, func, args):
|
def bool_errcheck(result, func, args):
|
||||||
|
|
@ -40,9 +40,9 @@ if sys.platform == 'win32': # pragma: no cover (windows)
|
||||||
#
|
#
|
||||||
# More info on the escape sequences supported:
|
# More info on the escape sequences supported:
|
||||||
# https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
|
# https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
|
||||||
stdout = GetStdHandle(STD_OUTPUT_HANDLE)
|
stderr = GetStdHandle(STD_ERROR_HANDLE)
|
||||||
flags = GetConsoleMode(stdout)
|
flags = GetConsoleMode(stderr)
|
||||||
SetConsoleMode(stdout, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
SetConsoleMode(stderr, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_enable()
|
_enable()
|
||||||
|
|
@ -90,7 +90,7 @@ def use_color(setting: str) -> bool:
|
||||||
return (
|
return (
|
||||||
setting == 'always' or (
|
setting == 'always' or (
|
||||||
setting == 'auto' and
|
setting == 'auto' and
|
||||||
sys.stdout.isatty() and
|
sys.stderr.isatty() and
|
||||||
terminal_supports_color and
|
terminal_supports_color and
|
||||||
os.getenv('TERM') != 'dumb'
|
os.getenv('TERM') != 'dumb'
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -29,26 +29,26 @@ def test_use_color_always():
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_no_tty():
|
def test_use_color_no_tty():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=False):
|
with mock.patch.object(sys.stderr, 'isatty', return_value=False):
|
||||||
assert use_color('auto') is False
|
assert use_color('auto') is False
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_tty_with_color_support():
|
def test_use_color_tty_with_color_support():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||||
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||||
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
||||||
assert use_color('auto') is True
|
assert use_color('auto') is True
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_tty_without_color_support():
|
def test_use_color_tty_without_color_support():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||||
with mock.patch('pre_commit.color.terminal_supports_color', False):
|
with mock.patch('pre_commit.color.terminal_supports_color', False):
|
||||||
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
||||||
assert use_color('auto') is False
|
assert use_color('auto') is False
|
||||||
|
|
||||||
|
|
||||||
def test_use_color_dumb_term():
|
def test_use_color_dumb_term():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||||
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||||
with envcontext.envcontext((('TERM', 'dumb'),)):
|
with envcontext.envcontext((('TERM', 'dumb'),)):
|
||||||
assert use_color('auto') is False
|
assert use_color('auto') is False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue