D2079: color: honor NO_COLOR

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Wed Feb 7 17:17:44 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The http://no-color.org/ initiative is trying to get programs that
  emit color by default to honor a NO_COLOR environment variable to
  disable color.
  
  I think that's a good idea. So this commit implements support for
  NO_COLOR.
  
  I'm not sure if the precedence of settings is proper here. Right now,
  NO_COLOR overrides config settings set by hgrc or --config. But it
  doesn't override --color. I can see an argument for honoring
  --config as well. Same for hgrc (since color is enabled by default
  these days). But the existing logic/precedence is unclear to me. So
  I went with an easy implementation.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2079

AFFECTED FILES
  mercurial/color.py
  tests/test-status-color.t

CHANGE DETAILS

diff --git a/tests/test-status-color.t b/tests/test-status-color.t
--- a/tests/test-status-color.t
+++ b/tests/test-status-color.t
@@ -46,6 +46,42 @@
   [status.unknown|? ][status.unknown|b/in_b] (glob)
   [status.unknown|? ][status.unknown|in_root]
 
+NO_COLOR disables color
+
+  $ NO_COLOR=1 hg status
+  ? a/1/in_a_1
+  ? a/in_a
+  ? b/1/in_b_1
+  ? b/2/in_b_2
+  ? b/in_b
+  ? in_root
+
+  $ NO_COLOR=0 hg status
+  ? a/1/in_a_1
+  ? a/in_a
+  ? b/1/in_b_1
+  ? b/2/in_b_2
+  ? b/in_b
+  ? in_root
+
+  $ NO_COLOR= hg status
+  ? a/1/in_a_1
+  ? a/in_a
+  ? b/1/in_b_1
+  ? b/2/in_b_2
+  ? b/in_b
+  ? in_root
+
+NO_COLOR is overridden by --color
+
+  $ NO_COLOR=1 hg --color=always status
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4ma/1/in_a_1\x1b[0m (esc)
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4ma/in_a\x1b[0m (esc)
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4mb/1/in_b_1\x1b[0m (esc)
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4mb/2/in_b_2\x1b[0m (esc)
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4mb/in_b\x1b[0m (esc)
+  \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4min_root\x1b[0m (esc)
+
 hg status with template
   $ hg status -T "{label('red', path)}\n" --color=debug
   [red|a/1/in_a_1]
diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -198,6 +198,13 @@
     if config == 'debug':
         return 'debug'
 
+    # The http://no-color.org/ initiative wants to standardize on an environment
+    # variable to disable color. The value of this variable doesn't matter.
+    if 'NO_COLOR' in encoding.environ:
+        # Allow --color CLI argument to override NO_COLOR
+        if ui.configsource('ui', 'color') != '--color':
+            return None
+
     auto = (config == 'auto')
     always = False
     if not auto and util.parsebool(config):



To: indygreg, #hg-reviewers
Cc: mercurial-devel, spectral


More information about the Mercurial-devel mailing list