Fix SGR passing edge case.

This commit is contained in:
Daniel Schmidt 2023-05-19 19:57:17 +02:00
parent 4127d81de6
commit 7b3a48d9d5
2 changed files with 30 additions and 26 deletions

4
sgr.go
View File

@ -17,6 +17,10 @@ func extractSGR(s string) (string, bool) {
}
for i := 2; i < len(s); i++ {
if s[i] == ' ' || s[i] == termenv.CSI[0] {
return "", false
}
if s[i] == 'm' {
return s[:i+1], true
}

View File

@ -1,36 +1,36 @@
package crt
import (
"bytes"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
"testing"
"bytes"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSGR(t *testing.T) {
buf := &bytes.Buffer{}
lip := lipgloss.NewRenderer(buf, termenv.WithProfile(termenv.TrueColor))
testString := lip.NewStyle().Bold(true).Foreground(lipgloss.Color("#ff00ff")).Render("Hello World") + "asdasdasdasdasd" + lip.NewStyle().Italic(true).Background(lipgloss.Color("#ff00ff")).Render("Hello World")
buf := &bytes.Buffer{}
lip := lipgloss.NewRenderer(buf, termenv.WithProfile(termenv.TrueColor))
testString := lip.NewStyle().Bold(true).Foreground(lipgloss.Color("#ff00ff")).Render("Hello World") + "asdasdasdasdasd" + lip.NewStyle().Italic(true).Background(lipgloss.Color("#ff00ff")).Render("Hello World")
var sequences []any
for i := 0; i < len(testString); i++ {
sgr, ok := extractSGR(testString[i:])
if ok {
i += len(sgr) - 1
var sequences []any
for i := 0; i < len(testString); i++ {
sgr, ok := extractSGR(testString[i:])
if ok {
i += len(sgr) - 1
if res, ok := parseSGR(sgr); ok {
sequences = append(sequences, res...)
}
}
}
if res, ok := parseSGR(sgr); ok {
sequences = append(sequences, res...)
}
}
}
assert.Equal(t, []any{
SGRBold{},
SGRFgTrueColor{R: 255, G: 0, B: 255},
SGRReset{},
SGRItalic{},
SGRBgTrueColor{R: 255, G: 0, B: 255},
SGRReset{},
}, sequences)
assert.Equal(t, []any{
SGRBold{},
SGRFgTrueColor{R: 255, G: 0, B: 255},
SGRReset{},
SGRItalic{},
SGRBgTrueColor{R: 255, G: 0, B: 255},
SGRReset{},
}, sequences)
}