Skip to content

Commit

Permalink
text: add hyperlink formatter (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyatuning committed Jul 19, 2023
1 parent 83f990b commit 62b2484
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions text/hyperlink.go
@@ -0,0 +1,14 @@
package text

import "fmt"

func Hyperlink(url, text string) string {
if url == "" {
return text
}
if text == "" {
return url
}
// 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
@@ -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"))
}

0 comments on commit 62b2484

Please sign in to comment.