lipgloss icon indicating copy to clipboard operation
lipgloss copied to clipboard

(v2) styling table not working as expected

Open bashbunni opened this issue 7 months ago • 3 comments

For the record, it's the same behaviour in v1, so not like it's broken in v2, it just never worked >.<. You should be able to set table styles with a Lip Gloss Style like so:

func main() {
	s := lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render
	baseStyle := lipgloss.NewStyle().Background(lipgloss.Color("#FF60FF")).Foreground(lipgloss.Color("#00FFB2"))

	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(baseStyle.Render(t.String()))
}

want:

Image

got:

Image

bashbunni avatar Apr 23 '25 19:04 bashbunni

fwiw this only happens when another style is used to style content inside the table. Even if StyleFunc is used to style that cell

func main() {
	baseStyle := lipgloss.NewStyle().Background(lipgloss.Color("#FF60FF")).Foreground(lipgloss.Color("#00FFB2"))

	data := [][]string{
		{"Bubble Tea", "Milky"},
		{"Milk Tea", "Also milky"},
		{"Actual milk", "Milky as well"},
	}

	t := table.New()
	t.Rows(data...)
	t.StyleFunc(func(row, col int) lipgloss.Style {
		if data[row][col] == "Milky" {
			return lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
		}
		return lipgloss.NewStyle()
	})
	fmt.Println(baseStyle.Render(t.String()))
}

Image

works in Andrey's PR though

Image

bashbunni avatar Apr 23 '25 19:04 bashbunni

Just for future reference: I added a test case that can be used to debug this on this commit: https://github.com/charmbracelet/lipgloss/commit/c887c37fe0ba2c4ff48377ced2b3c351d3ea8b17

It also includes a fix, but a non-ideal one, so don't be too attached to it.

andreynering avatar Apr 25 '25 14:04 andreynering

Also, for clarity, it was fixed with an additional setting on https://github.com/charmbracelet/lipgloss/pull/519 and https://github.com/charmbracelet/glamour/pull/431. What's not working is to detect automatically when doing:

baseStyle.Render(t.String())

andreynering avatar Apr 25 '25 15:04 andreynering