-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerminalColor.hpp
164 lines (146 loc) · 6.14 KB
/
TerminalColor.hpp
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
//=====================================================================
// Quentin Quadrat <lecrapouille@gmail.com>
// License: Public Domain :)
//=====================================================================
#ifndef TERMINAL_COLOR_HPP
# define TERMINAL_COLOR_HPP
# include <atomic>
# include <utility>
# include <iostream>
// **************************************************************
//! \brief Namespace for managing colorful messages in console.
//! This code was inspired by https://github.com/agauniyal/rang
//! but contains less stuffs than the original. For example this
//! code will only work with consoles supporting ANSI color.
//!
//! Example:
//! --------
//! termcolor::enable();
//! std::cout << termcolor::fg::green << termcolor::bg::red << "hello"
//! << std::endl;
//! std::cout << termcolor::state::On << termcolor::fg::red << "hello"
//! << termcolor::fg::green << termcolor::bg::red << " word"
//! << termcolor::style::normal
//! << termcolor::state::Off
//! << std::endl;
// **************************************************************
namespace termcolor
{
//------------------------------------------------------------------
//! \brief Switch on or switch off colorful text. When off even if
//! you write std::cout << termcolor::fg::green the green color will
//! be ignored.
//------------------------------------------------------------------
enum class state {
off = 0, //! \brief Color informations are ignored.
on = 1 //! \brief Color informations are taken into account.
};
//------------------------------------------------------------------
//! \brief Return if color informations are currently ignored or
//! taken into account.
//------------------------------------------------------------------
inline std::atomic<termcolor::state>& isEnabled()
{
static std::atomic<termcolor::state> value(termcolor::state::on);
return value;
}
//------------------------------------------------------------------
//! \brief Color informations will be taken into account.
//------------------------------------------------------------------
inline void enable()
{
termcolor::isEnabled() = termcolor::state::on;
}
//------------------------------------------------------------------
//! \brief Color informations will be ignored.
//------------------------------------------------------------------
inline void disable()
{
termcolor::isEnabled() = termcolor::state::off;
}
//------------------------------------------------------------------
//! \brief Alternative way for isEnabled() using overloaded << operator.
//------------------------------------------------------------------
inline std::ostream& operator<<(std::ostream &os, const termcolor::state& value)
{
termcolor::isEnabled() = value;
return os;
}
//------------------------------------------------------------------
//! \brief ANSI text style
//------------------------------------------------------------------
enum class style {
normal = 0, bold, dim, italic, underline, blink, rblink, reversed,
conceal, crossed
};
//------------------------------------------------------------------
//! \brief ANSI foreground color
//------------------------------------------------------------------
enum class fg {
black = 30, red, green, yellow, blue, magenta, cyan, gray, reset
};
//------------------------------------------------------------------
//! \brief ANSI background color
//------------------------------------------------------------------
enum class bg {
normal = 1, black = 40, red, green, yellow, blue, magenta, cyan,
gray, reset
};
//------------------------------------------------------------------
//! \brief Structure for holding color information in a local variable
//------------------------------------------------------------------
struct color
{
color(termcolor::style style = termcolor::style::normal,
termcolor::fg fg = termcolor::fg::reset)
: m_style(style), m_fg(fg)
{}
termcolor::style m_style;
termcolor::fg m_fg;
};
//------------------------------------------------------------------
//! \brief Needed by g++-5.4 else it will not compile this file and
//! fail with operator<< overload. Newest gcc do not complain. This
//! code allows the template T to be either the struct color or enums
//! (fg, bg or style).
//------------------------------------------------------------------
template <typename T>
using enableIfColorTyped = typename std::enable_if<
std::is_same<T, termcolor::color>::value ||
std::is_same<T, termcolor::style>::value ||
std::is_same<T, termcolor::fg>::value ||
std::is_same<T, termcolor::bg>::value, std::ostream&>::type;
//------------------------------------------------------------------
//! \brief Change the console background or foreground color or
//! style given by the template T.
//------------------------------------------------------------------
template <typename T>
inline enableIfColorTyped<T> setColor(std::ostream &os, const T& value)
{
return os << "\033[" << static_cast<int>(value) << 'm';
}
//------------------------------------------------------------------
//! \brief Change the console background or foreground color and the
//! style given by the structure.
//------------------------------------------------------------------
template <>
inline std::ostream& setColor<termcolor::color>(std::ostream &os,
const termcolor::color& c)
{
return os << "\033["
<< static_cast<int>(c.m_style) << ';'
<< static_cast<int>(c.m_fg) << 'm';
}
//------------------------------------------------------------------
//! \brief Overload the << operator. T can be either fg or bg or style
//! or color.
//------------------------------------------------------------------
template <typename T>
inline enableIfColorTyped<T> operator<<(std::ostream &os, const T& value)
{
if (termcolor::state::on == termcolor::isEnabled())
return setColor(os, value);
return os;
}
}
#endif // TERMINAL_COLOR_HPP