Skip to content

Commit db8c05d

Browse files
committed
Added ANSIColor constexpr ctor.
1 parent e555be1 commit db8c05d

File tree

3 files changed

+236
-110
lines changed

3 files changed

+236
-110
lines changed

ansicolor.cpp

+59-67
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include "door.h"
21
#include <string>
32

3+
#include "door.h"
4+
45
/**
56
* @file
67
* @brief ANSIColor
@@ -13,9 +14,7 @@ namespace door {
1314
* with sensible defaults (White on Black).
1415
*
1516
*/
16-
ANSIColor::ANSIColor()
17-
: fg(COLOR::WHITE), bg(COLOR::BLACK), reset(0), bold(0), blink(0),
18-
inverse(0) {}
17+
ANSIColor::ANSIColor() : fg(COLOR::WHITE), bg(COLOR::BLACK), attr(0) {}
1918

2019
/**
2120
* Construct a new ANSIColor::ANSIColor object
@@ -111,18 +110,18 @@ ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
111110
*/
112111
ANSIColor &ANSIColor::Attr(ATTR a) {
113112
switch (a) {
114-
case ATTR::RESET:
115-
reset = 1;
116-
break;
117-
case ATTR::BOLD:
118-
bold = 1;
119-
break;
120-
case ATTR::BLINK:
121-
blink = 1;
122-
break;
123-
case ATTR::INVERSE:
124-
inverse = 1;
125-
break;
113+
case ATTR::RESET:
114+
attr |= ATTR_RESET;
115+
break;
116+
case ATTR::BOLD:
117+
attr |= ATTR_BOLD;
118+
break;
119+
case ATTR::BLINK:
120+
attr |= ATTR_BLINK;
121+
break;
122+
case ATTR::INVERSE:
123+
attr |= ATTR_INVERSE;
124+
break;
126125
}
127126
return *this;
128127
}
@@ -136,8 +135,9 @@ ANSIColor &ANSIColor::Attr(ATTR a) {
136135
* @return bool
137136
*/
138137
bool ANSIColor::operator==(const ANSIColor &c) const {
139-
return ((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
140-
(blink == c.blink) and (inverse == c.inverse));
138+
return ((fg == c.fg) and (bg == c.bg) and
139+
((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
140+
((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
141141
}
142142

143143
/**
@@ -149,8 +149,9 @@ bool ANSIColor::operator==(const ANSIColor &c) const {
149149
* @return bool
150150
*/
151151
bool ANSIColor::operator!=(const ANSIColor &c) const {
152-
return !((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
153-
(blink == c.blink) and (inverse == c.inverse));
152+
return !((fg == c.fg) and (bg == c.bg) and
153+
((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
154+
((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
154155
}
155156

156157
/**
@@ -160,10 +161,7 @@ bool ANSIColor::operator!=(const ANSIColor &c) const {
160161
*/
161162
void ANSIColor::setFg(COLOR f) {
162163
fg = f;
163-
reset = 0;
164-
bold = 0;
165-
blink = 0;
166-
inverse = 0;
164+
attr = 0;
167165
}
168166

169167
/**
@@ -174,7 +172,7 @@ void ANSIColor::setFg(COLOR f) {
174172
*/
175173
void ANSIColor::setFg(COLOR f, ATTR a) {
176174
fg = f;
177-
attr(a);
175+
setAttr(a);
178176
}
179177

180178
/**
@@ -191,12 +189,9 @@ void ANSIColor::setBg(COLOR b) { bg = b; }
191189
*
192190
* @param[in] a ATTR
193191
*/
194-
void ANSIColor::attr(ATTR a) {
192+
void ANSIColor::setAttr(ATTR a) {
195193
// first, clear all attributes
196-
reset = 0;
197-
bold = 0;
198-
blink = 0;
199-
inverse = 0;
194+
attr = 0;
200195
Attr(a);
201196
}
202197

@@ -208,29 +203,30 @@ std::string ANSIColor::output(void) const {
208203
std::string clr(CSI);
209204

210205
// check for special cases
211-
if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
206+
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
207+
(bg == COLOR::BLACK)) {
212208
clr += "0m";
213209
return clr;
214210
}
215211

216-
if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
212+
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::WHITE) and
213+
(bg == COLOR::BLACK)) {
217214
clr += "0m";
218215
return clr;
219216
}
220217

221-
if (reset) {
218+
if ((attr & ATTR_RESET) == ATTR_RESET) {
222219
clr += "0;";
223220
}
224221

225-
if (bold) {
226-
if (blink) {
222+
if ((attr & ATTR_BOLD) == ATTR_BOLD) {
223+
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
227224
clr += "5;";
228225
}
229226
clr += "1;";
230227
} else {
231-
if (!reset)
232-
clr += "0;";
233-
if (blink) {
228+
if (!((attr & ATTR_RESET) == ATTR_RESET)) clr += "0;";
229+
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
234230
clr += "5;";
235231
}
236232
}
@@ -253,9 +249,9 @@ std::string ANSIColor::debug(void) {
253249
output += ", BG";
254250
output += std::to_string((int)bg);
255251
output += ", B";
256-
output += std::to_string(bold);
252+
output += std::to_string((attr & ATTR_BOLD) == ATTR_BOLD);
257253
output += ", R";
258-
output += std::to_string(reset);
254+
output += std::to_string((attr & ATTR_RESET) == ATTR_RESET);
259255

260256
return output;
261257
}
@@ -272,27 +268,29 @@ std::string ANSIColor::output(ANSIColor &previous) const {
272268
// color output optimization
273269

274270
// check for special cases
275-
if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
271+
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
272+
(bg == COLOR::BLACK)) {
276273
clr += "0m";
277274
previous = *this;
278-
previous.reset = 0;
275+
previous.attr &= ~ATTR_RESET; // reset = 0;
279276
return clr;
280277
}
281278

282279
bool temp_reset = false;
283-
if ((!blink) and (blink != previous.blink)) {
280+
if ((!((attr & ATTR_BLINK) == ATTR_BLINK)) and
281+
(((attr & ATTR_BLINK) == ATTR_BLINK) !=
282+
((previous.attr & ATTR_BLINK) == ATTR_BLINK))) {
284283
temp_reset = true;
285284
}
286285

287-
if ((reset) or (temp_reset)) {
286+
if (((attr & ATTR_RESET) == ATTR_RESET) or (temp_reset)) {
288287
// current has RESET, so default to always sending colors.
289288
if (temp_reset) {
290289
clr += "0m";
291290
}
292291

293292
// this fixes the extra \x1b that shows up with reset.
294-
if (clr.compare(CSI) == 0)
295-
clr.clear();
293+
if (clr.compare(CSI) == 0) clr.clear();
296294
clr += output();
297295

298296
/*
@@ -303,7 +301,7 @@ std::string ANSIColor::output(ANSIColor &previous) const {
303301
*/
304302

305303
previous = *this;
306-
previous.reset = 0;
304+
previous.attr &= ~ATTR_RESET;
307305
return clr;
308306
}
309307

@@ -314,38 +312,33 @@ std::string ANSIColor::output(ANSIColor &previous) const {
314312

315313
// resume "optimization"
316314

317-
if (bold != previous.bold) {
315+
if (((attr & ATTR_BOLD) == ATTR_BOLD) !=
316+
((previous.attr & ATTR_BOLD) == ATTR_BOLD)) {
318317
// not the same, so handle this.
319-
if (bold) {
320-
if (blink) {
318+
if ((attr & ATTR_BOLD) == ATTR_BOLD) {
319+
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
321320
clr += "5;";
322321
}
323322
clr += "1;";
324-
if (fg != previous.fg)
325-
clr += std::to_string((int)fg + 30) + ";";
326-
if (bg != previous.bg)
327-
clr += std::to_string((int)bg + 40) + ";";
323+
if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
324+
if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
328325
} else {
329326
clr += "0;";
330-
if (blink) {
327+
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
331328
clr += "5;";
332329
}
333330

334331
// RESET to turn OFF BOLD, clears previous
335-
if (fg != COLOR::WHITE)
336-
clr += std::to_string((int)fg + 30) + ";";
337-
if (bg != COLOR::BLACK)
338-
clr += std::to_string((int)bg + 40) + ";";
332+
if (fg != COLOR::WHITE) clr += std::to_string((int)fg + 30) + ";";
333+
if (bg != COLOR::BLACK) clr += std::to_string((int)bg + 40) + ";";
339334
}
340335
} else {
341336
// not bold.
342-
if (blink) {
337+
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
343338
clr += "5;";
344339
}
345-
if (fg != previous.fg)
346-
clr += std::to_string((int)fg + 30) + ";";
347-
if (bg != previous.bg)
348-
clr += std::to_string((int)bg + 40) + ";";
340+
if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
341+
if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
349342
};
350343

351344
// Remove ';' if last character
@@ -376,8 +369,7 @@ std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
376369
d->track = false;
377370
out = c.output(d->previous);
378371
// if (!out.empty())
379-
if (out.compare("\x1b[") == 0)
380-
std::abort();
372+
if (out.compare("\x1b[") == 0) std::abort();
381373

382374
*d << out;
383375
d->track = true;
@@ -392,4 +384,4 @@ std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
392384

393385
ANSIColor reset(ATTR::RESET);
394386

395-
} // namespace door
387+
} // namespace door

0 commit comments

Comments
 (0)