From f9695a7947d8ebdab93ffef4b4fb7608fd2b32ad Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 28 Aug 2022 09:22:10 +0530 Subject: [PATCH] Add url support to styling --- tools/utils/style/wrapper.go | 34 +++++++++++++++++++++++++++++-- tools/utils/style/wrapper_test.go | 2 ++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tools/utils/style/wrapper.go b/tools/utils/style/wrapper.go index 1f3f0c6d9..c5d8d22b4 100644 --- a/tools/utils/style/wrapper.go +++ b/tools/utils/style/wrapper.go @@ -272,6 +272,22 @@ func (self sgr_code) is_empty() bool { return self._prefix == "" } +type url_code struct { + url string +} + +func (self url_code) prefix() string { + return fmt.Sprintf("\x1b]8;;%s\x1b\\", self.url) +} + +func (self url_code) suffix() string { + return "\x1b]8;;\x1b\\" +} + +func (self url_code) is_empty() bool { + return self.url == "" +} + func (self *sgr_code) update() { p := make([]string, 0, 1) s := make([]string, 0, 1) @@ -283,13 +299,22 @@ func (self *sgr_code) update() { p, s = self.fg.as_sgr(30, p, s) p, s = self.bg.as_sgr(40, p, s) p, s = self.uc.as_sgr(50, p, s) - self._prefix = "\x1b[" + strings.Join(p, ";") + "m" - self._suffix = "\x1b[" + strings.Join(s, ";") + "m" + if len(p) > 0 { + self._prefix = "\x1b[" + strings.Join(p, ";") + "m" + } else { + self._prefix = "" + } + if len(s) > 0 { + self._suffix = "\x1b[" + strings.Join(s, ";") + "m" + } else { + self._suffix = "" + } } func parse_spec(spec string) []escape_code { ans := make([]escape_code, 0, 1) sgr := sgr_code{} + url := url_code{} sparts, _ := shlex.Split(spec) for _, p := range sparts { parts := strings.SplitN(p, "=", 2) @@ -317,12 +342,17 @@ func parse_spec(spec string) []escape_code { sgr.underline.from_string(val) case "ucol", "underline_color", "uc": sgr.uc.from_string(val, false) + case "url": + url.url = val } } sgr.update() if !sgr.is_empty() { ans = append(ans, &sgr) } + if !url.is_empty() { + ans = append(ans, &url) + } return ans } diff --git a/tools/utils/style/wrapper_test.go b/tools/utils/style/wrapper_test.go index bb99bc93b..deff801dd 100644 --- a/tools/utils/style/wrapper_test.go +++ b/tools/utils/style/wrapper_test.go @@ -32,6 +32,7 @@ func TestANSIStyleSprint(t *testing.T) { } } + test("", "", "") test("bold", "\x1b[1m", "\x1b[22m") test("bold fg=red u=curly", "\x1b[1;4:3;31m", "\x1b[22;4:0;39m") test("fg=123", "\x1b[38:5:123m", "\x1b[39m") @@ -42,4 +43,5 @@ func TestANSIStyleSprint(t *testing.T) { test("bg=123", "\x1b[48:5:123m", "\x1b[49m") test("uc=123", "\x1b[58:2:135:255:255m", "\x1b[59m") + test("url=123", "\x1b]8;;123\x1b\\", "\x1b]8;;\x1b\\") }