💾 Archived View for gmi.noulin.net › gitRepositories › printfRGB › file › printfRGB.c.gmi captured on 2023-07-10 at 18:02:44. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

printfRGB

Log

Files

Refs

README

LICENSE

printfRGB.c (1301B)

     1 #include <inttypes.h>
     2 #include <stdio.h>
     3 #include <printf.h>
     4 
     5 #define TERMRGB "\x1b[38;2;"
     6 #define BGTERMRGB "\x1b[48;2;"
     7 
     8 int print_k(FILE *stream, const struct printf_info *info, const void *const *args) {
     9   uint32_t rgbColor = *((uint32_t*) args[0]);
    10   char b[20];
    11   size_t len;
    12 
    13   snprintf(b, sizeof(b), TERMRGB "%d;%d;%dm", rgbColor>>16, (rgbColor&0xFF00)>>8, rgbColor&0xFF);
    14 
    15   /* Pad to the minimum field width and print to the stream. */
    16   len = fprintf(stream, "%*s", (info->left ? -info->width : info->width), b);
    17   return len;
    18 }
    19 
    20 int print_K(FILE *stream, const struct printf_info *info, const void *const *args) {
    21   uint32_t rgbColor = *((uint32_t*) args[0]);
    22   char b[20];
    23   size_t len;
    24 
    25   snprintf(b, sizeof(b), BGTERMRGB "%d;%d;%dm", rgbColor>>16, (rgbColor&0xFF00)>>8, rgbColor&0xFF);
    26 
    27   /* Pad to the minimum field width and print to the stream. */
    28   len = fprintf(stream, "%*s", (info->left ? -info->width : info->width), b);
    29   return len;
    30 }
    31 
    32 
    33 int print_k_arginfo(const struct printf_info *info, size_t n, int *argtypes, int* size) {
    34   if (n > 0) {
    35     argtypes[0] = PA_POINTER;
    36     size[0]     = sizeof(uint32_t);
    37   }
    38   return 1;
    39 }
    40 
    41 void initPrintfRBG(void) {
    42   register_printf_specifier('k', print_k, print_k_arginfo);
    43   register_printf_specifier('K', print_K, print_k_arginfo);
    44 }