summaryrefslogtreecommitdiffstats
path: root/src/util.cc
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2012-01-19 00:09:16 (GMT)
committerNico Weber <thakis@chromium.org>2012-01-19 00:09:16 (GMT)
commita2c4b6780dcf105821e4f2ec1f0b591adbeb6dca (patch)
treea62348131d053354b7d8d5fb1c3ca5b3d5527d67 /src/util.cc
parent63013b3bd5ca79d454a95a6f985d98f16f21da1c (diff)
downloadNinja-a2c4b6780dcf105821e4f2ec1f0b591adbeb6dca.zip
Ninja-a2c4b6780dcf105821e4f2ec1f0b591adbeb6dca.tar.gz
Ninja-a2c4b6780dcf105821e4f2ec1f0b591adbeb6dca.tar.bz2
Strip ansi escape sequences from subcommand output when not writing to a smart terminal.
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index dbf7566..5f5d8dc 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -255,3 +255,31 @@ string GetLastErrorString() {
return msg;
}
#endif
+
+static bool islatinalpha(int c) {
+ // isalpha() is locale-dependent.
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+}
+
+string StripAnsiEscapeCodes(const string& in) {
+ string stripped;
+ stripped.reserve(in.size());
+
+ for (size_t i = 0; i < in.size(); ++i) {
+ if (in[i] != '\33') {
+ // Not an escape code.
+ stripped.push_back(in[i]);
+ continue;
+ }
+
+ // Only strip CSIs for now.
+ if (i + 1 >= in.size()) break;
+ if (in[i + 1] != '[') continue; // Not a CSI.
+ i += 2;
+
+ // Skip everything up to and including the next [a-zA-Z].
+ while (i < in.size() && !islatinalpha(in[i]))
+ ++i;
+ }
+ return stripped;
+}