90s

Minimalist, customizable shell written in C99 with syntax highlighting
git clone https://codeberg.org/night0721/90s
Log | Files | Refs | README | LICENSE

color.c (520B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 #include "90s.h"
      6 
      7 // color str in place
      8 char *color_text(char *str, const char *color) {
      9     int size = snprintf(NULL, 0, "\x1b[38;2;%sm%s\x1b[0m", color, str) + 1; // calculate size that is needed for colored string
     10     if (size < 0) {
     11         fprintf(stderr, "90s: snprintf failed\n");
     12         exit(EXIT_FAILURE);
     13     }
     14     char *buf = memalloc(size);
     15 
     16     snprintf(buf, size, "\x1b[38;2;%sm%s\x1b[0m", color, str); // format string to buf
     17     return buf;
     18 }