2024-01-31 02:02:32 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// color str in place
|
|
|
|
void color_text(char str[], const char *color) {
|
|
|
|
int size = snprintf(NULL, 0, "\x1b[38;2;%sm%s\x1b[0m", color, str) + 1; // calculate size that is needed for colored string
|
|
|
|
if (size < 0) {
|
|
|
|
fprintf(stderr, "rush: snprintf failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
char *buf = malloc(size);
|
|
|
|
if (buf == NULL) {
|
2024-02-01 20:02:30 +01:00
|
|
|
fprintf(stderr, "rush: Error allocating memory\n");
|
2024-01-31 02:02:32 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, size, "\x1b[38;2;%sm%s\x1b[0m", color, str); // format string to buf
|
|
|
|
strcpy(str, buf);
|
|
|
|
free(buf);
|
|
|
|
}
|