From 486872150638d56483c2bc429ba9e137d9974ee8 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 15 Mar 2018 16:08:07 +0100 Subject: Filter ANSI colors when not writing to a terminal Fixes https://github.com/NixOS/nixpkgs/issues/37114. --- src/libutil/logging.cc | 2 +- src/libutil/util.cc | 4 ++-- src/libutil/util.hh | 10 ++++++---- src/nix/progress-bar.cc | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index e860b27d3d..799c6e1ae4 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -53,7 +53,7 @@ public: prefix = std::string("<") + c + ">"; } - writeToStderr(prefix + filterANSIEscapes(fs.s) + "\n"); + writeToStderr(prefix + filterANSIEscapes(fs.s, !tty) + "\n"); } void startActivity(ActivityId act, Verbosity lvl, ActivityType type, diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 2391e14a94..a60ba8508e 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1185,7 +1185,7 @@ void ignoreException() } -std::string filterANSIEscapes(const std::string & s, unsigned int width) +std::string filterANSIEscapes(const std::string & s, bool filterAll, unsigned int width) { std::string t, e; size_t w = 0; @@ -1210,7 +1210,7 @@ std::string filterANSIEscapes(const std::string & s, unsigned int width) if (i != s.end() && *i >= 0x40 && *i <= 0x5f) e += *i++; } - if (last == 'm') + if (!filterAll && last == 'm') t += e; } diff --git a/src/libutil/util.hh b/src/libutil/util.hh index c5c537ee63..500ab7811b 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -391,11 +391,13 @@ void ignoreException(); #define ANSI_BLUE "\e[34;1m" -/* Truncate a string to 'width' printable characters. Certain ANSI - escape sequences (such as colour setting) are copied but not - included in the character count. Other ANSI escape sequences are - filtered. Also, tabs are expanded to spaces. */ +/* Truncate a string to 'width' printable characters. If 'filterAll' + is true, all ANSI escape sequences are filtered out. Otherwise, + some escape sequences (such as colour setting) are copied but not + included in the character count. Also, tabs are expanded to + spaces. */ std::string filterANSIEscapes(const std::string & s, + bool filterAll = false, unsigned int width = std::numeric_limits::max()); diff --git a/src/nix/progress-bar.cc b/src/nix/progress-bar.cc index e6553c06f4..40b905ba32 100644 --- a/src/nix/progress-bar.cc +++ b/src/nix/progress-bar.cc @@ -308,7 +308,7 @@ public: auto width = getWindowSize().second; if (width <= 0) std::numeric_limits::max(); - writeToStderr("\r" + filterANSIEscapes(line, width) + "\e[K"); + writeToStderr("\r" + filterANSIEscapes(line, false, width) + "\e[K"); } std::string getStatus(State & state) -- cgit 1.4.1