90s/color.c

19 lines
522 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
2024-02-11 00:31:29 +01:00
char *color_text(char *str, const char *color) {
2024-01-31 02:02:32 +01:00
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
2024-02-11 00:31:29 +01:00
return buf;
2024-01-31 02:02:32 +01:00
}