ColrC  0.4.0
An easy to use C library for linux terminal colors/escape-codes.
Colr_example.c
#include "colr.h"
int main(void) {
/*
Colr() is for styling one piece of text.
When combined with the colr_cat() macro it allows you to seperate colors/styles.
*/
char* colorized = colr_cat(
Colr("America ", fore(RED)),
Colr("the ", fore(WHITE)),
Colr("beautiful", fore(BLUE)),
".\n"
);
/*
All of the Colr, fore, back, and style resources were free'd by `colr`.
You are responsible for the text and the resulting colorized string.
*/
if (!colorized) return 1;
printf("%s", colorized);
free(colorized);
/*
You can justify ColorTexts, ColorResults, and strings.
*/
char* center = colr_center(Colr("test", fore(RED)), 40);
puts(center);
free(center);
// Using the Colr_* variants, you can nest them.
// The colr_center, colr_ljust, colr_rjust (and Colr_* variants) will
// accept strings, ColorTexts, or ColorResults.
// If you don't want to use spaces as the pad character, use the *_char variants.
// Notice that colr_puts() doesn't require you to free() the nested Colr* calls.
// As long as they are used inside a colr/Colr call, they are released
// in the next call to a colr/Colr function.
"\n",
Colr_ljust_char(Colr("this", fore(BLUE)), 20, '-'),
" ",
Colr("thing", fore(YELLOW)),
Colr("out", style(BOLD))
),
20
),
Colr_center("okay?", 20)
)
);
/*
There are some justification macros that make it easy to create
ColorText's with center, left, or right-justified text.
*/
char* just = colr_cat(
ColorText_center("This is centered.", 80, fore("lightblue")),
"\n",
ColorText_ljust("This is on the left.", 38, fore(ext_hex("ff2525"))),
"----",
ColorText_rjust("This is on the right.", 38, fore(ext_rgb(255, 53, 125)))
);
if (!colorized) return 1;
printf("%s\n", just);
free(just);
/*
If you don't need several Colr() calls, there is a shortcut for safely
creating colorized text using colr().
*/
char* fast = colr(
"Hello from ColrC.",
fore("#2500FF"),
back(ext_hex("#353535")),
style(UNDERLINE)
);
if (!fast) return 1;
printf("%s\n", fast);
free(fast);
}