From: porkchop@invalid.foo   
      
   On Wed, 12 Nov 2025 14:23:24 -0000 (UTC), Michael Sanders wrote:   
      
   > [...]   
      
   Better handling for colors & non-supported terminals...   
      
   #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 terminals lacking url support: env -i TERM=dumb ./click_link   
    *   
    * enjoy!   
    */   
      
   #include    
   #include    
   #include    
      
   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");   
      
    int supported =   
    (term_program && strcmp(term_program, "iTerm.app") == 0) ||   
    vte_version || (term && strcmp(term, "xterm-kitty") == 0) ||   
    wt_session;   
      
    const char *fmt = supported ? fmt_url : fmt_fallback;   
      
    size_t len = snprintf(NULL, 0, fmt, url,   
    supported ? COLOR : NULL,   
    supported ? txt : NULL,   
    supported ? RESET : NULL   
    );   
      
    char *out = malloc(len + 1); if (!out) return NULL; // <--   
      
    snprintf(out, len + 1, fmt, url,   
    supported ? COLOR : "",   
    supported ? txt : "",   
    supported ? 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)   
|