90s/color.c

20 lines
543 B
C
Raw Normal View History

2024-01-31 02:02:32 +01:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "rush.h"
2024-01-31 02:02:32 +01:00
// 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 = memalloc(size);
2024-01-31 02:02:32 +01:00
snprintf(buf, size, "\x1b[38;2;%sm%s\x1b[0m", color, str); // format string to buf
strcpy(str, buf);
free(buf);
}