summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2021-03-03 01:52:27 +0000
committerSadie Powell <sadie@witchery.services>2021-03-03 01:52:27 +0000
commitd55581a1107de1e548b67d29a9ca0fcb7f1a0572 (patch)
tree7e84901a544935455efd5ade99222960a3a7c9ea /include
parentdbaa0f6be119a1e74e6139d240d77efcba43b504 (diff)
Fix not being able to colour format output to stderr on Windows.
Diffstat (limited to 'include')
-rw-r--r--include/consolecolors.h51
1 files changed, 41 insertions, 10 deletions
diff --git a/include/consolecolors.h b/include/consolecolors.h
index fdc351f5a..e8329c44e 100644
--- a/include/consolecolors.h
+++ b/include/consolecolors.h
@@ -25,9 +25,12 @@
#include <stdio.h>
#ifdef _WIN32
+# include <iostream>
# include <io.h>
# define isatty(x) _isatty((x))
# define fileno(x) _fileno((x))
+extern WindowsStream StandardError;
+extern WindowsStream StandardOutput;
#else
# include <unistd.h>
#endif
@@ -42,55 +45,83 @@ namespace
return isatty(fileno(stdout));
#endif
}
+
+#ifdef _WIN32
+ inline WindowsStream& GetStreamHandle(std::ostream& os)
+ {
+ if (os.rdbuf() == std::cerr.rdbuf())
+ return StandardError;
+
+ if (os.rdbuf() == std::cout.rdbuf())
+ return StandardOutput;
+
+ // This will never happen.
+ throw std::invalid_argument("Tried to write color codes to a stream other than stdout or stderr!");
+ }
+#endif
}
#ifdef _WIN32
#include <windows.h>
-extern WORD g_wOriginalColors;
-extern WORD g_wBackgroundColor;
-extern HANDLE g_hStdout;
-
inline std::ostream& con_green(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_red(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_white(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_white_bright(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_bright(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor);
+ }
return stream;
}
inline std::ostream& con_reset(std::ostream& stream)
{
if (CanUseColors())
- SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
+ {
+ const WindowsStream& ws = GetStreamHandle(stream);
+ SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor);
+ }
return stream;
}