Updates escape code handling to be safer plus renders literally in local scheme

This commit is contained in:
sloumdrone 2019-12-13 20:19:50 -08:00
parent 021678d9da
commit f3f406260e
1 changed files with 16 additions and 4 deletions

20
page.go
View File

@ -50,15 +50,20 @@ func (p *Page) ScrollPositionRange(termHeight int) (int, int) {
func (p *Page) WrapContent(width int, color bool) { func (p *Page) WrapContent(width int, color bool) {
counter := 0 counter := 0
var content strings.Builder var content strings.Builder
var esc strings.Builder
escape := false escape := false
content.Grow(len(p.RawContent)) content.Grow(len(p.RawContent))
for _, ch := range []rune(p.RawContent) { for _, ch := range []rune(p.RawContent) {
if escape { if escape {
if color {
esc.WriteRune(ch)
}
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') { if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
escape = false escape = false
} if ch == 'm' {
if color { content.WriteString(esc.String())
content.WriteRune(ch) esc.Reset()
}
} }
continue continue
} }
@ -77,9 +82,16 @@ func (p *Page) WrapContent(width int, color bool) {
// Get rid of control characters we dont want // Get rid of control characters we dont want
continue continue
} else if ch == 27 { } else if ch == 27 {
if p.Location.Scheme == "local" {
if counter+4 >= width {
content.WriteRune('\n')
}
content.WriteString("\\033")
continue
}
escape = true escape = true
if color { if color {
content.WriteRune(ch) esc.WriteRune(ch)
} }
continue continue
} else { } else {