InspIRCd  3.0
consolecolors.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 linuxdaemon <[email protected]>
5  * Copyright (C) 2013, 2019, 2021 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 ChrisTX <[email protected]>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20 
21 
22 #pragma once
23 
24 #include <ostream>
25 #include <stdio.h>
26 
27 #ifdef _WIN32
28 # include <iostream>
29 # include <io.h>
30 # define isatty(x) _isatty((x))
31 # define fileno(x) _fileno((x))
32 extern WindowsStream StandardError;
33 extern WindowsStream StandardOutput;
34 #else
35 # include <unistd.h>
36 #endif
37 
38 namespace
39 {
40  inline bool CanUseColors()
41  {
42 #ifdef INSPIRCD_DISABLE_COLORS
43  return false;
44 #else
45  return isatty(fileno(stdout));
46 #endif
47  }
48 
49 #ifdef _WIN32
50  inline WindowsStream& GetStreamHandle(std::ostream& os)
51  {
52  if (os.rdbuf() == std::cerr.rdbuf())
53  return StandardError;
54 
55  if (os.rdbuf() == std::cout.rdbuf())
56  return StandardOutput;
57 
58  // This will never happen.
59  throw std::invalid_argument("Tried to write color codes to a stream other than stdout or stderr!");
60  }
61 #endif
62 }
63 
64 #ifdef _WIN32
65 
66 #include <windows.h>
67 
68 inline std::ostream& con_green(std::ostream& stream)
69 {
70  if (CanUseColors())
71  {
72  const WindowsStream& ws = GetStreamHandle(stream);
73  SetConsoleTextAttribute(ws.Handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
74  }
75  return stream;
76 }
77 
78 inline std::ostream& con_red(std::ostream& stream)
79 {
80  if (CanUseColors())
81  {
82  const WindowsStream& ws = GetStreamHandle(stream);
83  SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_INTENSITY | ws.BackgroundColor);
84  }
85  return stream;
86 }
87 
88 inline std::ostream& con_white(std::ostream& stream)
89 {
90  if (CanUseColors())
91  {
92  const WindowsStream& ws = GetStreamHandle(stream);
93  SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | ws.BackgroundColor);
94  }
95  return stream;
96 }
97 
98 inline std::ostream& con_white_bright(std::ostream& stream)
99 {
100  if (CanUseColors())
101  {
102  const WindowsStream& ws = GetStreamHandle(stream);
103  SetConsoleTextAttribute(ws.Handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | ws.BackgroundColor);
104  }
105  return stream;
106 }
107 
108 inline std::ostream& con_bright(std::ostream& stream)
109 {
110  if (CanUseColors())
111  {
112  const WindowsStream& ws = GetStreamHandle(stream);
113  SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor);
114  }
115  return stream;
116 }
117 
118 inline std::ostream& con_reset(std::ostream& stream)
119 {
120  if (CanUseColors())
121  {
122  const WindowsStream& ws = GetStreamHandle(stream);
123  SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor);
124  }
125  return stream;
126 }
127 
128 #else
129 
130 inline std::ostream& con_green(std::ostream& stream)
131 {
132  if (!CanUseColors())
133  return stream;
134  return stream << "\033[1;32m";
135 }
136 
137 inline std::ostream& con_red(std::ostream& stream)
138 {
139  if (!CanUseColors())
140  return stream;
141  return stream << "\033[1;31m";
142 }
143 
144 inline std::ostream& con_white(std::ostream& stream)
145 {
146  if (!CanUseColors())
147  return stream;
148  return stream << "\033[0m";
149 }
150 
151 inline std::ostream& con_white_bright(std::ostream& stream)
152 {
153  if (!CanUseColors())
154  return stream;
155  return stream << "\033[1m";
156 }
157 
158 inline std::ostream& con_bright(std::ostream& stream)
159 {
160  if (!CanUseColors())
161  return stream;
162  return stream << "\033[1m";
163 }
164 
165 inline std::ostream& con_reset(std::ostream& stream)
166 {
167  if (!CanUseColors())
168  return stream;
169  return stream << "\033[0m";
170 }
171 
172 #endif
Definition: inspircd_win32wrapper.h:201