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

add hyperlink formatter #263

Merged
merged 1 commit into from
Jul 19, 2023
Merged
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
14 changes: 14 additions & 0 deletions text/hyperlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package text

import "fmt"

func Hyperlink(url, text string) string {
if url == "" {
return text
}
if text == "" {
return url
}
Comment on lines +9 to +11
Copy link
Owner

@jedib0t jedib0t May 23, 2023

Choose a reason for hiding this comment

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

It may be better to default the text to url if no text was provided?

Also, rename text to alias?

// source https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", url, text)
}
13 changes: 13 additions & 0 deletions text/hyperlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package text

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHyperlink(t *testing.T) {
assert.Equal(t, "Ghost", Hyperlink("", "Ghost"))
assert.Equal(t, "https://example.com", Hyperlink("https://example.com", ""))
assert.Equal(t, "\x1b]8;;https://example.com\x1b\\Ghost\x1b]8;;\x1b\\", Hyperlink("https://example.com", "Ghost"))
}
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add an Example function for godoc? You can take a look at some of the other test files in this package to see what I'm talking about.