Skip to content
Permalink
Browse files

Add support for NO_COLOR env variable

Add support for the NO_COLOR environment variable
(https://no-color.org/) which when set (regardess of its value) prevents
the addition of ANSI color.

When set, NO_COLOR prevails over CLICOLOR and CLICOLOR_FORCE environment
variables. MODULES_COLOR overrides these tree variables.

Fixes #310
  • Loading branch information
xdelaruelle committed Feb 24, 2020
1 parent e2a381d commit 01e2f4add17a96fc66386d1815ea62b861a7654f
@@ -1194,20 +1194,23 @@ ENVIRONMENT

Colored output enablement is defined in the following order of preference:
:option:`--color` command line switch, then :envvar:`MODULES_COLOR`
environment variable, then :envvar:`CLICOLOR` and :envvar:`CLICOLOR_FORCE`
environment variables, then the default set in :file:`modulecmd.tcl` script
configuration. Which means :envvar:`MODULES_COLOR` overrides default
configuration and the :envvar:`CLICOLOR`/:envvar:`CLICOLOR_FORCE` variables.
environment variable, then :envvar:`NO_COLOR`, :envvar:`CLICOLOR` and
:envvar:`CLICOLOR_FORCE` environment variables, then the default set in
:file:`modulecmd.tcl` script configuration. Which means
:envvar:`MODULES_COLOR` overrides default configuration and the
:envvar:`NO_COLOR` and :envvar:`CLICOLOR`/:envvar:`CLICOLOR_FORCE` variables.
:option:`--color` command line switch overrides every other ways to enable or
disable this mode.

:envvar:`CLICOLOR` and :envvar:`CLICOLOR_FORCE` environment variables are
also honored to define color mode. The ``never`` mode is set if
:envvar:`NO_COLOR`, :envvar:`CLICOLOR` and :envvar:`CLICOLOR_FORCE`
environment variables are also honored to define color mode. The ``never``
mode is set if :envvar:`NO_COLOR` is defined (regardless of its value) or if
:envvar:`CLICOLOR` equals to ``0``. If :envvar:`CLICOLOR` is set to another
value, it corresponds to the ``auto`` mode. The ``always`` mode is set if
:envvar:`CLICOLOR_FORCE` is set to a value different than ``0``. Color mode
set with these two variables is superseded by mode set with
:envvar:`MODULES_COLOR` environment variable.
:envvar:`CLICOLOR_FORCE` is set to a value different than ``0``.
:envvar:`NO_COLOR` variable prevails over :envvar:`CLICOLOR` and
:envvar:`CLICOLOR_FORCE`. Color mode set with these three variables is
superseded by mode set with :envvar:`MODULES_COLOR` environment variable.

.. only:: html

@@ -297,7 +297,10 @@ proc initConfColors {envvar value validvallist intvallist} {
# Initialize color configuration value
proc initConfColor {envvar value validvallist intvallist} {
# overriden value coming from environment via standard variable
if {[info exists ::env(CLICOLOR)]} {
# https://no-color.org/ and https://bixense.com/clicolors/
if {[info exists ::env(NO_COLOR)]} {
set value never
} elseif {[info exists ::env(CLICOLOR)]} {
if {$::env(CLICOLOR) eq {0}} {
set value never
} else {
@@ -87,6 +87,7 @@ catch {unset env(MODULES_SITECONFIG)}
# clean any color configuration
catch {unset env(MODULES_COLORS)}
catch {unset env(MODULES_TERM_BACKGROUND)}
catch {unset env(NO_COLOR)}
catch {unset env(CLICOLOR)}
catch {unset env(CLICOLOR_FORCE)}
# ensure colored output is disabled by default
@@ -84,6 +84,7 @@ catch {unset env(MODULES_SITECONFIG)}
# clean any color configuration
catch {unset env(MODULES_COLORS)}
catch {unset env(MODULES_TERM_BACKGROUND)}
catch {unset env(NO_COLOR)}
catch {unset env(CLICOLOR)}
catch {unset env(CLICOLOR_FORCE)}
# ensure colored output is disabled by default
@@ -184,6 +184,16 @@ if {$install_color eq {y}} {
}

# environment variable or/and command-line switch override
setenv_var NO_COLOR 0
color_test_case {} 0 $sgr
setenv_var NO_COLOR 1
color_test_case {} 0 $sgr
setenv_var NO_COLOR {}
color_test_case {} 0 $sgr
setenv_var NO_COLOR foo
color_test_case {} 0 $sgr
unsetenv_var NO_COLOR

setenv_var CLICOLOR 0
color_test_case {} 0 $sgr
setenv_var CLICOLOR 1
@@ -201,6 +211,21 @@ color_test_case {--color=auto} 1 $sgr
color_test_case {--color=never} 0 $sgr
unsetenv_var CLICOLOR_FORCE

# NO_COLOR prevails over CLICOLOR and CLICOLOR_FORCE
setenv_var NO_COLOR 1
setenv_var CLICOLOR 0
color_test_case {} 0 $sgr
setenv_var CLICOLOR 1
color_test_case {} 0 $sgr
setenv_var CLICOLOR_FORCE 0
color_test_case {} 0 $sgr
setenv_var CLICOLOR_FORCE 1
color_test_case {} 0 $sgr
unsetenv_var CLICOLOR
color_test_case {} 0 $sgr
unsetenv_var CLICOLOR_FORCE
unsetenv_var NO_COLOR

setenv_var MODULES_COLOR always
color_test_case {} 2 $sgr
color_test_case {--color} 2 $sgr
@@ -212,6 +237,10 @@ setenv_var CLICOLOR 0
color_test_case {} 2 $sgr
unsetenv_var CLICOLOR

setenv_var NO_COLOR 1
color_test_case {} 2 $sgr
unsetenv_var NO_COLOR

setenv_var MODULES_COLOR auto
color_test_case {} 1 $sgr
color_test_case {--color} 2 $sgr
@@ -230,6 +259,10 @@ setenv_var CLICOLOR 1
color_test_case {} 0 $sgr
unsetenv_var CLICOLOR

setenv_var NO_COLOR 1
color_test_case {} 0 $sgr
unsetenv_var NO_COLOR

setenv_var CLICOLOR_FORCE 1
color_test_case {} 0 $sgr
unsetenv_var CLICOLOR_FORCE

0 comments on commit 01e2f4a

Please sign in to comment.
You can’t perform that action at this time.