Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add WithHyperlink helper and trim spaces
  • Loading branch information
xzfc committed Feb 14, 2026
commit 9f9274a3a760534096478090a70fc18533617e47
7 changes: 2 additions & 5 deletions pkg/cmd/issue/shared/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ func PrintIssues(io *iostreams.IOStreams, now time.Time, prefix string, totalCou
issueNum = "#" + issueNum
}
issueNum = prefix + issueNum
colorFunc := cs.ColorFromString(prShared.ColorForIssueState(issue))
issueURL := issue.URL
table.AddField(issueNum, tableprinter.WithColor(func(t string) string {
return colorFunc(cs.Hyperlink(t, issueURL))
}))
table.AddField(issueNum, tableprinter.WithColor(cs.WithHyperlink(issue.URL,
cs.ColorFromString(prShared.ColorForIssueState(issue)))))
if !isTTY {
table.AddField(issue.State)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/pr/checks/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func addRow(tp *tableprinter.TablePrinter, io *iostreams.IOStreams, o check) {
name += fmt.Sprintf(" (%s)", o.Event)
}
tp.AddField(mark, tableprinter.WithColor(markColor))
link := o.Link
tp.AddField(name, tableprinter.WithColor(func(t string) string {
return cs.Hyperlink(t, link)
}))
tp.AddField(name, tableprinter.WithColor(cs.WithHyperlink(o.Link, nil)))
tp.AddField(o.Description)
tp.AddField(elapsed)
if !isLinkEnabled {
Expand Down
8 changes: 3 additions & 5 deletions pkg/cmd/pr/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,9 @@ func listRun(opts *ListOptions) error {
if isTTY {
prNum = "#" + prNum
}
prURL := pr.URL
colorFunc := cs.ColorFromString(shared.ColorForPRState(pr))
table.AddField(prNum, tableprinter.WithColor(func(t string) string {
return colorFunc(cs.Hyperlink(t, prURL))
}))

table.AddField(prNum, tableprinter.WithColor(cs.WithHyperlink(pr.URL,
cs.ColorFromString(shared.ColorForPRState(pr)))))
table.AddField(text.RemoveExcessiveWhitespace(pr.Title))
table.AddField(pr.HeadLabel(), tableprinter.WithColor(cs.Cyan))
if !isTTY {
Expand Down
26 changes: 25 additions & 1 deletion pkg/iostreams/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +273 to +282
Copy link

Copilot AI Feb 14, 2026

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_text and trailing_spaces stand out and make the new logic harder to read/grep consistently. Please rename to linkText / trailingSpaces (and similar) for idiomatic Go.

Suggested change
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\\%s", url, link_text, trailing_spaces)
linkText := strings.TrimRight(text, " ")
trailingSpaces := text[len(linkText):]
if linkText == "" {
// ... but still allow spaces-only text to be clickable.
linkText = text
trailingSpaces = ""
}
// https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\%s", url, linkText, trailingSpaces)

Copilot uses AI. Check for mistakes.
}

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
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithHyperlink currently calls Hyperlink before invoking the provided colorize function. This is unsafe for colorizers that transform the input based on string indices/content (e.g., highlightMatch in gist list), because the injected OSC 8 bytes will shift indices and can corrupt both highlighting and the hyperlink escape sequence. Consider restructuring so you first split/truncate trailing padding from the original text, then apply colorize to the non-padding portion, and finally wrap that result in the OSC 8 sequence while appending the original trailing spaces outside the link.

Suggested change
// 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 uses AI. Check for mistakes.
}
}
Comment on lines +267 to +297
Copy link

Copilot AI Feb 14, 2026

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).

Copilot uses AI. Check for mistakes.

// Label stylizes text based on label's RGB hex color.
Expand Down