2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 # define isatty(x) _isatty((x))
31 # define fileno(x) _fileno((x))
32 extern WindowsStream StandardError;
33 extern WindowsStream StandardOutput;
40 inline bool CanUseColors()
42 #ifdef INSPIRCD_DISABLE_COLORS
45 return isatty(fileno(stdout));
50 inline WindowsStream& GetStreamHandle(std::ostream& os)
52 if (os.rdbuf() == std::cerr.rdbuf())
55 if (os.rdbuf() == std::cout.rdbuf())
56 return StandardOutput;
58 // This will never happen.
59 throw std::invalid_argument("Tried to write color codes to a stream other than stdout or stderr!");
68 inline std::ostream& con_green(std::ostream& stream)
72 const WindowsStream& ws = GetStreamHandle(stream);
73 SetConsoleTextAttribute(ws.Handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
78 inline std::ostream& con_red(std::ostream& stream)
82 const WindowsStream& ws = GetStreamHandle(stream);
83 SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_INTENSITY | ws.BackgroundColor);
88 inline std::ostream& con_white(std::ostream& stream)
92 const WindowsStream& ws = GetStreamHandle(stream);
93 SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | ws.BackgroundColor);
98 inline std::ostream& con_white_bright(std::ostream& stream)
102 const WindowsStream& ws = GetStreamHandle(stream);
103 SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
108 inline std::ostream& con_bright(std::ostream& stream)
112 const WindowsStream& ws = GetStreamHandle(stream);
113 SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor);
118 inline std::ostream& con_reset(std::ostream& stream)
122 const WindowsStream& ws = GetStreamHandle(stream);
123 SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor);
130 inline std::ostream& con_green(std::ostream& stream)
134 return stream << "\033[1;32m";
137 inline std::ostream& con_red(std::ostream& stream)
141 return stream << "\033[1;31m";
144 inline std::ostream& con_white(std::ostream& stream)
148 return stream << "\033[0m";
151 inline std::ostream& con_white_bright(std::ostream& stream)
155 return stream << "\033[1m";
158 inline std::ostream& con_bright(std::ostream& stream)
162 return stream << "\033[1m";
165 inline std::ostream& con_reset(std::ostream& stream)
169 return stream << "\033[0m";