From: Bruno Haible Date: Wed, 14 Aug 2019 00:46:36 +0000 (+0200) Subject: libtextstyle: Support the NO_COLOR environment variable. X-Git-Tag: v0.21~195 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7040b13946b3dce5fbe2be8ecfda11e03eed7028;p=thirdparty%2Fgettext.git libtextstyle: Support the NO_COLOR environment variable. It follows the specification at https://no-color.org/. The support is in the example programs, not in the function styled_ostream_create, so that it can be overridden through the command-line option --color=always. We only test whether the environment variable is set, not whether its value is non-empty. POSIX-specified environment variables are treated like unset when their value is empty; this is because in the old days it was not possible to unset an environment variable. But nowadays: - all shells support the 'unset' built-in, - all platforms that have the setenv() function also have the unsetenv() function, and - the 'env' program from GNU coreutils supports --unset=VARIABLE to unset a variable. This makes it possible to unset an environment variable that is set by the parent process. * libtextstyle/adhoc-tests/hello.c (main): Do not emit styling when the environment variable NO_COLOR is set. * libtextstyle/examples/color-filter/filter.c (main): Likewise. * libtextstyle/examples/color-hello/hello.c (main): Likewise. * gettext-tools/src/write-catalog.c (msgdomain_list_print): Likewise. * libtextstyle/doc/libtextstyle.texi (The NO_COLOR variable): New section. * libtextstyle/NEWS: Mention it. --- diff --git a/gettext-tools/src/write-catalog.c b/gettext-tools/src/write-catalog.c index 67d79e122..504bbaccd 100644 --- a/gettext-tools/src/write-catalog.c +++ b/gettext-tools/src/write-catalog.c @@ -210,7 +210,9 @@ msgdomain_list_print (msgdomain_list_ty *mdlp, const char *filename, #if ENABLE_COLOR if (output_syntax->supports_color && (color_mode == color_yes - || (color_mode == color_tty && to_stdout && isatty (STDOUT_FILENO)))) + || (color_mode == color_tty && to_stdout + && isatty (STDOUT_FILENO) + && getenv ("NO_COLOR") == NULL))) { int fd; ostream_t stream; diff --git a/libtextstyle/NEWS b/libtextstyle/NEWS index d1ef466d9..915a55d66 100644 --- a/libtextstyle/NEWS +++ b/libtextstyle/NEWS @@ -1,3 +1,7 @@ +New in 0.21: +* The example programs support the NO_COLOR environment variable, + as specified in https://no-color.org/. + New in 0.20: * New class: noop_styled_ostream_t. New constructor function: noop_styled_ostream_create. diff --git a/libtextstyle/adhoc-tests/hello.c b/libtextstyle/adhoc-tests/hello.c index 114749363..1f827e57c 100644 --- a/libtextstyle/adhoc-tests/hello.c +++ b/libtextstyle/adhoc-tests/hello.c @@ -56,7 +56,9 @@ main (int argc, char *argv[]) } if (color_mode == color_yes - || (color_mode == color_tty && isatty (STDOUT_FILENO)) + || (color_mode == color_tty + && isatty (STDOUT_FILENO) + && getenv ("NO_COLOR") == NULL) || color_mode == color_html) { /* If no style file is explicitly specified, use the default in the diff --git a/libtextstyle/doc/libtextstyle.texi b/libtextstyle/doc/libtextstyle.texi index d641a0737..30b955a99 100644 --- a/libtextstyle/doc/libtextstyle.texi +++ b/libtextstyle/doc/libtextstyle.texi @@ -243,6 +243,7 @@ scroll around in the styled output. For example: @menu * The TERM variable:: +* The NO_COLOR variable:: * Emacs:: * The --color option:: * The --style option:: @@ -357,6 +358,19 @@ default @code{TERM=vt100}. On Windows consoles, no @code{TERM} setting is needed. +@node The NO_COLOR variable +@section The environment variable @code{NO_COLOR} + +@vindex NO_COLOR@r{, environment variable} +@c The name of this environment variable is specified by https://no-color.org/. +The environment variable @code{NO_COLOR} can be used to suppress styling +in the textual output. When this environment variable is set (to any value), +@code{libtextstyle}-enabled programs will not emit colors and other text +styling. + +This environment variable can be overridden by passing the command-line option +@samp{--color=always} (see @ref{The --color option}). + @node Emacs @section Emacs as a terminal emulator diff --git a/libtextstyle/examples/color-filter/filter.c b/libtextstyle/examples/color-filter/filter.c index e97e2f96b..483d57ae5 100644 --- a/libtextstyle/examples/color-filter/filter.c +++ b/libtextstyle/examples/color-filter/filter.c @@ -88,7 +88,9 @@ main (int argc, char *argv[]) } if (color_mode == color_yes - || (color_mode == color_tty && isatty (STDOUT_FILENO)) + || (color_mode == color_tty + && isatty (STDOUT_FILENO) + && getenv ("NO_COLOR") == NULL) || color_mode == color_html) { /* Find the style file. */ diff --git a/libtextstyle/examples/color-hello/hello.c b/libtextstyle/examples/color-hello/hello.c index 87e091f12..272053d74 100644 --- a/libtextstyle/examples/color-hello/hello.c +++ b/libtextstyle/examples/color-hello/hello.c @@ -140,7 +140,9 @@ main (int argc, char *argv[]) } if (color_mode == color_yes - || (color_mode == color_tty && isatty (STDOUT_FILENO)) + || (color_mode == color_tty + && isatty (STDOUT_FILENO) + && getenv ("NO_COLOR") == NULL) || color_mode == color_html) { /* Find the style file. */