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.
#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;
+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.
}
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
@menu
* The TERM variable::
+* The NO_COLOR variable::
* Emacs::
* The --color option::
* The --style option::
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
}
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. */
}
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. */