-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcprintf.c
373 lines (373 loc) · 10.3 KB
/
cprintf.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
370
371
372
373
// SPDX-License-Identifier: MIT
/*
*
* This file is part of cprintf, with ABSOLUTELY NO WARRANTY.
*
* MIT License
*
* Copyright (c) 2024 Moe-hacker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
#include "include/cprintf.h"
#if __STDC_VERSION__ < 202000L
#define bool _Bool
#define true ((_Bool) + 1u)
#define false ((_Bool) + 0u)
#endif
char *cprintf_base_color = "254;228;208";
static void print_rgb_fg_color(const char *_Nonnull color)
{
/*
* print \033[1;38;2;R;G;Bm format color.
*/
char buf[17];
for (size_t i = 1; i < strlen(color) - 1; i++) {
buf[i - 1] = color[i];
buf[i] = 0;
}
printf("\033[38;2;%sm", buf);
}
static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull color)
{
/*
* print \033[1;38;2;R;G;Bm format color.
*/
char buf[17];
for (size_t i = 1; i < strlen(color) - 1; i++) {
buf[i - 1] = color[i];
buf[i] = 0;
}
fprintf(stream, "\033[38;2;%sm", buf);
}
static void print_rgb_bg_color(const char *_Nonnull color)
{
/*
* print \033[1;38;2;R;G;Bm format color.
*/
char buf[17];
for (size_t i = 1; i < strlen(color) - 1; i++) {
buf[i - 1] = color[i];
buf[i] = 0;
}
printf("\033[48;2;%sm", buf);
}
static void fprint_rgb_bg_color(FILE *_Nonnull stream, const char *_Nonnull color)
{
/*
* print \033[1;38;2;R;G;Bm format color.
*/
char buf[17];
for (size_t i = 1; i < strlen(color) - 1; i++) {
buf[i - 1] = color[i];
buf[i] = 0;
}
fprintf(stream, "\033[48;2;%sm", buf);
}
static bool is_rgb_color(const char *_Nonnull color)
{
/*
* Check if color is an R;G;B format color.
*/
int sem = 0;
// If R > 255, it's not a color.
if (atoi(color) > 255) {
return false;
}
for (size_t i = 1; i < strlen(color) - 1; i++) {
if (color[i] == ';') {
sem++;
// If G or B > 255, it's not a color.
if (atoi(&color[i + 1]) > 255) {
return false;
}
}
// If there are more than 2 `;`, the format is not correct.
if (sem > 2) {
return false;
}
// If the color include other charactor, the format is not correct.
if (!isdigit(color[i]) && color[i] != ';') {
return false;
}
}
// If there are not 2 `;`, the format is not correct.
if (sem != 2) {
return false;
}
return true;
}
static const char *cfprintf_print_fg_color(FILE *_Nonnull stream, const char *_Nonnull buf)
{
/*
* Only valid {color} will be recognized,
* and for other '{' without 'color}', we print a '{'.
* we return the pointer to the last character that is
* not recognized as color.
*/
const char *ret = buf;
char color[17] = { '\0' };
for (int i = 0; i < 16; i++) {
if (buf[i] == '}') {
color[i] = buf[i];
color[i + 1] = 0;
ret = &(buf[i]);
break;
}
color[i] = buf[i];
color[i + 1] = 0;
}
if (strcmp(color, "{clear}") == 0) {
fprintf(stream, "\033[0m");
} else if (strcmp(color, "{black}") == 0) {
fprintf(stream, "\033[30m");
} else if (strcmp(color, "{red}") == 0) {
fprintf(stream, "\033[31m");
} else if (strcmp(color, "{green}") == 0) {
fprintf(stream, "\033[32m");
} else if (strcmp(color, "{yellow}") == 0) {
fprintf(stream, "\033[33m");
} else if (strcmp(color, "{blue}") == 0) {
fprintf(stream, "\033[34m");
} else if (strcmp(color, "{purple}") == 0) {
fprintf(stream, "\033[35m");
} else if (strcmp(color, "{cyan}") == 0) {
fprintf(stream, "\033[36m");
} else if (strcmp(color, "{white}") == 0) {
fprintf(stream, "\033[37m");
} else if (strcmp(color, "{base}") == 0) {
fprintf(stream, "\033[1;38;2;%sm", cprintf_base_color);
} else if (strcmp(color, "{underline}") == 0) {
fprintf(stream, "\033[4m");
} else if (strcmp(color, "{highlight}") == 0) {
fprintf(stream, "\033[1m");
} else if (is_rgb_color(color)) {
fprint_rgb_fg_color(stream, color);
} else {
ret = buf;
fprintf(stream, "{");
}
return ret;
}
static const char *cprintf_print_fg_color(const char *_Nonnull buf)
{
/*
* Only valid {color} will be recognized,
* and for other '{' without 'color}', we print a '{'.
* we return the pointer to the last character that is
* not recognized as color.
*/
const char *ret = buf;
char color[17] = { '\0' };
for (int i = 0; i < 16; i++) {
if (buf[i] == '}') {
color[i] = buf[i];
color[i + 1] = 0;
ret = &(buf[i]);
break;
}
color[i] = buf[i];
color[i + 1] = 0;
}
if (strcmp(color, "{clear}") == 0) {
printf("\033[0m");
} else if (strcmp(color, "{black}") == 0) {
printf("\033[30m");
} else if (strcmp(color, "{red}") == 0) {
printf("\033[31m");
} else if (strcmp(color, "{green}") == 0) {
printf("\033[32m");
} else if (strcmp(color, "{yellow}") == 0) {
printf("\033[33m");
} else if (strcmp(color, "{blue}") == 0) {
printf("\033[34m");
} else if (strcmp(color, "{purple}") == 0) {
printf("\033[35m");
} else if (strcmp(color, "{cyan}") == 0) {
printf("\033[36m");
} else if (strcmp(color, "{white}") == 0) {
printf("\033[37m");
} else if (strcmp(color, "{base}") == 0) {
printf("\033[1;38;2;%sm", cprintf_base_color);
} else if (strcmp(color, "{underline}") == 0) {
printf("\033[4m");
} else if (strcmp(color, "{highlight}") == 0) {
printf("\033[1m");
} else if (is_rgb_color(color)) {
print_rgb_fg_color(color);
} else {
ret = buf;
printf("{");
}
return ret;
}
static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_Nonnull buf)
{
/*
* Only valid [color] will be recognized,
* and for other '[' without 'color[', we print a '['.
* we return the pointer to the last character that is
* not recognized as color.
*/
const char *ret = buf;
char color[17] = { '\0' };
for (int i = 0; i < 16; i++) {
if (buf[i] == ']') {
color[i] = buf[i];
color[i + 1] = 0;
ret = &(buf[i]);
break;
}
color[i] = buf[i];
color[i + 1] = 0;
}
if (strcmp(color, "[clear]") == 0) {
fprintf(stream, "\033[0m");
} else if (strcmp(color, "[black]") == 0) {
fprintf(stream, "\033[40m");
} else if (strcmp(color, "[red]") == 0) {
fprintf(stream, "\033[41m");
} else if (strcmp(color, "[green]") == 0) {
fprintf(stream, "\033[42m");
} else if (strcmp(color, "[yellow]") == 0) {
fprintf(stream, "\033[43m");
} else if (strcmp(color, "[blue]") == 0) {
fprintf(stream, "\033[44m");
} else if (strcmp(color, "[purple]") == 0) {
fprintf(stream, "\033[45m");
} else if (strcmp(color, "[cyan]") == 0) {
fprintf(stream, "\033[46m");
} else if (strcmp(color, "[white]") == 0) {
fprintf(stream, "\033[47m");
} else if (strcmp(color, "[base]") == 0) {
fprintf(stream, "\033[1;48;2;%sm", cprintf_base_color);
} else if (strcmp(color, "[underline]") == 0) {
fprintf(stream, "\033[4m");
} else if (strcmp(color, "[highlight]") == 0) {
fprintf(stream, "\033[1m");
} else if (is_rgb_color(color)) {
fprint_rgb_bg_color(stream, color);
} else {
ret = buf;
fprintf(stream, "[");
}
return ret;
}
static const char *cprintf_print_bg_color(const char *_Nonnull buf)
{
/*
* Only valid [color] will be recognized,
* and for other '[' without 'color]', we print a '['.
* we return the pointer to the last character that is
* not recognized as color.
*/
const char *ret = buf;
char color[17] = { '\0' };
for (int i = 0; i < 16; i++) {
if (buf[i] == ']') {
color[i] = buf[i];
color[i + 1] = 0;
ret = &(buf[i]);
break;
}
color[i] = buf[i];
color[i + 1] = 0;
}
if (strcmp(color, "[clear]") == 0) {
printf("\033[0m");
} else if (strcmp(color, "[black]") == 0) {
printf("\033[40m");
} else if (strcmp(color, "[red]") == 0) {
printf("\033[41m");
} else if (strcmp(color, "[green]") == 0) {
printf("\033[42m");
} else if (strcmp(color, "[yellow]") == 0) {
printf("\033[43m");
} else if (strcmp(color, "[blue]") == 0) {
printf("\033[44m");
} else if (strcmp(color, "[purple]") == 0) {
printf("\033[45m");
} else if (strcmp(color, "[cyan]") == 0) {
printf("\033[46m");
} else if (strcmp(color, "[white]") == 0) {
printf("\033[47m");
} else if (strcmp(color, "[base]") == 0) {
printf("\033[1;48;2;%sm", cprintf_base_color);
} else if (strcmp(color, "[underline]") == 0) {
printf("\033[4m");
} else if (strcmp(color, "[highlight]") == 0) {
printf("\033[1m");
} else if (is_rgb_color(color)) {
print_rgb_bg_color(color);
} else {
ret = buf;
printf("[");
}
return ret;
}
void __cprintf(const char *_Nonnull buf)
{
const char *p = NULL;
p = buf;
for (size_t i = 0; i < strlen(buf); i++) {
// Search for '{'.
if (*p == '{') {
// *p will be moved because we need to skip the {color} string.
p = cprintf_print_fg_color(p);
} else if (*p == '[') {
// *p will be moved because we need to skip the [color] string.
p = cprintf_print_bg_color(p);
} else {
printf("%c", *p);
}
// Recompute the value of i.
i = (size_t)(p - buf);
// Goto the next charactor.
p = &(p[1]);
}
// We will always reset the color in the end.
printf("\033[0m");
fflush(stdout);
}
void __cfprintf(FILE *_Nonnull stream, const char *_Nonnull buf)
{
const char *p = NULL;
p = buf;
for (size_t i = 0; i < strlen(buf); i++) {
// Search for '{' or '['.
if (*p == '{') {
// *p will be moved because we need to skip the {color} string.
p = cfprintf_print_fg_color(stream, p);
} else if (*p == '[') {
// *p will be moved because we need to skip the {color} string.
p = cfprintf_print_bg_color(stream, p);
} else {
fprintf(stream, "%c", *p);
}
// Recompute the value of i.
i = (size_t)(p - buf);
// Goto the next charactor.
p = &(p[1]);
}
// We will always reset the color in the end.
fprintf(stream, "\033[0m");
fflush(stream);
}