From 649935a610d4f97ad99f98fed2facffdd5be5265 Mon Sep 17 00:00:00 2001 From: George Stark Date: Tue, 31 Jan 2023 12:37:30 +0300 Subject: Add support for OSC 8 hyperlinks (HTML-like anchors) backport of 1c6f8db736efc62d9a9b38bfbc43ec03c8544696 from gnome-terminal --- src/terminal-util.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/terminal-util.c') diff --git a/src/terminal-util.c b/src/terminal-util.c index ede72da..d0f1864 100644 --- a/src/terminal-util.c +++ b/src/terminal-util.c @@ -22,6 +22,7 @@ #include +#define _GNU_SOURCE /* for strchrnul */ #include #include #include @@ -707,6 +708,55 @@ terminal_util_add_proxy_env (GHashTable *env_table) g_object_unref (settings_socks); } +/** + * terminal_util_hyperlink_uri_label: + * @uri: a URI + * + * Formats @uri to be displayed in a tooltip. + * Performs URI-decoding and converts IDN hostname to UTF-8. + * + * Returns: (transfer full): The human readable URI as plain text + */ +char *terminal_util_hyperlink_uri_label (const char *uri) +{ + char *unesc = NULL; + gboolean replace_hostname; + + if (uri == NULL) + return NULL; + + unesc = g_uri_unescape_string(uri, NULL); + if (unesc == NULL) + unesc = g_strdup(uri); + + if (g_ascii_strncasecmp(unesc, "ftp://", 6) == 0 || + g_ascii_strncasecmp(unesc, "http://", 7) == 0 || + g_ascii_strncasecmp(unesc, "https://", 8) == 0) + { + char *unidn = NULL; + + char *hostname = strchr(unesc, '/') + 2; + char *hostname_end = strchrnul(hostname, '/'); + char save = *hostname_end; + *hostname_end = '\0'; + unidn = g_hostname_to_unicode(hostname); + replace_hostname = unidn != NULL && g_ascii_strcasecmp(unidn, hostname) != 0; + *hostname_end = save; + if (replace_hostname) + { + char *new_unesc = g_strdup_printf("%.*s%s%s", + (int) (hostname - unesc), + unesc, + unidn, + hostname_end); + g_free(unesc); + unesc = new_unesc; + } + g_free(unidn); + } + return unesc; +} + /* Bidirectional object/widget binding */ typedef struct -- cgit v1.2.1