]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libtextstyle: Support the NO_COLOR environment variable.
authorBruno Haible <bruno@clisp.org>
Wed, 14 Aug 2019 00:46:36 +0000 (02:46 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 14 Aug 2019 00:46:36 +0000 (02:46 +0200)
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.

gettext-tools/src/write-catalog.c
libtextstyle/NEWS
libtextstyle/adhoc-tests/hello.c
libtextstyle/doc/libtextstyle.texi
libtextstyle/examples/color-filter/filter.c
libtextstyle/examples/color-hello/hello.c

index 67d79e122879d4be1da9f42fc827545f3bfd9b77..504bbaccdde59c9c4636df3d8a4c33e115e19921 100644 (file)
@@ -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;
index d1ef466d9bda13db7f4413dca70bcb6ae87c7202..915a55d66fde1164a932ee8293e64307c292c01d 100644 (file)
@@ -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.
index 1147493639ad16d7f15a766d87d9489133bce842..1f827e57c18d613cebe62d442b66ccaf27942fd5 100644 (file)
@@ -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
index d641a07377bcc52bf504237c9ed801bbb5d3956f..30b955a99f54a0323a63bfd40c4a9d5f22da7aaf 100644 (file)
@@ -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
 
index e97e2f96b719283d001b1cd78cfaab2a3263889d..483d57ae5bd235d79d758ba9ec5a92e9c8223aed 100644 (file)
@@ -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.  */
index 87e091f127d547930bd719c16fa873d39c964a97..272053d74b2b815586753b45ff5157405107f10b 100644 (file)
@@ -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.  */