-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleStyle.h
470 lines (390 loc) · 14.8 KB
/
ConsoleStyle.h
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
ConsoleStyle - Colors and styles for your Terminal
***********************************************************************************
The zlib License
Copyright ©2023 Marc Schöndorf
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use of
this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
***********************************************************************************
*/
#ifndef CONSOLE_STYLE_DOT_H
#define CONSOLE_STYLE_DOT_H
//***********************************************************************
// Determine the target OS
#ifndef TARGET_OS_FORCE_UNIX
#if (__linux__) || (__unix__) || (__unix)
#define TARGET_OS_UNIX
#elif (__APPLE__ && __MACH__)
#define TARGET_OS_MAC
#elif _WIN32 // For Win32 and Win64
#define TARGET_OS_WINDOWS
#else
#error The target OS is unknown or unsupported. If you believe your OS is UNIX compatible, manually define "TARGET_OS_FORCE_UNIX" to skip this check.
#endif
#else
#define TARGET_OS_UNIX // User forced to use UNIX
#endif
#include <iostream>
#include <atomic>
#include <mutex>
// OS specific includes
#ifdef TARGET_OS_WINDOWS
//#include <windows.h>
//#include <io.h>
#else
#include <unistd.h>
#endif
namespace ConsoleStyle
{
class Modifier;
//***********************************************************************
// Color and style attributes
enum class STYLE : uint8_t
{
// Styles
bold = 1,
faint = 2,
italic = 3,
underline = 4,
blink = 5,
invert = 7,
hide = 8, // Not widely supported
strike = 9,
// Reset
reset = 0,
// No change
none = 255
};
// Foreground color
enum class FG : uint8_t
{
// Normal intensity
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
gray = 37,
// Bright intensity
black_b = 90,
red_b = 91,
green_b = 92,
yellow_b = 93,
blue_b = 94,
magenta_b = 95,
cyan_b = 96,
white = 97,
// Reset
reset = 39,
// No change
none = 255
};
// Background color
enum class BG : uint8_t
{
// Normal intensity
black = 40,
red = 41,
green = 42,
yellow = 43,
blue = 44,
magenta = 45,
cyan = 46,
gray = 47,
// Bright intensity
black_b = 100,
red_b = 101,
green_b = 102,
yellow_b = 103,
blue_b = 104,
magenta_b = 105,
cyan_b = 106,
white = 107,
// Reset
reset = 49,
// No change
none = 255
};
//***********************************************************************
// Capability modes
enum class CapabilityMode : uint8_t
{
Disable = 0,
CheckOnce = 1,
Auto = 2,
Force = 3
};
//***********************************************************************
// Concept for all ConsoleStyle attributes (colors, styles) and a Modifier
template <typename T>
concept IsConsoleStyleAttribute = std::is_same_v<T, STYLE> or std::is_same_v<T, FG> or std::is_same_v<T, BG>;
template <typename T>
concept IsCombinedModifier = std::is_same_v<T, Modifier>;
//***********************************************************************
// Implementation
class ConsoleCapabilities final
{
private:
std::atomic<bool> m_GotCapabilityInfoIsTerminal = false;
std::atomic<bool> m_GotCapabilityInfoStylesSupported = false;
std::atomic<bool> m_IsTerminalOutput = false;
std::atomic<bool> m_AreStylesSupported = false;
//std::mutex m_CapabilitiesMutex;
inline int Fileno_OS(FILE* const file) const noexcept
{
#ifdef TARGET_OS_WINDOWS
return _fileno(file);
#else
return fileno(file);
#endif
}
inline bool Is_pty_Win(int fd) const noexcept
{
//return false; // Warn me for ToDo
}
inline bool Is_tty_OS(int fd) const noexcept
{
#ifdef TARGET_OS_WINDOWS
return _isatty(fd);
#else
return isatty(fd);
#endif
}
//***********************************************************************
// Is output a terminal
inline bool IsTerminalOutput(const std::streambuf* const sbuf) const noexcept
{
int fd = -1;
// Get file descriptor
if(sbuf == std::cout.rdbuf())
fd = Fileno_OS(stdout);
else if(sbuf == std::cerr.rdbuf() || sbuf == std::clog.rdbuf())
fd = Fileno_OS(stderr);
bool isTerminal = (Is_tty_OS(fd) != 0);
#ifdef TARGET_OS_WINDOWS
// Additional check for Pty/Msys on Windows
if(!isTerminal)
isTerminal = Is_pty_Win(fd);
#endif
return isTerminal;
}
//***********************************************************************
// Are styles and colors supported
inline bool AreStylesSupported() const noexcept
{
#ifdef TARGET_OS_WINDOWS
return true; // Windows always supports colors and (some) styles
#else
static const char* const knownTerminals[] = {
"ansi", "color", "console", "konsole",
"linux", "gnome", "msys", "cygwin", "xterm", "kterm",
"putty", "screen", "vt100", "rxvt" };
const char* const env = std::getenv("TERM");
if(!env)
return false;
return std::any_of(std::begin(knownTerminals), std::end(knownTerminals),
[&](const char* const terminal) { return std::strstr(env, terminal) != nullptr; });
#endif
}
public:
ConsoleCapabilities() = default;
~ConsoleCapabilities() = default;
// Non copyable
ConsoleCapabilities(const ConsoleCapabilities&) = delete;
void operator=(const ConsoleCapabilities&) = delete;
//***********************************************************************
// Check and store capabilities
inline bool CheckIsTerminalOutput(const std::streambuf* const sbuf) noexcept
{
m_IsTerminalOutput = IsTerminalOutput(sbuf);
m_GotCapabilityInfoIsTerminal = true;
return m_IsTerminalOutput;
}
inline bool CheckAreStylesSupported() noexcept
{
m_AreStylesSupported = AreStylesSupported();
m_GotCapabilityInfoStylesSupported = true;
return m_AreStylesSupported;
}
//***********************************************************************
// Get stored capabilities
inline bool GetIsTerminalOutput() const noexcept { return m_IsTerminalOutput; }
inline bool GetAreStylesSupported() const noexcept { return m_AreStylesSupported; }
// Did we check IsTerminalOutput and AreStylesSupported at least once?
inline bool GotCapabilityInfo() const noexcept
{
return m_GotCapabilityInfoIsTerminal && m_GotCapabilityInfoStylesSupported;
}
//***********************************************************************
// Reset stored capabilities and if they were checked at least once
inline void ResetCapabilityInfo() noexcept
{
m_GotCapabilityInfoIsTerminal = false;
m_GotCapabilityInfoStylesSupported = false;
m_IsTerminalOutput = false;
m_AreStylesSupported = false;
}
};
//***********************************************************************
// Implementation
class ConsoleStyleImpl final
{
private:
ConsoleCapabilities m_Capabilities;
std::atomic<CapabilityMode> m_Mode = CapabilityMode::Auto;
public:
ConsoleStyleImpl() = default;
~ConsoleStyleImpl() = default;
// Non copyable
ConsoleStyleImpl(const ConsoleStyleImpl&) = delete;
void operator=(const ConsoleStyleImpl&) = delete;
// Singleton interface
inline static ConsoleStyleImpl& GetInstance() noexcept // Thread safe in C++11 and above
{
static ConsoleStyleImpl me;
return me;
}
//***********************************************************************
// Check capabilities (if necessary and configured)
bool CheckCapabilities(const std::streambuf* const sbuf) noexcept
{
// ToDO: Thread safety
if(m_Mode == CapabilityMode::Disable)
return false;
if(m_Mode == CapabilityMode::Force)
return true;
// Check only once on the first usage (faster, but doesn't catch changing output streams)
if(m_Mode == CapabilityMode::CheckOnce)
{
// Have we already checked?
if(m_Capabilities.GotCapabilityInfo())
{
// Already checked, get stored results
return (m_Capabilities.GetAreStylesSupported() && m_Capabilities.GetIsTerminalOutput());
}
else
{
// Never checked. Explicitly call both functions to store both results
const bool a = m_Capabilities.CheckAreStylesSupported();
const bool b = m_Capabilities.CheckIsTerminalOutput(sbuf);
return (a && b);
}
}
// Auto: Check on every call (slower)
if(m_Mode == CapabilityMode::Auto)
{
return (m_Capabilities.CheckAreStylesSupported() && m_Capabilities.CheckIsTerminalOutput(sbuf));
}
return false;
}
// Setting the capability mode
void SetCapabilityMode(const CapabilityMode& mode) noexcept
{
// Changing the mode requires to clear stored capability info
m_Capabilities.ResetCapabilityInfo();
m_Mode = mode;
}
//***********************************************************************
// Set color or style attribute
#ifdef TARGET_OS_MAC
template <IsConsoleStyleAttribute T>
inline std::ostream& SetAttribute(std::ostream& os, const T attribute)
{
// ToDO: Thread safety
if(attribute == T::none) // No change
return os;
return os << "\033[" << static_cast<int32_t>(attribute) << "m";
}
#else
#error No impl. ToDo
#endif
//***********************************************************************
// std::ostream operator overload for color and style attributes
template <IsConsoleStyleAttribute T>
friend inline std::ostream& operator<<(std::ostream& os, const T& attribute);
// std::ostream operator overload for Modifier
template <IsCombinedModifier T>
friend inline std::ostream& operator<<(std::ostream& os, const T& modifier);
};
//***********************************************************************
// Modifier to combine color and style attributes into one object
class Modifier
{
private:
STYLE m_Style;
FG m_FG;
BG m_BG;
public:
Modifier(const FG& fg = FG::none, const BG& bg = BG::none, const STYLE& style = STYLE::none)
: m_Style(style)
, m_FG(fg)
, m_BG(bg)
{}
template <IsConsoleStyleAttribute T>
inline void Set(const T& attribute) noexcept
{
if constexpr (std::is_same_v<T, FG>)
m_FG = attribute;
else if constexpr (std::is_same_v<T, BG>)
m_BG = attribute;
else if constexpr (std::is_same_v<T, STYLE>)
m_Style = attribute;
}
inline STYLE GetStyle() const noexcept { return m_Style; }
inline FG GetFG() const noexcept { return m_FG; }
inline BG GetBG() const noexcept { return m_BG; }
};
// Reset-all Modifier
static Modifier resetAll(FG::reset, BG::reset, STYLE::reset);
//***********************************************************************
// std::ostream operator overload for color and style attributes
template <IsConsoleStyleAttribute T>
inline std::ostream& operator<<(std::ostream& os, const T& attribute)
{
if(ConsoleStyleImpl::GetInstance().CheckCapabilities(os.rdbuf()))
return ConsoleStyleImpl::GetInstance().SetAttribute(os, attribute);
else
return os;
}
// std::ostream operator overload for Modifier
template <IsCombinedModifier T>
inline std::ostream& operator<<(std::ostream& os, const T& modifier)
{
if(ConsoleStyleImpl::GetInstance().CheckCapabilities(os.rdbuf()))
{
ConsoleStyleImpl::GetInstance().SetAttribute(os, modifier.GetStyle());
ConsoleStyleImpl::GetInstance().SetAttribute(os, modifier.GetBG());
return ConsoleStyleImpl::GetInstance().SetAttribute(os, modifier.GetFG());
}
else
{
return os;
}
}
//***********************************************************************
// Functions to interface configs
void SetConsoleCapabilityMode(const CapabilityMode& mode) noexcept
{
ConsoleStyleImpl::GetInstance().SetCapabilityMode(mode);
}
}
// Shorter name
namespace cstyle = ConsoleStyle;
// Cleanup our preprocessor OS checks
#undef TARGET_OS_UNIX
#undef TARGET_OS_MAC
#undef TARGET_OS_WINDOWS
#endif