fix(link): allow multiple links in segment text

This commit is contained in:
Jan De Dobbeleer 2022-09-02 08:54:36 +02:00 committed by Jan De Dobbeleer
parent 56a59a1b32
commit 336f351b98
2 changed files with 26 additions and 0 deletions

View file

@ -140,6 +140,22 @@ func (a *Ansi) InitPlain() {
}
func (a *Ansi) GenerateHyperlink(text string) string {
parts := strings.SplitAfter(text, ")")
var buffer strings.Builder
var part string
for i := range parts {
if strings.Contains(parts[i], "[") && !strings.Contains(parts[i], "]") {
part += parts[i]
continue
}
part += parts[i]
buffer.WriteString(a.replaceHyperlink(part))
part = ""
}
return buffer.String()
}
func (a *Ansi) replaceHyperlink(text string) string {
// hyperlink matching
results := regex.FindNamedRegexMatch("(?P<ALL>(?:\\[(?P<TEXT>.+)\\])(?:\\((?P<URL>.*)\\)))", text)
if len(results) != 3 {

View file

@ -31,9 +31,19 @@ func TestGenerateHyperlinkWithUrl(t *testing.T) {
ShellName string
Expected string
}{
{
Text: "[google](http://www.google.be) [maps (2/2)](http://maps.google.be)",
ShellName: shell.FISH,
Expected: "\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\ \x1b]8;;http://maps.google.be\x1b\\maps (2/2)\x1b]8;;\x1b\\",
},
{Text: "[google](http://www.google.be)", ShellName: shell.ZSH, Expected: "%{\x1b]8;;http://www.google.be\x1b\\%}google%{\x1b]8;;\x1b\\%}"},
{Text: "[google](http://www.google.be)", ShellName: shell.PWSH, Expected: "\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\"},
{Text: "[google](http://www.google.be)", ShellName: shell.BASH, Expected: "\\[\x1b]8;;http://www.google.be\x1b\\\\\\]google\\[\x1b]8;;\x1b\\\\\\]"},
{
Text: "[google](http://www.google.be) [maps](http://maps.google.be)",
ShellName: shell.FISH,
Expected: "\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\ \x1b]8;;http://maps.google.be\x1b\\maps\x1b]8;;\x1b\\",
},
}
for _, tc := range cases {
a := Ansi{}