diff options
author | Nico Weber <thakis@chromium.org> | 2012-01-19 00:09:16 (GMT) |
---|---|---|
committer | Nico Weber <thakis@chromium.org> | 2012-01-19 00:09:16 (GMT) |
commit | a2c4b6780dcf105821e4f2ec1f0b591adbeb6dca (patch) | |
tree | a62348131d053354b7d8d5fb1c3ca5b3d5527d67 /src/util.cc | |
parent | 63013b3bd5ca79d454a95a6f985d98f16f21da1c (diff) | |
download | Ninja-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.cc | 28 |
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; +} |