Skip to content

Commit

Permalink
libtextstyle: Support the NO_COLOR environment variable.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bhaible committed Aug 14, 2019
1 parent 8e280e2 commit 7040b13
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion gettext-tools/src/write-catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions libtextstyle/NEWS
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 3 additions & 1 deletion libtextstyle/adhoc-tests/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions libtextstyle/doc/libtextstyle.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion libtextstyle/examples/color-filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion libtextstyle/examples/color-hello/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 7040b13

Please sign in to comment.