44 explicit Color(
enum C c = DEFAULT) noexcept :
Color(std::cout, c) {}
51 explicit Color(std::ostream &
os,
enum C c = DEFAULT) noexcept : m_NL(
true), m_os(
os) {
53 m_os << seqBegin << c <<
'm';
62 Color(
unsigned char r,
unsigned char g,
unsigned char b) noexcept
63 :
Color(std::cout, r, g, b) {}
72 Color(std::ostream &
os,
unsigned char r,
unsigned char g,
unsigned char b) noexcept
73 : m_NL(
true), m_os(
os) {
76 m_os << seqBegin << COL256 <<
";2;" <<
77 toUnsigned(r) <<
';' <<
78 toUnsigned(g) <<
';' <<
93 static void forceColor(
bool force) { m_forceColor = force; }
104 std::ostream &
os() {
return m_os; }
121 static unsigned toUnsigned(
const T &val) {
return val; }
122 static unsigned outIndex(
const std::ostream &
os) {
123 if (
os.rdbuf() == std::cout.rdbuf())
125 if (
os.rdbuf() == std::cerr.rdbuf())
129 static bool doColor(
const std::ostream &
os) {
131 return m_forceColorValue;
132 auto idx = outIndex(
os);
133 if (m_doColor[idx] < 0)
134 m_doColor[idx] = isatty(idx + 1);
135 return m_doColor[idx];
138 friend void testColor();
139 inline static std::string_view seqBegin =
"\033[01;";
140 inline static std::string_view seqEnd =
"\033[0m";
141 inline static signed char m_doColor[] = { -1, -1, 1 };
142 inline static bool m_forceColor =
false;
143 inline static bool m_forceColorValue =
false;
151 return std::move(os);
158 return std::move(os);
Colorized output.
Definition Color.h:18
Color(std::ostream &os, enum C c=DEFAULT) noexcept
New Color stream (into os) using color c.
Definition Color.h:51
Color(enum C c=DEFAULT) noexcept
New Color stream (stdout) using color c.
Definition Color.h:44
Ctrl
Controls for Color.
Definition Color.h:35
static void forceColor(bool force)
Force color value (see forceColorValue()).
Definition Color.h:93
void ctrl(Ctrl ctrl)
Controls the stream behavior.
Definition Color.h:110
Color(unsigned char r, unsigned char g, unsigned char b) noexcept
New Color stream (stdout) using an RGB color.
Definition Color.h:62
static void forceColorValue(bool value)
If forceColor(true) was called, use of colors is bound to value.
Definition Color.h:98
C
Predefined colors.
Definition Color.h:21
std::ostream & os()
Return current output stream.
Definition Color.h:104
Color(std::ostream &os, unsigned char r, unsigned char g, unsigned char b) noexcept
New Color stream (into os) using an RGB color.
Definition Color.h:72