Skip to content

Commit 1184678

Browse files
bollukripken
authored andcommitted
Allow color API to enable and disable colors (#2111)
This is useful for front-ends which wish to selectively enable or disable coloring. Also expose these APIs from the C API.
1 parent 1095ef9 commit 1184678

15 files changed

Lines changed: 43 additions & 15 deletions

src/binaryen-c.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ir/utils.h"
2727
#include "pass.h"
2828
#include "shell-interface.h"
29+
#include "support/colors.h"
2930
#include "wasm-binary.h"
3031
#include "wasm-builder.h"
3132
#include "wasm-interpreter.h"
@@ -3824,6 +3825,10 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module,
38243825
return NULL;
38253826
}
38263827

3828+
void BinaryenSetColorsEnabled(int enabled) { Colors::setEnabled(enabled); }
3829+
3830+
int BinaryenAreColorsEnabled() { return Colors::isEnabled(); }
3831+
38273832
#ifdef __EMSCRIPTEN__
38283833
// Override atexit - we don't need any global ctors to actually run, and
38293834
// otherwise we get clutter in the output in debug builds

src/binaryen-c.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module,
12211221
BinaryenType* paramTypes,
12221222
BinaryenIndex numParams);
12231223

1224+
// Enable or disable coloring for the WASM printer
1225+
void BinaryenSetColorsEnabled(int enabled);
1226+
1227+
// Query whether color is enable for the WASM printer
1228+
int BinaryenAreColorsEnabled();
12241229
#ifdef __cplusplus
12251230
} // extern "C"
12261231
#endif

src/passes/pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static void dumpWast(Name name, Module* wasm) {
428428
fullName += std::to_string(getpid()) + '-';
429429
#endif
430430
fullName += numstr + "-" + name.str + ".wasm";
431-
Colors::disable();
431+
Colors::setEnabled(false);
432432
ModuleWriter writer;
433433
writer.setBinary(false); // TODO: add an option for binary
434434
writer.write(*wasm, fullName);

src/support/colors.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
#include <ostream>
2121

2222
namespace {
23-
bool colors_disabled = false;
23+
bool colors_enabled = true;
2424
} // anonymous namespace
2525

26-
void Colors::disable() { colors_disabled = true; }
26+
void Colors::setEnabled(bool enabled) { colors_enabled = enabled; }
27+
bool Colors::isEnabled() { return colors_enabled; }
2728

2829
#if defined(__linux__) || defined(__APPLE__)
2930
#include <unistd.h>
@@ -34,7 +35,7 @@ void Colors::outputColorCode(std::ostream& stream, const char* colorCode) {
3435
(isatty(STDOUT_FILENO) &&
3536
(!getenv("COLORS") || getenv("COLORS")[0] != '0')); // implicit
3637
}();
37-
if (has_color && !colors_disabled) {
38+
if (has_color && colors_enabled) {
3839
stream << colorCode;
3940
}
4041
}
@@ -50,7 +51,7 @@ void Colors::outputColorCode(std::ostream& stream, const WORD& colorCode) {
5051
}();
5152
static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
5253
static HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE);
53-
if (has_color && !colors_disabled)
54+
if (has_color && colors_enabled)
5455
SetConsoleTextAttribute(&stream == &std::cout ? hStdout : hStderr,
5556
colorCode);
5657
}

src/support/colors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include <iosfwd>
2121

2222
namespace Colors {
23-
void disable();
23+
void setEnabled(bool enabled);
24+
bool isEnabled();
2425

2526
#if defined(__linux__) || defined(__APPLE__)
2627
void outputColorCode(std::ostream& stream, const char* colorCode);

src/tools/asm2wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main(int argc, const char* argv[]) {
5353
Options::Arguments::One,
5454
[](Options* o, const std::string& argument) {
5555
o->extra["output"] = argument;
56-
Colors::disable();
56+
Colors::setEnabled(false);
5757
})
5858
.add(
5959
"--mapped-globals",

src/tools/wasm-as.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main(int argc, const char* argv[]) {
4646
Options::Arguments::One,
4747
[](Options* o, const std::string& argument) {
4848
o->extra["output"] = argument;
49-
Colors::disable();
49+
Colors::setEnabled(false);
5050
})
5151
.add("--validate",
5252
"-v",

src/tools/wasm-ctor-eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ int main(int argc, const char* argv[]) {
408408
Options::Arguments::One,
409409
[](Options* o, const std::string& argument) {
410410
o->extra["output"] = argument;
411-
Colors::disable();
411+
Colors::setEnabled(false);
412412
})
413413
.add("--emit-text",
414414
"-S",

src/tools/wasm-dis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int argc, const char* argv[]) {
3939
Options::Arguments::One,
4040
[](Options* o, const std::string& argument) {
4141
o->extra["output"] = argument;
42-
Colors::disable();
42+
Colors::setEnabled(false);
4343
})
4444
.add(
4545
"--source-map",

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main(int argc, const char* argv[]) {
5959
Options::Arguments::One,
6060
[&outfile](Options*, const std::string& argument) {
6161
outfile = argument;
62-
Colors::disable();
62+
Colors::setEnabled(false);
6363
})
6464
.add("--debuginfo",
6565
"-g",

0 commit comments

Comments
 (0)