summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/support/colors.cpp27
-rw-r--r--src/support/colors.h15
2 files changed, 34 insertions, 8 deletions
diff --git a/src/support/colors.cpp b/src/support/colors.cpp
index 94f8bedc9..9a3b04f70 100644
--- a/src/support/colors.cpp
+++ b/src/support/colors.cpp
@@ -19,24 +19,37 @@
#include <cstdlib>
#include <ostream>
-#if defined(__linux__) || defined(__APPLE__)
-# define CAN_HAZ_COLOR 1
-# include <unistd.h>
-#endif
-
namespace {
bool colors_disabled = false;
} // anonymous namespace
void Colors::disable() { colors_disabled = true; }
+#if defined(__linux__) || defined(__APPLE__)
+#include <unistd.h>
+
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 && !colors_disabled) stream << colorCode;
-#endif
}
+#elif defined(_WIN32)
+#include <windows.h>
+#include <io.h>
+#include <iostream>
+
+void Colors::outputColorCode(std::ostream&stream, const WORD &colorCode) {
+ const static bool has_color = []() {
+ return (getenv("COLORS") && getenv("COLORS")[0] == '1') || // forced
+ (_isatty(_fileno(stdout)) &&
+ (!getenv("COLORS") || getenv("COLORS")[0] != '0')); // implicit
+ }();
+ static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
+ static HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE);
+ if (has_color && !colors_disabled)
+ SetConsoleTextAttribute(&stream == &std::cout ? hStdout : hStderr, colorCode);
+}
+#endif
diff --git a/src/support/colors.h b/src/support/colors.h
index 41a39d589..d81ecbc16 100644
--- a/src/support/colors.h
+++ b/src/support/colors.h
@@ -21,7 +21,9 @@
namespace Colors {
void disable();
-void outputColorCode(std::ostream&stream, const char *colorCode);
+
+#if defined(__linux__) || defined(__APPLE__)
+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"); }
inline void magenta(std::ostream& stream) { outputColorCode(stream,"\033[35m"); }
@@ -30,6 +32,17 @@ inline void grey(std::ostream& stream) { outputColorCode(stream,"\033[37m"); }
inline void green(std::ostream& stream) { outputColorCode(stream,"\033[32m"); }
inline void blue(std::ostream& stream) { outputColorCode(stream,"\033[34m"); }
inline void bold(std::ostream& stream) { outputColorCode(stream,"\033[1m"); }
+#elif defined(_WIN32)
+void outputColorCode(std::ostream& stream, const unsigned short &colorCode);
+inline void normal(std::ostream& stream) { outputColorCode(stream, 0x07); }
+inline void red(std::ostream& stream) { outputColorCode(stream, 0x0c); }
+inline void magenta(std::ostream& stream) { outputColorCode(stream, 0x05); }
+inline void orange(std::ostream& stream) { outputColorCode(stream, 0x06); }
+inline void grey(std::ostream& stream) { outputColorCode(stream, 0x08); }
+inline void green(std::ostream& stream) { outputColorCode(stream, 0x02); }
+inline void blue(std::ostream& stream) { outputColorCode(stream, 0x09); }
+inline void bold(std::ostream& stream) { /* Do nothing */ }
+#endif
};
#endif // wasm_support_color_h