summaryrefslogtreecommitdiff
path: root/include/consolecolors.h
blob: e7ca5c82e551a61b0c41f4238b4247b199a65fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
 *   Copyright (C) 2013, 2019, 2021 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include <ostream>
#include <stdio.h>

#ifdef _WIN32
# include <iostream>
# include <io.h>
# define isatty(x) _isatty((x))
# define fileno(x) _fileno((x))
extern WindowsStream StandardError;
extern WindowsStream StandardOutput;
#else
# include <unistd.h>
#endif

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

#include <windows.h>

inline std::ostream& con_green(std::ostream& stream)
{
	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& stream)
{
	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& stream)
{
	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& stream)
{
	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& stream)
{
	if (CanUseColors())
	{
		const WindowsStream& ws = GetStreamHandle(stream);
		SetConsoleTextAttribute(ws.Handle, FOREGROUND_INTENSITY | ws.BackgroundColor);
	}
	return stream;
}

inline std::ostream& con_reset(std::ostream& stream)
{
	if (CanUseColors())
	{
		const WindowsStream& ws = GetStreamHandle(stream);
		SetConsoleTextAttribute(ws.Handle, ws.ForegroundColor);
	}
	return stream;
}

#else

inline std::ostream& con_green(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[1;32m";
}

inline std::ostream& con_red(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[1;31m";
}

inline std::ostream& con_white(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[0m";
}

inline std::ostream& con_white_bright(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[1m";
}

inline std::ostream& con_bright(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[1m";
}

inline std::ostream& con_reset(std::ostream& stream)
{
	if (!CanUseColors())
		return stream;
	return stream << "\033[0m";
}

#endif