charmbracelet/lipgloss

Table: truncation breaks ANSI styling

meowgorithm opened this issue · 3 comments

It appears that when truncation occurs on cell data with ansi styling present the ANSI reset sequence is truncated, resulting in the styling from the cell bleeding onto subsequent rows:

In other words it appears that something like the following is happening:

"\x1b[7m " + "Milk!" + "\x1b[0m" // before; ends in ANSI reset
"\x1b[7m " + "Mi…" // post-truncation; ANSI reset missing

The following code will reproduce the issue:

package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss"
	"github.com/charmbracelet/lipgloss/table"
)

func main() {
	s := lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render

	t := table.New()
	t.Row("Bubble Tea", s("Milky"))
	t.Row("Milk Tea", s("Also milky"))
	t.Row("Actual milk", s("Milky as well"))
	fmt.Println(t.Render())
}

Output:

We should be using StyleFunc to style these cells.

func main() {
	s := lipgloss.NewStyle().Foreground(lipgloss.Color("240"))

	t := table.New()
	t.Row("Bubble Tea", "Milky")
	t.Row("Milk Tea", "Also milky")
	t.Row("Actual milk", "Milky as well")
        t.StyleFunc(func (row, col int) lipgloss.Style {
            if col == 1 {
                return s
            }
            return lipgloss.NewStyle()
        })
	fmt.Println(t.Render())
}

Aha: @maaslalani also pointed out that this is actually fixed on master, too. Sounds like it's time for a release then.

This was solved by the awesome @mikelorant!

de46012