package viewport
import (
"github.com/charmbracelet/lipgloss"
"testing"
)
func TestViewport_StringWidth(t *testing.T) {
testCases := []struct {
input string
expectedWidth int
}{
{
"\\x1b[38;2;214;125;17mfoo\\x1b[0m",
len("foo"), // All the other test cases pass, this fails with "expected width 3, but got 31"
},
{
"\x1b[31mHello, World!\x1b[0m",
13, // Expected width of "Hello, World!" without ANSI codes
},
{
"\x1b[1mBold Text\x1b[0m",
9, // Expected width of "Bold Text"
},
{
"No ANSI here, just plain text",
29, // Expected width of the plain string
},
{
"\x1b[1m\x1b[0m",
0, // Only bold and reset codes, no text
},
}
for _, tc := range testCases {
result := lipgloss.Width(tc.input)
if result != tc.expectedWidth {
t.Errorf("For input '%s', expected width %d, but got %d", tc.input, tc.expectedWidth, result)
}
}
}
Describe the bug
The function
lipgloss.Width
fails on a particular input.Setup Please complete the following information along with version numbers, if applicable.
To Reproduce
Test case:
Source Code See above
Expected behavior The test passes:
Screenshots See above
Additional context Thanks!