From ca1e51c615f6c4ab3282b62ef7189c0f3089b66a Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 10 Jun 2026 18:01:11 +0200 Subject: [PATCH] Add support for named `[url]` tags in `print_rich()` ANSI conversion This uses OSC 8 escape codes, which are well-supported in modern terminal emulators. Note that Terminal.app and conhost (the legacy Windows console) don't support named URLs, but unnamed URLs can still be used. --- core/string/print_string.cpp | 17 ++++++++++++++++- doc/classes/@GlobalScope.xml | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core/string/print_string.cpp b/core/string/print_string.cpp index bae0e26e17..67239a924a 100644 --- a/core/string/print_string.cpp +++ b/core/string/print_string.cpp @@ -119,6 +119,7 @@ void __print_line_rich(const String &p_string) { String output; int pos = 0; + bool in_named_url = false; while (pos <= p_string.length()) { int brk_pos = p_string.find_char('[', pos); @@ -167,10 +168,24 @@ void __print_line_rich(const String &p_string) { output += "\u001b[2m"; } else if (tag == "/code") { output += "\u001b[22m"; + } else if (tag.begins_with("url=")) { + // Support named URLs using OSC 8 escape codes: + // + in_named_url = true; + const String url_link = tag.substr(strlen("url=")); + output += vformat("\u001b]8;;%s\u001b\\", url_link); } else if (tag == "url") { output += ""; } else if (tag == "/url") { - output += ""; + if (in_named_url) { + output += "\u001b]8;;\u001b\\"; + in_named_url = false; + } else { + // While it's legal to close an URL that was never opened using OSC 8 escape codes, + // it would result in the code being printed to unsupported terminal emulators + // when using unnamed URLs. + output += ""; + } } else if (tag == "center") { output += "\n\t\t\t"; } else if (tag == "/center") { diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index caf42a71a2..ac2f4c30d3 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -865,7 +865,7 @@ Converts one or more arguments of any type to string in the best way possible and prints them to the console. The following BBCode tags are supported: [code]b[/code], [code]i[/code], [code]u[/code], [code]s[/code], [code]indent[/code], [code]code[/code], [code]url[/code], [code]center[/code], [code]right[/code], [code]color[/code], [code]bgcolor[/code], [code]fgcolor[/code]. - URL tags only support URLs wrapped by a URL tag, not URLs with a different title. + URL tags support both URLs wrapped by a URL tag and URLs with a different title ([code skip-lint][url=address]text[/url][/code]). Support for URLs with a different title varies across terminal emulators. Most modern terminal emulators support them, but Terminal.app and conhost (the legacy Windows console) do not. When printing to standard output, the supported subset of BBCode is converted to ANSI escape codes for the terminal emulator to display. Support for ANSI escape codes varies across terminal emulators, especially for italic and strikethrough. In standard output, [code]code[/code] is represented with faint text but without any font change. Unsupported tags are left as-is in standard output. [codeblocks] [gdscript skip-lint]