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 { func (a *Ansi) Title(title string) string {
// we have to do this to prevent bash from misidentifying escape sequences // we have to do this to prevent bash/zsh from misidentifying escape sequences
if a.shell == shell.BASH { switch a.shell {
title = strings.ReplaceAll(title, `\`, `\\`) case shell.BASH:
title = strings.NewReplacer("`", "\\`", `\`, `\\`).Replace(title)
case shell.ZSH:
title = strings.NewReplacer("`", "\\`", `%`, `%%`).Replace(title)
} }
return fmt.Sprintf(a.title, title) return fmt.Sprintf(a.title, title)
} }

View file

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