1
- #include " door.h"
2
1
#include < string>
3
2
3
+ #include " door.h"
4
+
4
5
/* *
5
6
* @file
6
7
* @brief ANSIColor
@@ -13,9 +14,7 @@ namespace door {
13
14
* with sensible defaults (White on Black).
14
15
*
15
16
*/
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 ) {}
19
18
20
19
/* *
21
20
* Construct a new ANSIColor::ANSIColor object
@@ -111,18 +110,18 @@ ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
111
110
*/
112
111
ANSIColor &ANSIColor::Attr (ATTR a) {
113
112
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 ;
126
125
}
127
126
return *this ;
128
127
}
@@ -136,8 +135,9 @@ ANSIColor &ANSIColor::Attr(ATTR a) {
136
135
* @return bool
137
136
*/
138
137
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)))));
141
141
}
142
142
143
143
/* *
@@ -149,8 +149,9 @@ bool ANSIColor::operator==(const ANSIColor &c) const {
149
149
* @return bool
150
150
*/
151
151
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)))));
154
155
}
155
156
156
157
/* *
@@ -160,10 +161,7 @@ bool ANSIColor::operator!=(const ANSIColor &c) const {
160
161
*/
161
162
void ANSIColor::setFg (COLOR f) {
162
163
fg = f;
163
- reset = 0 ;
164
- bold = 0 ;
165
- blink = 0 ;
166
- inverse = 0 ;
164
+ attr = 0 ;
167
165
}
168
166
169
167
/* *
@@ -174,7 +172,7 @@ void ANSIColor::setFg(COLOR f) {
174
172
*/
175
173
void ANSIColor::setFg (COLOR f, ATTR a) {
176
174
fg = f;
177
- attr (a);
175
+ setAttr (a);
178
176
}
179
177
180
178
/* *
@@ -191,12 +189,9 @@ void ANSIColor::setBg(COLOR b) { bg = b; }
191
189
*
192
190
* @param[in] a ATTR
193
191
*/
194
- void ANSIColor::attr (ATTR a) {
192
+ void ANSIColor::setAttr (ATTR a) {
195
193
// first, clear all attributes
196
- reset = 0 ;
197
- bold = 0 ;
198
- blink = 0 ;
199
- inverse = 0 ;
194
+ attr = 0 ;
200
195
Attr (a);
201
196
}
202
197
@@ -208,29 +203,30 @@ std::string ANSIColor::output(void) const {
208
203
std::string clr (CSI);
209
204
210
205
// 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)) {
212
208
clr += " 0m" ;
213
209
return clr;
214
210
}
215
211
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)) {
217
214
clr += " 0m" ;
218
215
return clr;
219
216
}
220
217
221
- if (reset ) {
218
+ if ((attr & ATTR_RESET) == ATTR_RESET ) {
222
219
clr += " 0;" ;
223
220
}
224
221
225
- if (bold ) {
226
- if (blink ) {
222
+ if ((attr & ATTR_BOLD) == ATTR_BOLD ) {
223
+ if ((attr & ATTR_BLINK) == ATTR_BLINK ) {
227
224
clr += " 5;" ;
228
225
}
229
226
clr += " 1;" ;
230
227
} 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) {
234
230
clr += " 5;" ;
235
231
}
236
232
}
@@ -253,9 +249,9 @@ std::string ANSIColor::debug(void) {
253
249
output += " , BG" ;
254
250
output += std::to_string ((int )bg);
255
251
output += " , B" ;
256
- output += std::to_string (bold );
252
+ output += std::to_string ((attr & ATTR_BOLD) == ATTR_BOLD );
257
253
output += " , R" ;
258
- output += std::to_string (reset );
254
+ output += std::to_string ((attr & ATTR_RESET) == ATTR_RESET );
259
255
260
256
return output;
261
257
}
@@ -272,27 +268,29 @@ std::string ANSIColor::output(ANSIColor &previous) const {
272
268
// color output optimization
273
269
274
270
// 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)) {
276
273
clr += " 0m" ;
277
274
previous = *this ;
278
- previous.reset = 0 ;
275
+ previous.attr &= ~ATTR_RESET; // reset = 0;
279
276
return clr;
280
277
}
281
278
282
279
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))) {
284
283
temp_reset = true ;
285
284
}
286
285
287
- if ((reset ) or (temp_reset)) {
286
+ if (((attr & ATTR_RESET) == ATTR_RESET ) or (temp_reset)) {
288
287
// current has RESET, so default to always sending colors.
289
288
if (temp_reset) {
290
289
clr += " 0m" ;
291
290
}
292
291
293
292
// 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 ();
296
294
clr += output ();
297
295
298
296
/*
@@ -303,7 +301,7 @@ std::string ANSIColor::output(ANSIColor &previous) const {
303
301
*/
304
302
305
303
previous = *this ;
306
- previous.reset = 0 ;
304
+ previous.attr &= ~ATTR_RESET ;
307
305
return clr;
308
306
}
309
307
@@ -314,38 +312,33 @@ std::string ANSIColor::output(ANSIColor &previous) const {
314
312
315
313
// resume "optimization"
316
314
317
- if (bold != previous.bold ) {
315
+ if (((attr & ATTR_BOLD) == ATTR_BOLD) !=
316
+ ((previous.attr & ATTR_BOLD) == ATTR_BOLD)) {
318
317
// 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 ) {
321
320
clr += " 5;" ;
322
321
}
323
322
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 ) + " ;" ;
328
325
} else {
329
326
clr += " 0;" ;
330
- if (blink ) {
327
+ if ((attr & ATTR_BLINK) == ATTR_BLINK ) {
331
328
clr += " 5;" ;
332
329
}
333
330
334
331
// 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 ) + " ;" ;
339
334
}
340
335
} else {
341
336
// not bold.
342
- if (blink ) {
337
+ if ((attr & ATTR_BLINK) == ATTR_BLINK ) {
343
338
clr += " 5;" ;
344
339
}
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 ) + " ;" ;
349
342
};
350
343
351
344
// Remove ';' if last character
@@ -376,8 +369,7 @@ std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
376
369
d->track = false ;
377
370
out = c.output (d->previous );
378
371
// if (!out.empty())
379
- if (out.compare (" \x1b [" ) == 0 )
380
- std::abort ();
372
+ if (out.compare (" \x1b [" ) == 0 ) std::abort ();
381
373
382
374
*d << out;
383
375
d->track = true ;
@@ -392,4 +384,4 @@ std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
392
384
393
385
ANSIColor reset (ATTR::RESET);
394
386
395
- } // namespace door
387
+ } // namespace door
0 commit comments