charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
7.87k stars 222 forks source link

Formatting lost on linewrap #85

Open FredrikL opened 2 years ago

FredrikL commented 2 years ago

When rendering a long string with styles applied in a "box" the text formatting is lost when lines wrap.

Attached output + example code to reproduce (uses lipgloss 0.5.0)

Output image

Code to reproduce

func main() {
    parts := strings.Split("aaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccc", ".")
    formatted := fmt.Sprintf("%s.%s.%s",
        lipgloss.NewStyle().Foreground(lipgloss.Color("#874BFD")).Render(parts[0]),
        lipgloss.NewStyle().Foreground(lipgloss.Color("#ABE9B3")).Render(parts[1]),
        lipgloss.NewStyle().Foreground(lipgloss.Color("#89DCEB")).Render(parts[2]))

    dialogBoxStyle := lipgloss.NewStyle().
        Border(lipgloss.RoundedBorder()).
        Padding(1, 1).
        Width(40).
        BorderTop(true).
        BorderLeft(true).
        BorderRight(true).
        BorderBottom(true)

    inp := dialogBoxStyle.BorderForeground(lipgloss.Color("#df8e1d")).Render(formatted)

    fmt.Println(inp)
}
wesleimp commented 2 years ago

I'd like to tackle this issue. But I don't know how to solve it. I know the problem is at this line due to the border is resetting the previous wrapped line style

maaslalani commented 2 years ago

@wesleimp I'm not fully certain on the solution here either. But I think one possible route is to save some sort of state regarding the color before the border is drawn and then re-rendering that color after the border is rendered to the screen.

wesleimp commented 2 years ago

@maaslalani yeah, I was thinking about doing in that way. Maybe something like:

// get styles from the last line char
lastAppliedStyle := styles.GetFrom(line[len(line)])

But I don't know if there's a way to do that.

adamyaziji commented 6 months ago

Hey, is there any update on this? I think I'm running into the same issue. I've attached two screenshots. The first line is the only one to have the correct style regardless of the line length. Any guidance would be greatly appreciated!

Screenshot 2024-02-23 at 20 45 02 Screenshot 2024-02-23 at 20 45 21
colececil commented 4 months ago

I ran into this same issue recently. Until it gets fixed, it seems the way to work around it is to wrap the text before styling it, instead of after.