summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJF Bastien <jfb@chromium.org>2016-01-04 17:22:58 -0800
committerJF Bastien <jfb@chromium.org>2016-01-04 17:22:58 -0800
commit908539ab1f4cc54a9511f076ae670457533806a4 (patch)
tree6f496dc3fc425cb24ce47507ef5b0968a8f6e60f
parentc6d08d97c196e2ceae5e3b960befcd6c66916cad (diff)
downloadbinaryen-908539ab1f4cc54a9511f076ae670457533806a4.tar.gz
binaryen-908539ab1f4cc54a9511f076ae670457533806a4.tar.bz2
binaryen-908539ab1f4cc54a9511f076ae670457533806a4.zip
Disable colors when outputting to a file
This will allow other tools to consume the output.
-rw-r--r--src/s2wasm-main.cpp2
-rw-r--r--src/support/colors.cpp12
-rw-r--r--src/support/colors.h1
3 files changed, 11 insertions, 4 deletions
diff --git a/src/s2wasm-main.cpp b/src/s2wasm-main.cpp
index a7060dd6e..1940fcb1a 100644
--- a/src/s2wasm-main.cpp
+++ b/src/s2wasm-main.cpp
@@ -18,6 +18,7 @@
// wasm2asm console tool
//
+#include "support/colors.h"
#include "support/command-line.h"
#include "s2wasm.h"
@@ -30,6 +31,7 @@ int main(int argc, const char *argv[]) {
Options::Arguments::One,
[](Options *o, const std::string &argument) {
o->extra["output"] = argument;
+ Colors::disable();
})
.add("--global-base", "-g", "Where to start to place globals",
Options::Arguments::One,
diff --git a/src/support/colors.cpp b/src/support/colors.cpp
index aca720514..7310427e4 100644
--- a/src/support/colors.cpp
+++ b/src/support/colors.cpp
@@ -24,15 +24,19 @@
# include <unistd.h>
#endif
-namespace Colors {
-void outputColorCode(std::ostream& stream, const char* colorCode) {
+namespace {
+bool colors_disabled = false;
+} // anonymous namespace
+
+void Colors::disable() { colors_disabled = true; }
+
+void Colors::outputColorCode(std::ostream& stream, const char* colorCode) {
#if defined(CAN_HAZ_COLOR)
const static bool has_color = []() {
return (getenv("COLORS") && getenv("COLORS")[0] == '1') || // forced
(isatty(STDOUT_FILENO) &&
(!getenv("COLORS") || getenv("COLORS")[0] != '0')); // implicit
}();
- if (has_color) stream << colorCode;
+ if (has_color && !colors_disabled) stream << colorCode;
#endif
}
-} // namespace Colors
diff --git a/src/support/colors.h b/src/support/colors.h
index 03ebb1240..41a39d589 100644
--- a/src/support/colors.h
+++ b/src/support/colors.h
@@ -20,6 +20,7 @@
#include <iosfwd>
namespace Colors {
+void disable();
void outputColorCode(std::ostream&stream, const char *colorCode);
inline void normal(std::ostream& stream) { outputColorCode(stream,"\033[0m"); }
inline void red(std::ostream& stream) { outputColorCode(stream,"\033[31m"); }