Skip to content

Commit 7040b13

Browse files
committed
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.
1 parent 8e280e2 commit 7040b13

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

gettext-tools/src/write-catalog.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ msgdomain_list_print (msgdomain_list_ty *mdlp, const char *filename,
210210
#if ENABLE_COLOR
211211
if (output_syntax->supports_color
212212
&& (color_mode == color_yes
213-
|| (color_mode == color_tty && to_stdout && isatty (STDOUT_FILENO))))
213+
|| (color_mode == color_tty && to_stdout
214+
&& isatty (STDOUT_FILENO)
215+
&& getenv ("NO_COLOR") == NULL)))
214216
{
215217
int fd;
216218
ostream_t stream;

libtextstyle/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
New in 0.21:
2+
* The example programs support the NO_COLOR environment variable,
3+
as specified in https://no-color.org/.
4+
15
New in 0.20:
26
* New class: noop_styled_ostream_t.
37
New constructor function: noop_styled_ostream_create.

libtextstyle/adhoc-tests/hello.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ main (int argc, char *argv[])
5656
}
5757

5858
if (color_mode == color_yes
59-
|| (color_mode == color_tty && isatty (STDOUT_FILENO))
59+
|| (color_mode == color_tty
60+
&& isatty (STDOUT_FILENO)
61+
&& getenv ("NO_COLOR") == NULL)
6062
|| color_mode == color_html)
6163
{
6264
/* If no style file is explicitly specified, use the default in the

libtextstyle/doc/libtextstyle.texi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ scroll around in the styled output. For example:
243243

244244
@menu
245245
* The TERM variable::
246+
* The NO_COLOR variable::
246247
* Emacs::
247248
* The --color option::
248249
* The --style option::
@@ -357,6 +358,19 @@ default @code{TERM=vt100}.
357358

358359
On Windows consoles, no @code{TERM} setting is needed.
359360

361+
@node The NO_COLOR variable
362+
@section The environment variable @code{NO_COLOR}
363+
364+
@vindex NO_COLOR@r{, environment variable}
365+
@c The name of this environment variable is specified by https://no-color.org/.
366+
The environment variable @code{NO_COLOR} can be used to suppress styling
367+
in the textual output. When this environment variable is set (to any value),
368+
@code{libtextstyle}-enabled programs will not emit colors and other text
369+
styling.
370+
371+
This environment variable can be overridden by passing the command-line option
372+
@samp{--color=always} (see @ref{The --color option}).
373+
360374
@node Emacs
361375
@section Emacs as a terminal emulator
362376

libtextstyle/examples/color-filter/filter.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ main (int argc, char *argv[])
8888
}
8989

9090
if (color_mode == color_yes
91-
|| (color_mode == color_tty && isatty (STDOUT_FILENO))
91+
|| (color_mode == color_tty
92+
&& isatty (STDOUT_FILENO)
93+
&& getenv ("NO_COLOR") == NULL)
9294
|| color_mode == color_html)
9395
{
9496
/* Find the style file. */

libtextstyle/examples/color-hello/hello.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ main (int argc, char *argv[])
140140
}
141141

142142
if (color_mode == color_yes
143-
|| (color_mode == color_tty && isatty (STDOUT_FILENO))
143+
|| (color_mode == color_tty
144+
&& isatty (STDOUT_FILENO)
145+
&& getenv ("NO_COLOR") == NULL)
144146
|| color_mode == color_html)
145147
{
146148
/* Find the style file. */

0 commit comments

Comments
 (0)