From: porkchop@invalid.foo   
      
   On Wed, 12 Nov 2025 15:45:51 -0000 (UTC), Michael Sanders wrote:   
      
   [...]   
      
   > size_t len = snprintf(NULL, 0, fmt, url,   
   > supported ? COLOR : NULL,   
   > supported ? txt : NULL,   
   > supported ? RESET : NULL   
      
   [...]   
      
   >   
   > snprintf(out, len + 1, fmt, url,   
   > supported ? COLOR : "",   
   > supported ? txt : "",   
   > supported ? RESET : ""   
   > );   
      
   Love ternary, but above, its mighty heavy-handed...   
      
   #include    
   #include    
   #include    
      
   /*   
    * click_link() - Michael Sanders 2025   
    *   
    * returns a pointer to a clickable character array if the user's   
    * terminal supports it & heads up: you must free() the pointer   
    *   
    * test for unsupported terminals: env -i TERM=dumb ./click_link   
    *   
    * enjoy!   
    */   
      
   char *click_link(const char *txt, const char *url) {   
    const char *COLOR = "\033[92m"; // green   
    const char *RESET = "\033[0m";   
    const char *FMT_URL = "\033]8;;%s\033\\%s%s%s\033]8;;\033\\";   
    const char *FMT_FALLBACK = "%s";   
    const char *term_program = getenv("TERM_PROGRAM");   
    const char *vte_version = getenv("VTE_VERSION");   
    const char *term = getenv("TERM");   
    const char *wt_session = getenv("WT_SESSION");   
    const char *fmt, *color, *text, *reset;   
      
    if ((term_program && strcmp(term_program, "iTerm.app") == 0) ||   
    vte_version || (term && strcmp(term, "xterm-kitty") == 0) ||   
    wt_session) {   
    fmt = FMT_URL;   
    color = COLOR;   
    text = txt;   
    reset = RESET;   
    } else {   
    fmt = FMT_FALLBACK;   
    color = text = reset = "";   
    }   
      
    size_t len = snprintf(NULL, 0, fmt, url, color, text, reset);   
    char *out = malloc(len + 1); if (!out) return NULL; // <--   
    snprintf(out, len + 1, fmt, url, color, text, reset);   
    return out;   
   }   
      
   int main(void) {   
    char *url = click_link("click here", "https://gitlab.com/");   
    if (url) printf("For updates %s\n", url);   
    free(url);   
    return 0;   
   }   
      
   --   
   :wq   
   Mike Sanders   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|