Skip to content
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

Render hyperlinks in the command log output panel #3911

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion pkg/gui/command_log_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (gui *Gui) LogCommand(cmdStr string, commandLine bool) {
}

gui.Views.Extras.Autoscroll = true
gui.Views.Extras.UnderlineHyperLinksOnlyOnHover = true

textStyle := theme.DefaultTextColor
if !commandLine {
Expand All @@ -64,7 +65,7 @@ func (gui *Gui) printCommandLogHeader() {
gui.Views.Extras,
"%s: %s",
style.FgYellow.Sprint(gui.c.Tr.RandomTip),
style.FgGreen.Sprint(gui.getRandomTip()),
style.FgGreen.Sprint(style.UnderlineLinks(gui.getRandomTip())),
)
}
}
Expand Down
24 changes: 1 addition & 23 deletions pkg/gui/controllers/helpers/confirmation_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (self *ConfirmationHelper) CreatePopupPanel(ctx goContext.Context, opts typ
confirmationView.RenderTextArea()
} else {
self.c.ResetViewOrigin(confirmationView)
self.c.SetViewContent(confirmationView, style.AttrBold.Sprint(underlineLinks(opts.Prompt)))
self.c.SetViewContent(confirmationView, style.AttrBold.Sprint(style.UnderlineLinks(opts.Prompt)))
}

self.setKeyBindings(cancel, opts)
Expand All @@ -233,28 +233,6 @@ func (self *ConfirmationHelper) CreatePopupPanel(ctx goContext.Context, opts typ
self.c.Context().Push(self.c.Contexts().Confirmation)
}

func underlineLinks(text string) string {
result := ""
remaining := text
for {
linkStart := strings.Index(remaining, "https://")
if linkStart == -1 {
break
}

linkEnd := strings.IndexAny(remaining[linkStart:], " \n>")
if linkEnd == -1 {
linkEnd = len(remaining)
} else {
linkEnd += linkStart
}
underlinedLink := style.PrintSimpleHyperlink(remaining[linkStart:linkEnd])
result += remaining[:linkStart] + underlinedLink
remaining = remaining[linkEnd:]
}
return result + remaining
}

func (self *ConfirmationHelper) setKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
var onConfirm func() error
if opts.HandleConfirmPrompt != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/extras_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (gui *Gui) scrollDownExtra() error {
}

func (gui *Gui) getCmdWriter() io.Writer {
return &prefixWriter{writer: gui.Views.Extras, prefix: style.FgMagenta.Sprintf("\n\n%s\n", gui.c.Tr.GitOutput)}
return &prefixWriter{writer: gui.Views.Extras, prefix: style.FgMagenta.Sprintf("\n\n%s\n", style.UnderlineLinks(gui.c.Tr.GitOutput))}
}

// Ensures that the first write is preceded by writing a prefix.
Expand All @@ -80,5 +80,5 @@ func (self *prefixWriter) Write(p []byte) (int, error) {
return n, err
}
}
return self.writer.Write(p)
return self.writer.Write([]byte(style.UnderlineLinks(p)))
}
27 changes: 26 additions & 1 deletion pkg/gui/style/hyperlink.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package style

import "fmt"
import (
"fmt"
"strings"
)

// Render the given text as an OSC 8 hyperlink
func PrintHyperlink(text string, link string) string {
Expand All @@ -11,3 +14,25 @@ func PrintHyperlink(text string, link string) string {
func PrintSimpleHyperlink(link string) string {
return fmt.Sprintf("\033]8;;%s\033\\%s\033]8;;\033\\", link, link)
}

func UnderlineLinks[T string | []byte](text T) string {
result := ""
remaining := string(text)
for {
linkStart := strings.Index(remaining, "https://")
if linkStart == -1 {
break
}

linkEnd := strings.IndexAny(remaining[linkStart:], " \n>")
if linkEnd == -1 {
linkEnd = len(remaining)
} else {
linkEnd += linkStart
}
underlinedLink := PrintSimpleHyperlink(remaining[linkStart:linkEnd])
result += remaining[:linkStart] + underlinedLink
remaining = remaining[linkEnd:]
}
return result + remaining
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helpers
package style

import (
"testing"
Expand Down Expand Up @@ -56,7 +56,7 @@ func Test_underlineLinks(t *testing.T) {

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
result := underlineLinks(s.text)
result := UnderlineLinks(s.text)
assert.Equal(t, s.expectedResult, result)
})
}
Expand Down
Loading