Skip to content

Commit

Permalink
fix: escape backtick
Browse files Browse the repository at this point in the history
in zsh or bash, text between backtick was interpreted
  • Loading branch information
lnu authored and JanDeDobbeleer committed May 27, 2021
1 parent 3b56c24 commit 0075ac2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,21 @@ func (a *ansiUtils) consolePwd(pwd string) string {
}
return fmt.Sprintf(a.osc99, pwd)
}

func (a *ansiUtils) escapeText(text string) string {
// what to escape/replace is different per shell
// maybe we should refactor and maintain a list of characters to escap/replace
// like we do in ansi.go for ansi codes
switch a.shell {
case zsh:
// escape double quotes
text = strings.ReplaceAll(text, "\"", "\"\"")
case bash:
// escape backslashes to avoid replacements
// https://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
text = strings.ReplaceAll(text, "\\", "\\\\")
}
// escape backtick
text = strings.ReplaceAll(text, "`", "'")
return text
}
3 changes: 3 additions & 0 deletions src/ansi_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func (a *AnsiColor) writeAndRemoveText(background, foreground, text, textToRemov
}

func (a *AnsiColor) write(background, foreground, text string) {
text = a.ansi.escapeText(text)
text = a.ansi.formatText(text)
text = a.ansi.generateHyperlink(text)

// first we match for any potentially valid colors enclosed in <>
match := findAllNamedRegexMatch(`<(?P<foreground>[^,>]+)?,?(?P<background>[^>]+)?>(?P<content>[^<]*)<\/>`, text)
for i := range match {
Expand Down
10 changes: 1 addition & 9 deletions src/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -91,13 +90,7 @@ func (b *Block) renderSegments() string {
}
b.activeSegment = segment
b.endPowerline()
segmentValue := segment.stringValue
// escape backslashes to avoid replacements
// https://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
if b.env.getShellName() == bash {
segmentValue = strings.ReplaceAll(segment.stringValue, "\\", "\\\\")
}
b.renderSegmentText(segmentValue)
b.renderSegmentText(segment.stringValue)
}
if b.previousActiveSegment != nil && b.previousActiveSegment.Style == Powerline {
b.writePowerLineSeparator(Transparent, b.previousActiveSegment.background(), true)
Expand Down Expand Up @@ -174,7 +167,6 @@ func (b *Block) renderDiamondSegment(text string) {
}

func (b *Block) renderText(text string) {
text = b.ansi.generateHyperlink(text)
defaultValue := " "
prefix := b.activeSegment.getValue(Prefix, defaultValue)
postfix := b.activeSegment.getValue(Postfix, defaultValue)
Expand Down
1 change: 1 addition & 0 deletions src/console_title.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (t *consoleTitle) getConsoleTitle() string {
default:
title = base(t.getPwd(), t.env)
}
title = t.ansi.escapeText(title)
return fmt.Sprintf(t.ansi.title, title)
}

Expand Down

0 comments on commit 0075ac2

Please sign in to comment.