Repository: pre-commit/pre-commit

Issue:
No colors when hooks are run by a git commit
Hi,
I use pre-commit at home on linux which works perfect. But at work I have a windows pc. Here I have problems with the colorfull output. 

When the hooks are run by `tox` calling `pre-commit run` there are colors as usual. When the hooks are run by a `git commit` the colors are missing.

Concrete I mean the green for 'Passed', red for 'Failed ' and yellow/brown for 'Skipped' in the overview.

There is no difference if I run it via git-bash, cmd or powershell. Also there is no difference if I use the pycharm buildin terminal or others.


Discussion:
colors on windows are a bit fiddly and unfortunately there isn't much we can do to change them when run from `git` -- we try and call `SetConsoleMode` [here](https://github.com/pre-commit/pre-commit/blob/34e9d117861ba758b3fd2d0b33eb70de9f05238d/pre_commit/color.py#L45) but if that fails you don't get colors
Ok thanks for the fast answer. 
Then I have to live with it :(
actually hmm, on windows `sys.stdout.isatty()` is false, but `sys.stderr.isatty()` is true -- which is different than linux, maybe if we use stderr instead of stdout?

Resolving patch:
diff --git a/pre_commit/color.py b/pre_commit/color.py
--- a/pre_commit/color.py
+++ b/pre_commit/color.py
@@ -11,7 +11,7 @@ def _enable() -> None:
         from ctypes.wintypes import DWORD
         from ctypes.wintypes import HANDLE
 
-        STD_OUTPUT_HANDLE = -11
+        STD_ERROR_HANDLE = -12
         ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
 
         def bool_errcheck(result, func, args):
@@ -40,9 +40,9 @@ def bool_errcheck(result, func, args):
         #
         # More info on the escape sequences supported:
         # https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
-        stdout = GetStdHandle(STD_OUTPUT_HANDLE)
-        flags = GetConsoleMode(stdout)
-        SetConsoleMode(stdout, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
+        stderr = GetStdHandle(STD_ERROR_HANDLE)
+        flags = GetConsoleMode(stderr)
+        SetConsoleMode(stderr, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
 
     try:
         _enable()
@@ -90,7 +90,7 @@ def use_color(setting: str) -> bool:
     return (
         setting == 'always' or (
             setting == 'auto' and
-            sys.stdout.isatty() and
+            sys.stderr.isatty() and
             terminal_supports_color and
             os.getenv('TERM') != 'dumb'
         )


Test patch:
diff --git a/tests/color_test.py b/tests/color_test.py
--- a/tests/color_test.py
+++ b/tests/color_test.py
@@ -29,26 +29,26 @@ def test_use_color_always():
 
 
 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
 
 
 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 envcontext.envcontext((('TERM', envcontext.UNSET),)):
                 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.object(sys.stderr, 'isatty', return_value=True):
         with mock.patch('pre_commit.color.terminal_supports_color', False):
             with envcontext.envcontext((('TERM', envcontext.UNSET),)):
                 assert use_color('auto') is False
 
 
 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 envcontext.envcontext((('TERM', 'dumb'),)):
                 assert use_color('auto') is False