Skip to content

Commit

Permalink
Treat NO_COLOR="" the same as unset
Browse files Browse the repository at this point in the history
The docs say

> Command-line software which adds ANSI color to its output by default
> should check for a NO_COLOR environment variable that, when present
> and not an empty string (regardless of its value), prevents the
> addition of ANSI color.

but we were not checking for the empty string.

Link: https://no-color.org/
Link: sharkdp/fd#1421
  • Loading branch information
tavianator committed Nov 6, 2023
1 parent 7d69fef commit 79aee58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3670,7 +3670,8 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) {
}

enum use_color use_color = COLOR_AUTO;
if (getenv("NO_COLOR")) {
const char *no_color = getenv("NO_COLOR");
if (no_color && *no_color) {
// https://no-color.org/
use_color = COLOR_NEVER;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/bfs/nocolor_env_empty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$'rainbow/\e[1m'
$'rainbow/\e[1m/'$'\e[0m'
rainbow
rainbow/exec.sh
rainbow/socket
rainbow/broken
rainbow/chardev_link
rainbow/link.txt
rainbow/sticky_ow
rainbow/sgid
rainbow/pipe
rainbow/ow
rainbow/sugid
rainbow/suid
rainbow/sticky
rainbow/file.dat
rainbow/file.txt
rainbow/lower.gz
rainbow/lower.tar
rainbow/lower.tar.gz
rainbow/lu.tar.GZ
rainbow/mh1
rainbow/mh2
rainbow/ul.TAR.gz
rainbow/upper.GZ
rainbow/upper.TAR
rainbow/upper.TAR.GZ
5 changes: 5 additions & 0 deletions tests/bfs/nocolor_env_empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
command -v unbuffer &>/dev/null || skip

NO_COLOR= bfs_pty rainbow >"$OUT"
sort_output
diff_output

0 comments on commit 79aee58

Please sign in to comment.