-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Add supports for terminal hyperlinks in output (v2) #12682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -268,8 +268,32 @@ func (c *ColorScheme) Hyperlink(text, url string) string { | |||||||||||||||||||||||||||||||||||||
| if !c.linkEnabled { | ||||||||||||||||||||||||||||||||||||||
| return text | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Make trailing spaces not to be part of the link as it looks ugly, ... | ||||||||||||||||||||||||||||||||||||||
| link_text := strings.TrimRight(text, " ") | ||||||||||||||||||||||||||||||||||||||
| trailing_spaces := text[len(link_text):] | ||||||||||||||||||||||||||||||||||||||
| if link_text == "" { | ||||||||||||||||||||||||||||||||||||||
| // ... but still allow spaces-only text to be clickable. | ||||||||||||||||||||||||||||||||||||||
| link_text = text | ||||||||||||||||||||||||||||||||||||||
| trailing_spaces = "" | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda | ||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", url, text) | ||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\%s", url, link_text, trailing_spaces) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| func (c *ColorScheme) WithHyperlink(url string, colorize func(string) string) func(string) string { | ||||||||||||||||||||||||||||||||||||||
| if colorize == nil { | ||||||||||||||||||||||||||||||||||||||
| colorize = func(s string) string { return s } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if !c.linkEnabled { | ||||||||||||||||||||||||||||||||||||||
| return colorize | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return func(text string) string { | ||||||||||||||||||||||||||||||||||||||
| // Call c.Hyperlink first, then colorize. | ||||||||||||||||||||||||||||||||||||||
| // Otherwise space-trimming logic in c.Hyperlink wouldn't work. | ||||||||||||||||||||||||||||||||||||||
| return colorize(c.Hyperlink(text, url)) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+293
to
+295
|
||||||||||||||||||||||||||||||||||||||
| // Call c.Hyperlink first, then colorize. | |
| // Otherwise space-trimming logic in c.Hyperlink wouldn't work. | |
| return colorize(c.Hyperlink(text, url)) | |
| // Make trailing spaces not to be part of the link as it looks ugly, ... | |
| link_text := strings.TrimRight(text, " ") | |
| trailing_spaces := text[len(link_text):] | |
| if link_text == "" { | |
| // ... but still allow spaces-only text to be clickable. | |
| link_text = text | |
| trailing_spaces = "" | |
| } | |
| // Apply colorization only to the part that will be inside the hyperlink, | |
| // so index-based colorizers operate on the original, unmodified text. | |
| colored_link_text := colorize(link_text) | |
| // https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda | |
| return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\%s", url, colored_link_text, trailing_spaces) |
Copilot
AI
Feb 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New hyperlink rendering logic in Hyperlink/WithHyperlink isn’t covered by unit tests. Since pkg/iostreams/color_test.go already exists, please add tests for: links disabled (returns input), links enabled (OSC 8 wrapper), and the trailing-space handling (spaces excluded from link but preserved in output).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go style in this package uses camelCase for local variables;
link_textandtrailing_spacesstand out and make the new logic harder to read/grep consistently. Please rename tolinkText/trailingSpaces(and similar) for idiomatic Go.