X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fconsolecolors.h;h=e7ca5c82e551a61b0c41f4238b4247b199a65fc7;hb=635cb9d65f6d7f6758ae8ed874da00c8d94b6e39;hp=d92be58bc706f9dafa78f9771ad671a1f2f1eda7;hpb=b9e11915a976daaf790ebc763aff56e19fd49e0f;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/consolecolors.h b/include/consolecolors.h index d92be58bc..e7ca5c82e 100644 --- a/include/consolecolors.h +++ b/include/consolecolors.h @@ -1,6 +1,10 @@ /* * InspIRCd -- Internet Relay Chat Daemon * + * Copyright (C) 2019 linuxdaemon + * Copyright (C) 2013, 2019, 2021 Sadie Powell + * Copyright (C) 2012 ChrisTX + * * 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. @@ -18,81 +22,151 @@ #pragma once #include +#include #ifdef _WIN32 +# include +# include +# define isatty(x) _isatty((x)) +# define fileno(x) _fileno((x)) +extern WindowsStream StandardError; +extern WindowsStream StandardOutput; +#else +# include +#endif -#include +namespace +{ + inline bool CanUseColors() + { +#ifdef INSPIRCD_DISABLE_COLORS + return false; +#else + 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 -extern WORD g_wOriginalColors; -extern WORD g_wBackgroundColor; -extern HANDLE g_hStdout; +#include -inline std::ostream& con_green(std::ostream &s) +inline std::ostream& con_green(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor); - return s; + if (CanUseColors()) + { + const WindowsStream& ws = GetStreamHandle(stream); + SetConsoleTextAttribute(ws.Handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor); + } + return stream; } -inline std::ostream& con_red(std::ostream &s) +inline std::ostream& con_red(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor); - return s; + if (CanUseColors()) + { + const WindowsStream& ws = GetStreamHandle(stream); + SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_INTENSITY | ws.BackgroundColor); + } + return stream; } -inline std::ostream& con_white(std::ostream &s) +inline std::ostream& con_white(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor); - return s; + if (CanUseColors()) + { + 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 &s) +inline std::ostream& con_white_bright(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor); - return s; + if (CanUseColors()) + { + 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 &s) +inline std::ostream& con_bright(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor); - return s; + if (CanUseColors()) + { + const WindowsStream& ws = GetStreamHandle(stream); + SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor); + } + return stream; } -inline std::ostream& con_reset(std::ostream &s) +inline std::ostream& con_reset(std::ostream& stream) { - SetConsoleTextAttribute(g_hStdout, g_wOriginalColors); - return s; + if (CanUseColors()) + { + const WindowsStream& ws = GetStreamHandle(stream); + SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor); + } + return stream; } #else -inline std::ostream& con_green(std::ostream &s) +inline std::ostream& con_green(std::ostream& stream) { - return s << "\033[1;32m"; + if (!CanUseColors()) + return stream; + return stream << "\033[1;32m"; } -inline std::ostream& con_red(std::ostream &s) +inline std::ostream& con_red(std::ostream& stream) { - return s << "\033[1;31m"; + if (!CanUseColors()) + return stream; + return stream << "\033[1;31m"; } -inline std::ostream& con_white(std::ostream &s) +inline std::ostream& con_white(std::ostream& stream) { - return s << "\033[0m"; + if (!CanUseColors()) + return stream; + return stream << "\033[0m"; } -inline std::ostream& con_white_bright(std::ostream &s) +inline std::ostream& con_white_bright(std::ostream& stream) { - return s << "\033[1m"; + if (!CanUseColors()) + return stream; + return stream << "\033[1m"; } -inline std::ostream& con_bright(std::ostream &s) +inline std::ostream& con_bright(std::ostream& stream) { - return s << "\033[1m"; + if (!CanUseColors()) + return stream; + return stream << "\033[1m"; } -inline std::ostream& con_reset(std::ostream &s) +inline std::ostream& con_reset(std::ostream& stream) { - return s << "\033[0m"; + if (!CanUseColors()) + return stream; + return stream << "\033[0m"; } #endif