-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolors.c
369 lines (338 loc) · 11.2 KB
/
colors.c
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
#include <stdio.h>
#include "colors.h"
/* Immutable definitions for simplifying the transport of color information
between library functions.
*/
static int __COLOR_COUNT__ = ANSI_COLOR_COUNT;
static const char *__COLOR_HR_RG__[] = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "reset" };
static const char *__COLOR_HR_BR__[] = { "br_black", "br_red", "br_green", "br_yellow", "br_blue", "br_magenta", "br_cyan", "br_white", "reset" };
/* Returns the index of the given color in the global color index array. Will
return -1 if the color name is invalid
*/
int __FIND_COLOR_INDEX__(const char *color) {
for(int i = 0; i < __COLOR_COUNT__; ++i) {
if(!strcmp(color, __COLOR_HR_RG__[i])) {
return i;
}
}
return -1;
}
/* Returns the index of the given bright color in the global color index array.
Will return -1 if the color name is invalid
*/
int __FIND_BRIGHT_COLOR_INDEX__(const char *color) {
for(int i = 0; i < __COLOR_COUNT__; ++i) {
if(!strcmp(color, __COLOR_HR_BR__[i])) {
return i;
}
}
return -1;
}
/* Probably would be just as well done with a static array, but I was really
tied to the idea of using a struct for everything and I like how it turned
out.
*/
const char* __GET_CONST_COLOR_CODE__(const char *color, int isFG) {
int colorIndex;
if((colorIndex = __FIND_COLOR_INDEX__(color)) != -1) {
if(isFG) {
switch(colorIndex) {
case 0:
return Color_t.fg.black;
case 1:
return Color_t.fg.red;
case 2:
return Color_t.fg.green;
case 3:
return Color_t.fg.yellow;
case 4:
return Color_t.fg.blue;
case 5:
return Color_t.fg.magenta;
case 6:
return Color_t.fg.cyan;
case 7:
return Color_t.fg.white;
case 8:
return Color_t.fg.reset;
}
} else {
switch(colorIndex) {
case 0:
return Color_t.bg.black;
case 1:
return Color_t.bg.red;
case 2:
return Color_t.bg.green;
case 3:
return Color_t.bg.yellow;
case 4:
return Color_t.bg.blue;
case 5:
return Color_t.bg.magenta;
case 6:
return Color_t.bg.cyan;
case 7:
return Color_t.bg.white;
case 8:
return Color_t.bg.reset;
}
}
} else if((colorIndex = __FIND_BRIGHT_COLOR_INDEX__(color)) != -1) {
if(isFG) {
switch(colorIndex) {
case 0:
return Color_t.fg.br_black;
case 1:
return Color_t.fg.br_red;
case 2:
return Color_t.fg.br_green;
case 3:
return Color_t.fg.br_yellow;
case 4:
return Color_t.fg.br_blue;
case 5:
return Color_t.fg.br_magenta;
case 6:
return Color_t.fg.br_cyan;
case 7:
return Color_t.fg.br_white;
case 8:
return Color_t.fg.reset;
}
} else {
switch(colorIndex) {
case 0:
return Color_t.bg.br_black;
case 1:
return Color_t.bg.br_red;
case 2:
return Color_t.bg.br_green;
case 3:
return Color_t.bg.br_yellow;
case 4:
return Color_t.bg.br_blue;
case 5:
return Color_t.bg.br_magenta;
case 6:
return Color_t.bg.br_cyan;
case 7:
return Color_t.bg.br_white;
case 8:
return Color_t.bg.reset;
}
}
}
return NULL;
}
/* Composes and returns a ColorCode struct with either a foreground or
background color code defined, as specified by the "isFG" parameter, or
NOCOLOR if it does not exist.
*/
ColorCode parseColorCode(const char *color, int isFG) {
const char *CC = __GET_CONST_COLOR_CODE__(color, isFG);
if(!CC) {
return NOCOLOR;
}
ColorCode cc;
char *template = "\x1b[";
int CCSize = strlen(CC), templateSize = strlen(template);
if(isFG) {
strncpy(cc.fg, color, 20);
strncpy(cc.bg, "\0", 20);
} else {
strncpy(cc.bg, color, 20);
strncpy(cc.fg, "\0", 20);
}
char *buffer = cc.code;
int index = templateSize;
strncpy(buffer, template, templateSize);
strncpy(buffer + index, CC, CCSize);
index += CCSize;
buffer[index++] = 'm';
buffer[index] = '\0';
return cc;
}
/* Composes and returns a ColorCode struct with a background color defined, or
NOCOLOR if it does not exist.
*/
ColorCode parseBackgroundColorCode(const char *color) {
return parseColorCode(color, 0);
}
/* Composes and returns a ColorCode struct with a foreground color defined, or
NOCOLOR if it does not exist.
*/
ColorCode parseForegroundColorCode(const char *color) {
return parseColorCode(color, 1);
}
/* Composes and returns a ColorCode struct with both a foreground and
background color code defined, or NOCOLOR if either does not exist.
*/
ColorCode parseColorCodes(const char *foreground, const char *background) {
const char *fgcc = __GET_CONST_COLOR_CODE__(foreground, 1);
const char *bgcc = __GET_CONST_COLOR_CODE__(background, 0);
if(!fgcc || !bgcc) {
return NOCOLOR;
}
ColorCode cc;
char *template = "\x1b[";
int fgccSize = strlen(fgcc), bgccSize = strlen(bgcc),
templateSize = strlen(template);
strncpy(cc.fg, foreground, 20);
strncpy(cc.bg, background, 20);
char *buffer = cc.code;
int index = templateSize;
strncpy(buffer, template, templateSize);
strncpy(buffer + index, fgcc, fgccSize);
index += fgccSize;
buffer[index++] = ';';
strncpy(buffer + index, bgcc, bgccSize);
index += bgccSize;
buffer[index++] = 'm';
buffer[index] = '\0';
return cc;
}
/* Counts the number of color code capture groups contained within a character
String. Capture groupd begin with a "&(" and end with a ")&", and capture
groups may not contain other capture groups. Everything within a capture
group is considered to be a color code. Will parse until a null terminator
is encountered.
*/
int countColorCodes(const char *format) {
int count = 0;
for(int i = 0; *format != '\0'; ++i) {
// The beginning of a capture group has been found
if(*format == '&' && *(format + 1) == '(') {
while(*format != '\0') {
// The end of the capture group has been found
if(*format == ')' && *(format + 1) == '&') {
++count;
break;
} else {
++format;
}
}
} else {
++format;
}
}
return count;
}
/* Returns the end index of the next capture group in the given String,
starting from the start index given.
*/
int nextColorCodeEnd(const char *format, int start) {
format += start;
for(int i = start; *format != '\0'; ++i) {
if(*format == ')' && *(format + 1) == '&') {
return ++i;
} else {
++format;
}
}
return -1;
}
/* Returns the start index of the next capture group in the given String,
starting from the start index given.
*/
int nextColorCodeStart(const char *format, int start) {
format += start;
for(int i = start; *format != '\0'; ++i) {
if(*format == '&' && *(format + 1) == '(') {
// while(*format != '\0') {
// if(*format == ')' && *(format + 1) == '&') {
// return i;
// } else {
// ++format;
// }
// }
if(nextColorCodeEnd(format, 0) != -1) {
return i;
// makes sure that the capture group actually ends
} else {
++format;
}
} else {
++format;
}
}
return -1;
}
/* Parse and return the color of the next capture group in the String. Does not
advance the character pointer or manipulate the String.
*/
ColorCode getColorFromCapture(const char *cursor) {
cursor += 2;
int i = nextColorCodeEnd(cursor, 0);
char buffer[i];
strncpy(buffer, cursor, i - 1);
buffer[i - 1] = '\0';
if(*buffer == '_') {
return parseBackgroundColorCode(buffer + 1);
} else {
return parseForegroundColorCode(buffer);
}
}
/* Variadic color fprintf function with color parsing. Accepts a String with 0
or more embedded color capture groups, and a variadic argument list. Expects
the VA list to have been initialized with va_start, and will *not* close the
list upon completion.
*/
int cvfprintf(FILE *stream, const char *format, va_list args) {
int s = nextColorCodeStart(format, 0);
if(s != -1) {
const char *cursor = format;
int index = 0;
// All the color codes are shorter than their captures
char buffer[strlen(format) + 10];
// making room for the reset at the end
while(s != -1) {
if(s) {
strncpy(buffer + index, cursor, s);
index += s;
cursor += s;
}
ColorCode c = getColorFromCapture(cursor);
strncpy(buffer + index, c.code, strlen(c.code));
cursor += nextColorCodeEnd(cursor, 0) + 1;
s = nextColorCodeStart(cursor, 0);
index += strlen(c.code);
}
int remaining = strlen(cursor);
if(remaining) {
strncpy(buffer + index, cursor, remaining);
index += remaining;
// Place the final newline after the reset code
if(cursor[remaining - 1] == '\n') {
--index;
remaining = -1;
}
}
strncpy(buffer + index, Color_t.reset, strlen(Color_t.reset));
index += strlen(Color_t.reset);
if(remaining == -1) {
buffer[index++] = '\n';
}
buffer[index] = '\0';
return vfprintf(stream, buffer, args);
}
return vfprintf(stream, format, args);
}
/* Color fprintf function with color parsing. Accepts variable arguments.
*/
int cfprintf(FILE *stream, const char *format, ...) {
va_list args;
va_start(args, format);
int i = cvfprintf(stream, format, args);
va_end(args);
return i;
}
/* Color printf function with color parsing. Accepts variable arguments.
*/
int cprintf(const char *format, ...) {
va_list args;
va_start(args, format);
int i = cvfprintf(stdout, format, args);
va_end(args);
return i;
}