charmbracelet / x

Charm experimental packages
MIT License
139 stars 15 forks source link

ansi.Wrap does not wrap at 1 character #213

Closed pachecot closed 5 days ago

pachecot commented 1 week ago

trying to keep styles and reformat as a column.

running

fmt.Println(ansi.Wrap("VERTICAL", 1, ""))

I get..

VE
RT
IC
AL

expected

V
E
R
T
I
C
A
L

fmt.Println(ansi.Wrap("VERTICAL", 2, "")) gives a column 2 character wide as you would expect,

caarlos0 commented 6 days ago

are you sure you're using the correct package? I just added this test case, and it passes:

func TestIssue_213(t *testing.T) {
    expected := "VE\nRT\nIC\nAL"
    got := ansi.Wrap("VERTICAL", 2, "")
    if got != expected {
        t.Errorf("expected %q, got %q", expected, got)
    }
}

Can you confirm that you are using the pkg github.com/charmbracelet/x/ansi in its latest version?

pachecot commented 6 days ago

not sure if its the latest..

using from lipgloss github.com/charmbracelet/x/ansi v0.3.2

will pull latest later a do some more testing.

I didn't see any tests with a limit of 1.

try this..

func TestIssue_213(t *testing.T) {
    expected := "V\nE\nR\nT\nI\nC\nA\nL"
    got := ansi.Wrap("VERTICAL", 1, "")
    if got != expected {
        t.Errorf("expected %q, got %q", expected, got)
    }
}

Also this is probably a separate issue.

If I understand the function then if there are styles in the input it would/should do this.

func TestIssue_XXX(t *testing.T) {
    expected := "\x1b[31mV\nE\nR\nT\nI\nC\nA\nL\x1b[39m"
    got := ansi.Wrap("\x1b[31mVERTICAL\x1b[39m", 1, "")
    if got != expected {
        t.Errorf("expected %q, got %q", expected, got)
    }
}

inserting the '\n' between the open a and closing styles may generate some unexpected behavior in using this utility.

this is may be what you really want...

"\x1b[31mV\x1b[39m\n\x1b[31mE\x1b[39m\n\x1b[31mR\x1b[39m\n\x1b[31mT\x1b[39m\n\x1b[31mI\x1b[39m\n\x1b[31mC\x1b[39m\n\x1b[31mA\x1b[39m\n\x1b[31mL\x1b[39m"

Thanks Tom

caarlos0 commented 5 days ago

ah sorry, I misunderstood your issue!

thanks for the writeup - and the PR :)

cheers!

aymanbagabas commented 5 days ago

inserting the '\n' between the open a and closing styles may generate some unexpected behavior in using this utility.

this is may be what you really want...

"\x1b[31mV\x1b[39m\n\x1b[31mE\x1b[39m\n\x1b[31mR\x1b[39m\n\x1b[31mT\x1b[39m\n\x1b[31mI\x1b[39m\n\x1b[31mC\x1b[39m\n\x1b[31mA\x1b[39m\n\x1b[31mL\x1b[39m"

You're correct here. The idea behind ansi.Wrap and others was to be a generic solution for different escape sequences. We probably want to come up with special solutions for such cases and terminate or reset the SGR style sequence. Perhaps Lip Gloss might be the right place 🤔