]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/consolecolors.h
fdc351f5a4b6e52059d5c0d1148ba6e7c6060318
[user/henk/code/inspircd.git] / include / consolecolors.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
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 <io.h>
29 # define isatty(x) _isatty((x))
30 # define fileno(x) _fileno((x))
31 #else
32 # include <unistd.h>
33 #endif
34
35 namespace
36 {
37         inline bool CanUseColors()
38         {
39 #ifdef INSPIRCD_DISABLE_COLORS
40                 return false;
41 #else
42                 return isatty(fileno(stdout));
43 #endif
44         }
45 }
46
47 #ifdef _WIN32
48
49 #include <windows.h>
50
51 extern WORD g_wOriginalColors;
52 extern WORD g_wBackgroundColor;
53 extern HANDLE g_hStdout;
54
55 inline std::ostream& con_green(std::ostream& stream)
56 {
57         if (CanUseColors())
58                 SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
59         return stream;
60 }
61
62 inline std::ostream& con_red(std::ostream& stream)
63 {
64         if (CanUseColors())
65                 SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
66         return stream;
67 }
68
69 inline std::ostream& con_white(std::ostream& stream)
70 {
71         if (CanUseColors())
72                 SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
73         return stream;
74 }
75
76 inline std::ostream& con_white_bright(std::ostream& stream)
77 {
78         if (CanUseColors())
79                 SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
80         return stream;
81 }
82
83 inline std::ostream& con_bright(std::ostream& stream)
84 {
85         if (CanUseColors())
86                 SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
87         return stream;
88 }
89
90 inline std::ostream& con_reset(std::ostream& stream)
91 {
92         if (CanUseColors())
93                 SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
94         return stream;
95 }
96
97 #else
98
99 inline std::ostream& con_green(std::ostream& stream)
100 {
101         if (!CanUseColors())
102                 return stream;
103         return stream << "\033[1;32m";
104 }
105
106 inline std::ostream& con_red(std::ostream& stream)
107 {
108         if (!CanUseColors())
109                 return stream;
110         return stream << "\033[1;31m";
111 }
112
113 inline std::ostream& con_white(std::ostream& stream)
114 {
115         if (!CanUseColors())
116                 return stream;
117         return stream << "\033[0m";
118 }
119
120 inline std::ostream& con_white_bright(std::ostream& stream)
121 {
122         if (!CanUseColors())
123                 return stream;
124         return stream << "\033[1m";
125 }
126
127 inline std::ostream& con_bright(std::ostream& stream)
128 {
129         if (!CanUseColors())
130                 return stream;
131         return stream << "\033[1m";
132 }
133
134 inline std::ostream& con_reset(std::ostream& stream)
135 {
136         if (!CanUseColors())
137                 return stream;
138         return stream << "\033[0m";
139 }
140
141 #endif