-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathansicolor.cpp
387 lines (341 loc) · 8.33 KB
/
ansicolor.cpp
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
#include <string>
#include "door.h"
/**
* @file
* @brief ANSIColor
*/
namespace door {
/**
* Construct a new ANSIColor::ANSIColor object
* with sensible defaults (White on Black).
*
*/
ANSIColor::ANSIColor() : fg(COLOR::WHITE), bg(COLOR::BLACK), attr(0) {}
/**
* Construct a new ANSIColor::ANSIColor object
* with attribute set.
*
* @param[in] a ATTR
*/
ANSIColor::ANSIColor(ATTR a) : ANSIColor() { Attr(a); }
/**
* Construct a new ANSIColor::ANSIColor object
* with a foreground color.
*
* @param[in] f COLOR
*/
ANSIColor::ANSIColor(COLOR f) : ANSIColor() { fg = f; }
/**
* Construct a new ANSIColor::ANSIColor object
* with a foreground color and attribute.
*
* @param[in] f COLOR
* @param[in] a ATTR
*/
ANSIColor::ANSIColor(COLOR f, ATTR a) : ANSIColor() {
fg = f;
Attr(a);
}
/**
* Construct a new ANSIColor::ANSIColor object
* with a foreground color and attributes.
*
* @param[in] f COLOR
* @param[in] a1 ATTR
* @param[in] a2 ATTR
*/
ANSIColor::ANSIColor(COLOR f, ATTR a1, ATTR a2) : ANSIColor() {
fg = f;
Attr(a1);
Attr(a2);
}
/**
* Construct a new ANSIColor::ANSIColor object
* with a foreground and background color.
*
* @param[in] f foreground COLOR
* @param[in] b background COLOR
*/
ANSIColor::ANSIColor(COLOR f, COLOR b) : ANSIColor() {
fg = f;
bg = b;
}
/**
* Construct a new ANSIColor::ANSIColor object
* with a foreground color, background color,
* and attribute.
*
* @param[in] f foreground COLOR
* @param[in] b background COLOR
* @param[in] a ATTR
*/
ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a) : ANSIColor() {
fg = f;
bg = b;
Attr(a);
}
/**
* Construct a new ANSIColor::ANSIColor object
* with foreground, background color and attributes.
*
* @param[in] f foreground COLOR
* @param[in] b background COLOR
* @param[in] a1 ATTR
* @param[in] a2 ATTR
*/
ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
fg = f;
bg = b;
Attr(a1);
Attr(a2);
}
/**
* Set attribute. We return the object so
* calls can be chained.
*
* @param[in] a ATTR
* @return ANSIColor&
*/
ANSIColor &ANSIColor::Attr(ATTR a) {
switch (a) {
case ATTR::RESET:
attr |= ATTR_RESET;
break;
case ATTR::BOLD:
attr |= ATTR_BOLD;
break;
case ATTR::BLINK:
attr |= ATTR_BLINK;
break;
case ATTR::INVERSE:
attr |= ATTR_INVERSE;
break;
}
return *this;
}
/**
* Equal operator.
*
* This compares colors and attributes, but ignores reset.
*
* @param[in] c const ANSIColor &
* @return bool
*/
bool ANSIColor::operator==(const ANSIColor &c) const {
return ((fg == c.fg) and (bg == c.bg) and
((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
}
/**
* Not-equal operator.
*
* This compares colors and attributes, but ignores reset.
*
* @param[in] c const ANSIColor &
* @return bool
*/
bool ANSIColor::operator!=(const ANSIColor &c) const {
return !((fg == c.fg) and (bg == c.bg) and
((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
}
/**
* @brief Set foreground color
*
* @param[in] f foreground COLOR
*/
void ANSIColor::setFg(COLOR f) {
fg = f;
attr = 0;
}
/**
* @brief Set foreground color and attribute
*
* @param[in] f foreground COLOR
* @param[in] a ATTR
*/
void ANSIColor::setFg(COLOR f, ATTR a) {
fg = f;
setAttr(a);
}
/**
* @brief Set background color
*
* @param[in] b background COLOR
*/
void ANSIColor::setBg(COLOR b) { bg = b; }
/**
* @brief Set attribute
*
* This clears all the attributes before setting the selected ATTR.
*
* @param[in] a ATTR
*/
void ANSIColor::setAttr(ATTR a) {
// first, clear all attributes
attr = 0;
Attr(a);
}
/**
* Output the full ANSI codes for attributes and color.
* This does not look at the previous values.
*/
std::string ANSIColor::output(void) const {
std::string clr(CSI);
// check for special cases
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
(bg == COLOR::BLACK)) {
clr += "0m";
return clr;
}
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::WHITE) and
(bg == COLOR::BLACK)) {
clr += "0m";
return clr;
}
if ((attr & ATTR_RESET) == ATTR_RESET) {
clr += "0;";
}
if ((attr & ATTR_BOLD) == ATTR_BOLD) {
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
clr += "5;";
}
clr += "1;";
} else {
if (!((attr & ATTR_RESET) == ATTR_RESET)) clr += "0;";
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
clr += "5;";
}
}
clr += std::to_string(30 + (int)fg) + ";";
clr += std::to_string(40 + (int)bg) + "m";
return clr;
}
/**
* @brief Output debug string for ANSIColor
*
* @return std::string
*/
std::string ANSIColor::debug(void) {
std::string output;
output = "ANSIColor FG";
output += std::to_string((int)fg);
output += ", BG";
output += std::to_string((int)bg);
output += ", B";
output += std::to_string((attr & ATTR_BOLD) == ATTR_BOLD);
output += ", R";
output += std::to_string((attr & ATTR_RESET) == ATTR_RESET);
return output;
}
/**
* Output only what ANSI attributes and colors have changed.
* This uses the previous ANSIColor value to determine what
* has changed.
*
* This sets previous to the current upon completion.
*/
std::string ANSIColor::output(ANSIColor &previous) const {
std::string clr(CSI);
// color output optimization
// check for special cases
if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
(bg == COLOR::BLACK)) {
clr += "0m";
previous = *this;
previous.attr &= ~ATTR_RESET; // reset = 0;
return clr;
}
bool temp_reset = false;
if ((!((attr & ATTR_BLINK) == ATTR_BLINK)) and
(((attr & ATTR_BLINK) == ATTR_BLINK) !=
((previous.attr & ATTR_BLINK) == ATTR_BLINK))) {
temp_reset = true;
}
if (((attr & ATTR_RESET) == ATTR_RESET) or (temp_reset)) {
// current has RESET, so default to always sending colors.
if (temp_reset) {
clr += "0m";
}
// this fixes the extra \x1b that shows up with reset.
if (clr.compare(CSI) == 0) clr.clear();
clr += output();
/*
std::ofstream logf;
logf.open("ansicolor.log", std::ofstream::out | std::ofstream::app);
logf << "clr = [" << clr << "]" << std::endl;
logf.close();
*/
previous = *this;
previous.attr &= ~ATTR_RESET;
return clr;
}
if (*this == previous) {
clr.clear();
return clr;
}
// resume "optimization"
if (((attr & ATTR_BOLD) == ATTR_BOLD) !=
((previous.attr & ATTR_BOLD) == ATTR_BOLD)) {
// not the same, so handle this.
if ((attr & ATTR_BOLD) == ATTR_BOLD) {
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
clr += "5;";
}
clr += "1;";
if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
} else {
clr += "0;";
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
clr += "5;";
}
// RESET to turn OFF BOLD, clears previous
if (fg != COLOR::WHITE) clr += std::to_string((int)fg + 30) + ";";
if (bg != COLOR::BLACK) clr += std::to_string((int)bg + 40) + ";";
}
} else {
// not bold.
if ((attr & ATTR_BLINK) == ATTR_BLINK) {
clr += "5;";
}
if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
};
// Remove ';' if last character
std::string::iterator si = clr.end() - 1;
if (*si == ';') {
clr.erase(si);
}
if (clr.compare(CSI) == 0)
clr.clear();
else
clr += "m";
// final step, set previous to current and return the string;
previous = *this;
return clr;
};
/**
* This converts ANSI \ref COLOR and \ref ATTR to ANSI codes
* understood by the \ref Door output class.
*/
std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
std::string out;
Door *d = dynamic_cast<Door *>(&os);
if (d != nullptr) {
d->track = false;
out = c.output(d->previous);
// if (!out.empty())
if (out.compare("\x1b[") == 0) std::abort();
*d << out;
d->track = true;
} else {
// "dumb" version that can't remember anything/ doesn't optimize color
// output.
std::string out = c.output();
os << out;
}
return os;
}
ANSIColor reset(ATTR::RESET);
} // namespace door