From 62b248493def406b692da0e502439ea194d02081 Mon Sep 17 00:00:00 2001 From: Balazs Sandor Date: Wed, 19 Jul 2023 09:27:27 +0200 Subject: [PATCH] text: add hyperlink formatter (#263) --- text/hyperlink.go | 14 ++++++++++++++ text/hyperlink_test.go | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 text/hyperlink.go create mode 100644 text/hyperlink_test.go diff --git a/text/hyperlink.go b/text/hyperlink.go new file mode 100644 index 0000000..00a551a --- /dev/null +++ b/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) +} diff --git a/text/hyperlink_test.go b/text/hyperlink_test.go new file mode 100644 index 0000000..7d7692c --- /dev/null +++ b/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")) +}