fix(title): trim ansi

resolves #1922
This commit is contained in:
Jan De Dobbeleer 2022-03-14 22:08:55 +01:00 committed by Jan De Dobbeleer
parent 096cb8e997
commit d4fbbaebb3
3 changed files with 7 additions and 2 deletions

View file

@ -17,11 +17,11 @@ func measureText(text string) int {
text = strings.ReplaceAll(text, match["STR"], match["TEXT"])
}
}
text = textWithoutAnsi(text)
text = TrimAnsi(text)
return utf8.RuneCountInString(text)
}
func textWithoutAnsi(text string) string {
func TrimAnsi(text string) string {
if len(text) == 0 || !strings.Contains(text, "\x1b") {
return text
}

View file

@ -14,6 +14,7 @@ type Title struct {
func (t *Title) GetTitle() string {
title := t.getTitleTemplateText()
title = color.TrimAnsi(title)
title = t.Ansi.EscapeText(title)
return t.Ansi.Title(title)
}

View file

@ -96,6 +96,10 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
ShellName: "PowerShell",
Expected: "\x1b]0;MyUser@ :: PowerShell\a",
},
{
Template: "\x1b[93m[\x1b[39m\x1b[96mconsole-title\x1b[39m\x1b[96m ≡\x1b[39m\x1b[31m +0\x1b[39m\x1b[31m ~1\x1b[39m\x1b[31m -0\x1b[39m\x1b[31m !\x1b[39m\x1b[93m]\x1b[39m",
Expected: "\x1b]0;[console-title ≡ +0 ~1 -0 !]\a",
},
}
for _, tc := range cases {