Repository: conan-io/conan
Pull request #11421 (2022-06-08 09:36:53)

Fix colors in output windows cmd
Fixed the colors in any windows terminal (and the rest).

SSE4: cmd.exe
![image](https://user-images.githubusercontent.com/870236/172602896-6389e258-6476-4373-9cae-b62d48660d5a.png)

powershell
![image](https://user-images.githubusercontent.com/870236/172602982-506e3e5e-30dd-428a-b1ca-231e0f82c30e.png)

windows terminal
![image](https://user-images.githubusercontent.com/870236/172603728-cd43c889-b56a-4a29-a4c0-28a54799acb7.png)

cygwin
![image](https://user-images.githubusercontent.com/870236/172603109-f36132c1-729c-4f59-9820-27972a3246d3.png)

msys2
![image](https://user-images.githubusercontent.com/870236/172603777-d7fa35a4-a33c-4b80-a3d3-c0500f1b6104.png)

far manager
![image](https://user-images.githubusercontent.com/870236/172603883-5ef276d4-4985-49b4-b025-eee753bb379a.png)

SSE4: also works from inside the PyCharm terminal:
![image](https://user-images.githubusercontent.com/870236/172604534-b296d142-c99f-4601-80be-87b7e52f4116.png)


Patch:
diff --git a/conans/cli/output.py b/conans/cli/output.py
index 2dbd103fd..5b084e2e6 100644
--- a/conans/cli/output.py
+++ b/conans/cli/output.py
@@ -42,6 +42,8 @@ class ConanOutput:
     def __init__(self, scope=""):
         self.stream = sys.stderr
         self._scope = scope
+        # FIXME:  This is needed because in testing we are redirecting the sys.stderr to a buffer
+        #         stream to capture it, so colorama is not there to strip the color bytes
         self._color = color_enabled(self.stream)
 
     @property
diff --git a/conans/client/userio.py b/conans/client/userio.py
index 3c21fd3da..9cd18c66a 100644
--- a/conans/client/userio.py
+++ b/conans/client/userio.py
@@ -1,5 +1,6 @@
 import getpass
 import os
+import platform
 import sys
 
 from conans.errors import ConanException
@@ -11,29 +12,23 @@ def is_terminal(stream):
 
 def color_enabled(stream):
     """
-    NO_COLOR: No colors and no colorama
-    CLICOLOR: When 1, colors if is_terminal
-    CLICOLOR_FORCE: When 1, always colors (except if NO_COLOR)
+    NO_COLOR: No colors
+
+    https://no-color.org/
+
+    Command-line software which adds ANSI color to its output by default should check for the
+    presence of a NO_COLOR environment variable that, when present (**regardless of its value**),
+    prevents the addition of ANSI color.
     """
-    if os.getenv("NO_COLOR") == "1":
-        return False
-    elif os.getenv("CLICOLOR_FORCE") == "1" or (os.getenv("CLICOLOR", "1") != "0"
-                                                and is_terminal(stream)):
-        return True
-    else:
+    if os.getenv("NO_COLOR") is not None:
         return False
+    return is_terminal(stream)
 
 
 def init_colorama(stream):
     import colorama
-
-    if not color_enabled(stream):
-        if os.getenv("NO_COLOR") != "1":
-            colorama.init(strip=True)
-        return False
-    else:
-        colorama.init(convert=False, strip=False)
-        return True
+    if color_enabled(stream):
+        colorama.init()
 
 
 class UserInput(object):


Test patch:
diff --git a/conans/test/unittests/client/conan_output_test.py b/conans/test/unittests/client/conan_output_test.py
index 58921596b..1e5ee526d 100644
--- a/conans/test/unittests/client/conan_output_test.py
+++ b/conans/test/unittests/client/conan_output_test.py
@@ -1,7 +1,6 @@
 # coding=utf-8
 import sys
 import unittest
-from types import MethodType
 from unittest import mock
 
 from parameterized import parameterized
@@ -12,35 +11,8 @@ from conans.client.userio import init_colorama
 
 class ConanOutputTest(unittest.TestCase):
 
-    @parameterized.expand([(False, {}),
-                           (True, {"CLICOLOR": "0"}),
-                           (False, {"CLICOLOR": "1"}),
-                           (False, {"CLICOLOR_FORCE": "0"})])
-    def test_output_no_color(self, isatty, env):
-        with mock.patch("colorama.init") as init:
-            with mock.patch("sys.stderr.isatty", return_value=isatty), \
-                 mock.patch.dict("os.environ", env, clear=True):
-                init_colorama(sys.stderr)
-                out = ConanOutput()
-                assert out.color is False
-                init.assert_called()
-
-    @parameterized.expand([(True, {}),
-                           (False, {"CLICOLOR_FORCE": "1"}),
-                           (True, {"CLICOLOR": "1"})])
-    def test_output_color(self, isatty, env):
-        with mock.patch("colorama.init") as init:
-            with mock.patch("sys.stderr.isatty", return_value=isatty), \
-                 mock.patch.dict("os.environ", env, clear=True):
-                init_colorama(sys.stderr)
-                out = ConanOutput()
-                assert out.color is True
-                init.assert_called()
-
     @parameterized.expand([(True, {"NO_COLOR": "1"}),
-                           (True, {"NO_COLOR": "1", "CLICOLOR_FORCE": "1"}),
-                           (True, {"NO_COLOR": "1", "CLICOLOR_FORCE": "1", "CLICOLOR": "1"}),
-                           (False, {"NO_COLOR": "1", "CLICOLOR_FORCE": "1", "CLICOLOR": "1"})])
+                           (True, {"NO_COLOR": "0"})])
     def test_output_color_prevent_strip(self, isatty, env):
         with mock.patch("colorama.init") as init:
             with mock.patch("sys.stderr.isatty", return_value=isatty), \