]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/consolecolors.h
Remove the Kiwi links from the readme.
[user/henk/code/inspircd.git] / include / consolecolors.h
index 70696fbf9d19ec786b200a7665c8cf0fb5fcfc5e..e7ca5c82e551a61b0c41f4238b4247b199a65fc7 100644 (file)
@@ -1,6 +1,10 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2013, 2019, 2021 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
+ *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
  * License as published by the Free Software Foundation, version 2.
 #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
@@ -38,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;
 }