From cf32dc8b4fd4d56b14e43aacdb0f08aecaa19f6d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 27 Apr 2019 11:45:59 -0400 Subject: Add support for the NO_COLOR environment variable C.f. https://no-color.org/ --- parse.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/parse.c b/parse.c index 2eb7eca..6367cd6 100644 --- a/parse.c +++ b/parse.c @@ -3162,9 +3162,15 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { cmdline->argv[i] = argv[i]; } + enum use_color use_color = COLOR_AUTO; + if (getenv("NO_COLOR")) { + // https://no-color.org/ + use_color = COLOR_NEVER; + } + cmdline->colors = parse_colors(getenv("LS_COLORS")); - cmdline->cout = cfdup(stdout, cmdline->colors); - cmdline->cerr = cfdup(stderr, cmdline->colors); + cmdline->cout = cfdup(stdout, use_color ? cmdline->colors : NULL); + cmdline->cerr = cfdup(stderr, use_color ? cmdline->colors : NULL); if (!cmdline->cout || !cmdline->cerr) { perror("cfdup()"); goto fail; @@ -3175,7 +3181,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { cmdline->mtab_error = errno; } - bool stderr_tty = cmdline->cerr->colors; + bool stderr_tty = isatty(STDERR_FILENO); bool stdin_tty = isatty(STDIN_FILENO); struct parser_state state = { @@ -3185,7 +3191,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { .roots_tail = &cmdline->roots, .regex_flags = 0, .interactive = stderr_tty && stdin_tty, - .use_color = COLOR_AUTO, + .use_color = use_color, .implicit_print = true, .warn = stdin_tty, .non_option_seen = false, -- cgit v1.2.3