Add url support to styling

This commit is contained in:
Kovid Goyal 2022-08-28 09:22:10 +05:30
parent 29d9b70f0c
commit f9695a7947
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 34 additions and 2 deletions

View File

@ -272,6 +272,22 @@ func (self sgr_code) is_empty() bool {
return self._prefix == "" 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() { func (self *sgr_code) update() {
p := make([]string, 0, 1) p := make([]string, 0, 1)
s := 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.fg.as_sgr(30, p, s)
p, s = self.bg.as_sgr(40, p, s) p, s = self.bg.as_sgr(40, p, s)
p, s = self.uc.as_sgr(50, p, s) p, s = self.uc.as_sgr(50, p, s)
if len(p) > 0 {
self._prefix = "\x1b[" + strings.Join(p, ";") + "m" self._prefix = "\x1b[" + strings.Join(p, ";") + "m"
} else {
self._prefix = ""
}
if len(s) > 0 {
self._suffix = "\x1b[" + strings.Join(s, ";") + "m" self._suffix = "\x1b[" + strings.Join(s, ";") + "m"
} else {
self._suffix = ""
}
} }
func parse_spec(spec string) []escape_code { func parse_spec(spec string) []escape_code {
ans := make([]escape_code, 0, 1) ans := make([]escape_code, 0, 1)
sgr := sgr_code{} sgr := sgr_code{}
url := url_code{}
sparts, _ := shlex.Split(spec) sparts, _ := shlex.Split(spec)
for _, p := range sparts { for _, p := range sparts {
parts := strings.SplitN(p, "=", 2) parts := strings.SplitN(p, "=", 2)
@ -317,12 +342,17 @@ func parse_spec(spec string) []escape_code {
sgr.underline.from_string(val) sgr.underline.from_string(val)
case "ucol", "underline_color", "uc": case "ucol", "underline_color", "uc":
sgr.uc.from_string(val, false) sgr.uc.from_string(val, false)
case "url":
url.url = val
} }
} }
sgr.update() sgr.update()
if !sgr.is_empty() { if !sgr.is_empty() {
ans = append(ans, &sgr) ans = append(ans, &sgr)
} }
if !url.is_empty() {
ans = append(ans, &url)
}
return ans return ans
} }

View File

@ -32,6 +32,7 @@ func TestANSIStyleSprint(t *testing.T) {
} }
} }
test("", "", "")
test("bold", "\x1b[1m", "\x1b[22m") test("bold", "\x1b[1m", "\x1b[22m")
test("bold fg=red u=curly", "\x1b[1;4:3;31m", "\x1b[22;4:0;39m") 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") 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("bg=123", "\x1b[48:5:123m", "\x1b[49m")
test("uc=123", "\x1b[58:2:135:255:255m", "\x1b[59m") test("uc=123", "\x1b[58:2:135:255:255m", "\x1b[59m")
test("url=123", "\x1b]8;;123\x1b\\", "\x1b]8;;\x1b\\")
} }