-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolr_cat_example.c
48 lines (40 loc) · 1.53 KB
/
colr_cat_example.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
#include "colr.h"
int main(void) {
/*
You can build your strings with colr_cat().
Using a Colr (ColorText), or sprinkling fore(), back(), and style() calls,
you can build multi-color strings and only worry about allocating/freeing
the text.
The order/number of arguments does not matter.
colr_cat() accepts ColorTexts, ColorArgs, and strings (char*).
*/
char *colorized = colr_cat(
"This is plain.\n",
Colr("This is styled.\n", fore(rgb(255, 0, 155))),
fore(RED),
"This was styled by the previous ColorArg.\n",
NC,
"This is normal because of the 'reset code' that came before it.\n",
// See the colr_join example for more about this:
Colr_join(Colr("This was joined", fore(RED)), "[", "]")
);
// Prints a colorized, joined, version of all the strings above.
printf("%s\n", colorized);
// Free the allocated result, no leaks.
free(colorized);
// Like I said before, if your text was allocated, you must free it.
char *allocated;
asprintf(&allocated, "\nThis is my string #%d\n", 1);
char *colored = colr_cat(
Colr(allocated, fore(ext(255)), style(UNDERLINE)),
"This one should not be free'd though.\n"
);
printf("%s", colored);
free(colored);
free(allocated);
/*
For throw-away/nested results that will be used in ColrC functions/macros,
you can use the Colr_cat variant.
*/
colr_puts(Colr_cat("No leaks: ", Colr("see", fore(RED)), "?"));
}