charmbracelet/lipgloss

Lipgloss.Width fails on particular input

Closed this issue · 1 comments

Describe the bug

The function lipgloss.Width fails on a particular input.

Setup
Please complete the following information along with version numbers, if applicable.

  • OS [e.g. Ubuntu, macOS] macos
  • Shell [e.g. zsh, fish] zsh
  • Terminal Emulator [e.g. kitty, iterm] iterm2
  • Terminal Multiplexer [e.g. tmux] tmux
  • Locale [e.g. en_US.UTF-8, zh_CN.UTF-8, etc.]
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

To Reproduce

Test case:

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)
		}
	}
}

Source Code
See above

Expected behavior
The test passes:
image

Screenshots
See above

Additional context
Thanks!

Sorry, this works fine - I copy/pasted \ instead of \ in the ansi escape seqs. Closing, sorry for noise.