Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: set cell's background style override others #688

Open
flavono123 opened this issue Dec 17, 2024 · 1 comment
Open

table: set cell's background style override others #688

flavono123 opened this issue Dec 17, 2024 · 1 comment

Comments

@flavono123
Copy link

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

# 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

@flavono123
Copy link
Author

i can fix this by myself i think, but not sure about some parts are bugs or intended:

  • cell's "background" style can override selected rows' -> a bug i guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant