charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
8.22k stars 231 forks source link

Lipgloss.Width fails on particular input #374

Closed robinovitch61 closed 2 months ago

robinovitch61 commented 2 months ago

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:

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!

robinovitch61 commented 2 months ago

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