mirror of
https://github.com/BigJk/crt.git
synced 2026-02-06 10:47:25 +00:00
Fix SGR passing edge case.
This commit is contained in:
parent
4127d81de6
commit
7b3a48d9d5
4
sgr.go
4
sgr.go
@ -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
|
||||
}
|
||||
|
||||
52
sgr_test.go
52
sgr_test.go
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user