-
Notifications
You must be signed in to change notification settings - Fork 0
/
colr_join_example.c
76 lines (71 loc) · 2.26 KB
/
colr_join_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
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
#include "colr.h"
int main(void) {
/*
You can join things by a plain string or a colorized string.
For the pieces, the order/number of arguments does not matter.
colr_join() accepts ColorArgs, ColorResults, ColorTexts, and strings (char*).
*/
char* colorized = colr_join(
"\n",
"This is a plain line.",
Colr("This one is some kind of purple.", fore(rgb(125, 0, 155))),
Colr("This one is bright.", style(BRIGHT)),
"Another plain one, why not?"
);
if (!colorized) return 1;
// Prints each colorized piece of text on it's own line:
printf("%s\n", colorized);
free(colorized);
/*
The joiner can be a ColorText, string, or ColorArg (though ColorArgs
would be kinda useless).
*/
char* final = colr_join(
Colr(" <--> ", fore(ext_hex("#353535")), style(UNDERLINE)),
"This",
Colr(" that ", fore(RED)),
"the other."
);
if (!final) return 1;
// Prints each piece, joined by a colorized " <--> ".
printf("%s\n", final);
free(final);
/*
Nested joins can be achieved without leaking memory by using Colr_join().
It wraps it's results in a ColorResult, which the colr macros are safe
to `free()`.
*/
colr_puts(
Colr_join(
" ",
Colr_join(
Colr("warning", fore(YELLOW)),
"[",
"]"
),
Colr("This combination of calls should never leak.", fore(RED))
)
);
/*
Arrays of ColorText, ColorArgs, ColorResults, or strings can be used with
colr_join_array().
*/
char* joiner = " [and] ";
ColorText* words[] = {
Colr("this", fore(RED)),
Colr("that", fore(hex("ff3599"))),
Colr("the other", fore(BLUE), style(UNDERLINE)),
// The last member must be NULL.
NULL
};
char* s = colr_join_array(joiner, words);
if (!s) {
// Couldn't allocate memory for the final string.
for (size_t i = 0; words[i]; i++) colr_free(words[i]);
return 1;
}
printf("%s\n", s);
free(s);
// Don't forget to free your ColorResults/ColorTexts/ColorArgs.
for (size_t i = 0; words[i]; i++) colr_free(words[i]);
}