├── .gitignore ├── Makefile └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | gcc -Wall -Wextra -Ilib -Wpedantic -o ./build/main main.c; ./build/main; 4 | 5 | clean: 6 | rm -f $(OUT) 7 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const char *tclr_code_reset = "\x1b[0m"; 6 | 7 | const char *tclr_code_black = "\x1b[30m"; 8 | const char *tclr_code_red = "\x1b[31m"; 9 | const char *tclr_code_green = "\x1b[32m"; 10 | const char *tclr_code_yellow = "\x1b[33m"; 11 | const char *tclr_code_blue = "\x1b[34m"; 12 | const char *tclr_code_magenta = "\x1b[35m"; 13 | const char *tclr_code_cyan = "\x1b[36m"; 14 | const char *tclr_code_white = "\x1b[37m"; 15 | 16 | void tclr_color(const char *color_code, const char *message) { 17 | size_t start_len = strlen(color_code); 18 | size_t message_len = strlen(message); 19 | size_t end_len = strlen(tclr_code_reset); 20 | size_t total_len = start_len + message_len + end_len + 1; 21 | char *output = malloc(total_len); 22 | if (!output) { 23 | fprintf(stderr, "malloc failed in tclr_color\n"); 24 | exit(EXIT_FAILURE); 25 | } 26 | strcpy(output, color_code); 27 | strcat(output, message); 28 | strcat(output, tclr_code_reset); 29 | printf("%s", output); 30 | free(output); 31 | } 32 | 33 | void tclr_black(const char *msg) { tclr_color(tclr_code_black, msg); } 34 | void tclr_red(const char *msg) { tclr_color(tclr_code_red, msg); } 35 | void tclr_green(const char *msg) { tclr_color(tclr_code_green, msg); } 36 | void tclr_yellow(const char *msg) { tclr_color(tclr_code_yellow, msg); } 37 | void tclr_blue(const char *msg) { tclr_color(tclr_code_blue, msg); } 38 | void tclr_magenta(const char *msg) { tclr_color(tclr_code_magenta, msg); } 39 | void tclr_cyan(const char *msg) { tclr_color(tclr_code_cyan, msg); } 40 | void tclr_white(const char *msg) { tclr_color(tclr_code_white, msg); } 41 | 42 | int main(void) { 43 | tclr_black("black text\n"); 44 | tclr_red("red text\n"); 45 | tclr_green("green text\n"); 46 | tclr_yellow("yellow text\n"); 47 | tclr_blue("blue text\n"); 48 | tclr_magenta("magenta text\n"); 49 | tclr_cyan("cyan text\n"); 50 | tclr_white("white text\n"); 51 | return 0; 52 | } 53 | --------------------------------------------------------------------------------