charmbracelet / charmbracelet/bubbles

table: set cell's background style override others

Open
#688 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
8.9k
Forks
457
Avg merge
1d 18h
Merged PRs (30d)
5

Description

**Describe the bug**
when set a background style to table's cell, it override to others, selected row, and ignore its foreground color and bold font which are default.

**Setup**
Please complete the following information along with version numbers, if applicable.
- OS: macOS
- Shell: zsh
- Terminal Emulator: alacritty, cursor(vscode) default
- Terminal Multiplexer: no
- Locale: en_US.UTF-8

**To Reproduce**
Steps to reproduce the behavior:
1. Run following codes(`go run ./test.go`)

**Source Code**
```go
# test.go
package main

import (
"fmt"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type model struct {
defaultTable table.Model
styledTable table.Model
}

func newModel() model {
rows := []table.Row{
{"Chocolate Digestives", "UK", "Yes"},
{"Tim Tams", "Australia", "No"},
{"Hobnobs", "UK", "Yes"},
}
columns := []table.Column{
{Title: "Name", Width: 25},
{Title: "Country of Origin", Width: 16},
{Title: "Dunk-able", Width: 12},
}

defaultTable := table.New(table.WithRows(rows), table.WithColumns(columns))
styledTable := table.New(table.WithRows(rows), table.WithColumns(columns))

styledTable.SetStyles(table.Styles{
Header: table.DefaultStyles().Header,
Selected: table.DefaultStyles().Selected,
Cell: table.DefaultStyles().Cell.Background(lipgloss.Color("220")),
})

return model{defaultTable, styledTable}
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "q" {
return m, tea.Quit
}
}
return m, nil
}

func (m model) View() string {
return lipgloss.JoinHorizontal(
lipgloss.Top,
m.defaultTable.View(),
m.styledTable.View(),
)
}

func main() {
p := tea.NewProgram(newModel())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
}
}

```

**Expected behavior**

cell's background do not override other styles

**Screenshots**

(right side is `Background(lipgloss.Color("220"))` added to default cell style)

image

**Additional context**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.