InspIRCd  3.0
consolecolors.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * This file is part of InspIRCd. InspIRCd is free software: you can
5  * redistribute it and/or modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation, version 2.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 
18 #pragma once
19 
20 #include <ostream>
21 #include <stdio.h>
22 
23 #ifdef _WIN32
24 # include <io.h>
25 # define isatty(x) _isatty((x))
26 # define fileno(x) _fileno((x))
27 #else
28 # include <unistd.h>
29 #endif
30 
31 namespace
32 {
33  inline bool CanUseColors()
34  {
35 #ifdef INSPIRCD_DISABLE_COLORS
36  return false;
37 #else
38  return isatty(fileno(stdout));
39 #endif
40  }
41 }
42 
43 #ifdef _WIN32
44 
45 #include <windows.h>
46 
47 extern WORD g_wOriginalColors;
48 extern WORD g_wBackgroundColor;
49 extern HANDLE g_hStdout;
50 
51 inline std::ostream& con_green(std::ostream &s)
52 {
53  if (CanUseColors())
54  SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
55  return s;
56 }
57 
58 inline std::ostream& con_red(std::ostream &s)
59 {
60  if (CanUseColors())
61  SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
62  return s;
63 }
64 
65 inline std::ostream& con_white(std::ostream &s)
66 {
67  if (CanUseColors())
68  SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
69  return s;
70 }
71 
72 inline std::ostream& con_white_bright(std::ostream &s)
73 {
74  if (CanUseColors())
75  SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
76  return s;
77 }
78 
79 inline std::ostream& con_bright(std::ostream &s)
80 {
81  if (CanUseColors())
82  SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
83  return s;
84 }
85 
86 inline std::ostream& con_reset(std::ostream &s)
87 {
88  if (CanUseColors())
89  SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
90  return s;
91 }
92 
93 #else
94 
95 inline std::ostream& con_green(std::ostream &s)
96 {
97  if (!CanUseColors())
98  return s;
99  return s << "\033[1;32m";
100 }
101 
102 inline std::ostream& con_red(std::ostream &s)
103 {
104  if (!CanUseColors())
105  return s;
106  return s << "\033[1;31m";
107 }
108 
109 inline std::ostream& con_white(std::ostream &s)
110 {
111  if (!CanUseColors())
112  return s;
113  return s << "\033[0m";
114 }
115 
116 inline std::ostream& con_white_bright(std::ostream &s)
117 {
118  if (!CanUseColors())
119  return s;
120  return s << "\033[1m";
121 }
122 
123 inline std::ostream& con_bright(std::ostream &s)
124 {
125  if (!CanUseColors())
126  return s;
127  return s << "\033[1m";
128 }
129 
130 inline std::ostream& con_reset(std::ostream &s)
131 {
132  if (!CanUseColors())
133  return s;
134  return s << "\033[0m";
135 }
136 
137 #endif