• click_link()

    From Michael Sanders@3:633/10 to All on Wed Nov 12 14:23:24 2025
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /*
    * 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
    *
    * enjoy!
    *
    */

    char *click_link(const char *txt, const char *url) {
    const char *GREEN = "\033[92m";
    const char *RESET = "\033[0m";
    const char *fmt_url = "\033]8;;%s\033\\\033[92m%s\033[0m\033]8;;\033\\";
    const char *fmt_fallback = "\033[92m%s\033[0m (%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;

    /* pick format string & estimate required length */
    size_t len = snprintf(NULL, 0, supported ?
    fmt_url : fmt_fallback, url, txt);

    char *out = malloc(len + 1); if (!out) return NULL;

    snprintf(out, len + 1, supported ? fmt_url : fmt_fallback, url, txt);
    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

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael Sanders@3:633/10 to All on Wed Nov 12 15:45:51 2025
    On Wed, 12 Nov 2025 14:23:24 -0000 (UTC), Michael Sanders wrote:

    [...]

    Better handling for colors & non-supported terminals...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /*
    * 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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    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

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael Sanders@3:633/10 to All on Wed Nov 12 18:03:21 2025
    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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /*
    * 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

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)