ColrC  0.4.0
An easy to use C library for linux terminal colors/escape-codes.
colr_cat_example.c
#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);
/* Colr_cat:
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)), "?"));
/*
These can be deeply nested/mixed.
*/
// Building a rainbow message for flair in this example, preferably in RGB.
char* msg = "Colorize your program.";
ColorResult* rainbowmsg = ColrResult(
rainbow_fg(msg, 0.1, 3, 1) :
rainbow_fg_term(msg, 0.1, 3, 1)
);
// Use colr/Colr macros to build leak-free colorized strings.
"\n",
"\n",
Colr("ColrC", fore(BLUE), style(BOLD)),
40
),
"[",
" v. ",
Colr(COLR_VERSION, fore(CYAN), style(BOLD))
),
38
),
"]"
),
"[",
Colr_rjust(rainbowmsg, 38),
"]"
)
),
"\n"
)
);
}