From 20cce079f258dc3f62340ecd7b9de1bbd99ef814 Mon Sep 17 00:00:00 2001 From: "Yestin L. Harrison" Date: Thu, 1 Jul 2021 18:19:01 -0600 Subject: [PATCH 1/2] Respect TERM=dumb more consistently --- src/libmain/progress-bar.cc | 2 +- src/libutil/logging.cc | 2 +- src/libutil/util.cc | 4 ++++ src/libutil/util.hh | 3 +++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc index 15354549a..b2a6e2a82 100644 --- a/src/libmain/progress-bar.cc +++ b/src/libmain/progress-bar.cc @@ -484,7 +484,7 @@ Logger * makeProgressBar(bool printBuildLogs) { return new ProgressBar( printBuildLogs, - isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb" + shouldANSI() ); } diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index d2e801175..6b9b850ca 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -46,7 +46,7 @@ public: : printBuildLogs(printBuildLogs) { systemd = getEnv("IN_SYSTEMD") == "1"; - tty = isatty(STDERR_FILENO); + tty = shouldANSI(); } bool isVerbose() override { diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 7e57fd7ca..94460b367 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1372,6 +1372,10 @@ void ignoreException() } } +bool shouldANSI() +{ + return isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"; +} std::string filterANSIEscapes(const std::string & s, bool filterAll, unsigned int width) { diff --git a/src/libutil/util.hh b/src/libutil/util.hh index f84d0fb31..a8dd4bd47 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -482,6 +482,9 @@ constexpr char treeLast[] = "└───"; constexpr char treeLine[] = "│ "; constexpr char treeNull[] = " "; +/* Determine whether ANSI escape sequences are appropriate for the + present output. */ +bool shouldANSI(); /* Truncate a string to 'width' printable characters. If 'filterAll' is true, all ANSI escape sequences are filtered out. Otherwise, From 0fe84bef7286bf9b38dbb628f450ac07079f8fed Mon Sep 17 00:00:00 2001 From: "Yestin L. Harrison" Date: Fri, 2 Jul 2021 09:33:54 -0600 Subject: [PATCH 2/2] Add $NO_COLOR check to ANSI escape conditions --- src/libutil/util.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 94460b367..ee9f17228 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1374,7 +1374,9 @@ void ignoreException() bool shouldANSI() { - return isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"; + return isatty(STDERR_FILENO) + && getEnv("TERM").value_or("dumb") != "dumb" + && !getEnv("NO_COLOR").has_value(); } std::string filterANSIEscapes(const std::string & s, bool filterAll, unsigned int width)