fix(shell): escape special characters correctly

This commit is contained in:
L. Yeung 2022-08-28 11:06:01 +08:00 committed by Jan De Dobbeleer
parent dfae989782
commit c0935c87f2
2 changed files with 10 additions and 8 deletions

View file

@ -266,9 +266,12 @@ func (a *Ansi) ClearAfter() string {
}
func (a *Ansi) Title(title string) string {
// we have to do this to prevent bash from misidentifying escape sequences
if a.shell == shell.BASH {
title = strings.ReplaceAll(title, `\`, `\\`)
// we have to do this to prevent bash/zsh from misidentifying escape sequences
switch a.shell {
case shell.BASH:
title = strings.NewReplacer("`", "\\`", `\`, `\\`).Replace(title)
case shell.ZSH:
title = strings.NewReplacer("`", "\\`", `%`, `%%`).Replace(title)
}
return fmt.Sprintf(a.title, title)
}

View file

@ -395,12 +395,11 @@ func (segment *Segment) SetText() {
if segment.Interactive {
return
}
// we have to do this to prevent bash/zsh from misidentifying escape sequences
switch segment.env.Shell() {
case shell.BASH, shell.FISH:
segment.text = strings.ReplaceAll(segment.text, `\`, `\\`)
case shell.BASH:
segment.text = strings.NewReplacer("`", "\\`", `\`, `\\`).Replace(segment.text)
case shell.ZSH:
segment.text = strings.ReplaceAll(segment.text, `%`, `%%`)
case shell.PWSH, shell.PWSH5:
segment.text = strings.ReplaceAll(segment.text, "`", "``")
segment.text = strings.NewReplacer("`", "\\`", `%`, `%%`).Replace(segment.text)
}
}