charmbracelet/lipgloss

Width calculation not returning expected results

bljohnsondev opened this issue · 3 comments

Describe the bug
New to lipgloss and I was trying to find a way to add titles to boxes and in one of the issues someone posted a link to this as an example.

When I tested this code the top border is off by one character:

image

I found that the lipgloss.Width() calculation seems to be off. Here is a basic example of the widths:

lstyle := lipgloss.NewStyle().
	PaddingTop(0).
	PaddingBottom(0).
	PaddingLeft(1).
	PaddingRight(1)

bstyle := lipgloss.NewStyle().Foreground(b.BoxStyle.GetBorderTopForeground())

bleft := bstyle.Render(border.TopLeft)
bright := bstyle.Render(border.TopRight)
toplabel := lstyle.Render("Title")

widthall := lipgloss.Width(bleft + bright + toplabel)
widthind := lipgloss.Width(bleft) + lipgloss.Width(bright) + lipgloss.Width(toplabel)

fmt.Printf("width bleft = %d, width bright = %d, width toplabel = %d\n",
	lipgloss.Width(bleft),
	lipgloss.Width(bright),
	lipgloss.Width(toplabel),
)
fmt.Printf("width all = %d\n", widthall)
fmt.Printf("width ind = %d\n", widthind)

The result of this is the following:

width bleft = 1, width bright = 1, width toplabel = 7
width all = 8
width ind = 9

Unless I'm missing something shouldn't the width values for lenall and lenind both be 9?

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

  • OS: Universal Blue - Bluefin (based on Fedora)
  • Shell: zsh
  • Terminal Emulator: ptyxis (also tried with WezTerm)
  • Terminal Multiplexer: none
  • Locale: en_US.UTF-8

Expected behavior
I would have expected the width of the concatenated strings to be the same as the addition of each width.

I was running this in a bubbletea view when having this issue. I ran this with lipgloss alone and it works fine. This seems to be a problem with lipgloss+bubbletea. I will close for now.

@bljohnsondev I believe this was a regression, which we just pushed a fix for today. That is to say, if you bump your Lip Gloss dep to v0.12.1 the issue should be fixed now.

@bljohnsondev I believe this was a regression, which we just pushed a fix for today. That is to say, if you bump your Lip Gloss dep to v0.12.1 the issue should be fixed now.

I bumped it to v0.12.1 and it fixed the issue. Thanks for the quick response!