From 4519bbf1f7bec03dff0f71c0b0d9fbdf6de644a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20Bal=C3=A1zs?= Date: Sun, 14 May 2023 17:00:41 +0200 Subject: [PATCH] add hyperlink formatter --- 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")) +}