From 1bd6fce7dce2032fa7083c720650e65b80bf5256 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Wed, 29 Aug 2018 18:54:55 -0700 Subject: [PATCH 1/4] Don't print bogus characters on windows terminals that don't support colors. --- pre_commit/color.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pre_commit/color.py b/pre_commit/color.py index 44917ca0..e75ffd64 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -3,11 +3,13 @@ from __future__ import unicode_literals import os import sys +terminal_supports_colors = True if os.name == 'nt': # pragma: no cover (windows) from pre_commit.color_windows import enable_virtual_terminal_processing try: enable_virtual_terminal_processing() except WindowsError: + terminal_supports_colors = False pass RED = '\033[41m' @@ -29,7 +31,7 @@ def format_color(text, color, use_color_setting): color - The color start string use_color_setting - Whether or not to color """ - if not use_color_setting: + if not use_color_setting or not terminal_supports_colors: return text else: return '{}{}{}'.format(color, text, NORMAL) From a970d3b69b693fffd858fce7791c0d0168e584ff Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Thu, 30 Aug 2018 18:45:29 -0700 Subject: [PATCH 2/4] Removing useless pass statement. --- pre_commit/color.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pre_commit/color.py b/pre_commit/color.py index e75ffd64..e3b03420 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -10,7 +10,6 @@ if os.name == 'nt': # pragma: no cover (windows) enable_virtual_terminal_processing() except WindowsError: terminal_supports_colors = False - pass RED = '\033[41m' GREEN = '\033[42m' From 3d777bb386fef50a077c1814bcb1a5f26ed1a964 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Thu, 30 Aug 2018 19:15:46 -0700 Subject: [PATCH 3/4] Move logic to handle terminal not supporting colors to use_color --- pre_commit/color.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pre_commit/color.py b/pre_commit/color.py index e3b03420..c785e2c9 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -3,13 +3,13 @@ from __future__ import unicode_literals import os import sys -terminal_supports_colors = True +terminal_supports_color = True if os.name == 'nt': # pragma: no cover (windows) from pre_commit.color_windows import enable_virtual_terminal_processing try: enable_virtual_terminal_processing() except WindowsError: - terminal_supports_colors = False + terminal_supports_color = False RED = '\033[41m' GREEN = '\033[42m' @@ -30,7 +30,7 @@ def format_color(text, color, use_color_setting): color - The color start string use_color_setting - Whether or not to color """ - if not use_color_setting or not terminal_supports_colors: + if not use_color_setting: return text else: return '{}{}{}'.format(color, text, NORMAL) @@ -48,4 +48,7 @@ def use_color(setting): if setting not in COLOR_CHOICES: 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) + ) From 710eef317ab1bce800636f1e2578a8b3133ca959 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Thu, 30 Aug 2018 19:39:37 -0700 Subject: [PATCH 4/4] Fixing tests to account for the new terminal_supports_color variable --- tests/color_test.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/color_test.py b/tests/color_test.py index 0b8a4d69..6e11765c 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -35,9 +35,16 @@ def test_use_color_no_tty(): 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): - 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():