refactor(table): rename OverwriteStylesFromLipgloss to LipglossTable

Also, keeps any previously set data on the Lip Gloss table, if set.
This commit is contained in:
Andrey Nering
2025-05-29 17:42:12 -03:00
parent 2110223809
commit fe39af2a4d
4 changed files with 62 additions and 20 deletions
+9 -4
View File
@@ -364,14 +364,19 @@ func (m *Model) OverwriteStyles(s Styles) *Model {
return m
}
// OverwriteStylesFromLipgloss sets the [Model]'s style attributes from an
// existing [lipgloss.Table].
func (m *Model) OverwriteStylesFromLipgloss(t *table.Table) {
// LipglossTable sets the inner [lipgloss.Table].
func (m *Model) LipglossTable(t *table.Table) {
var (
previousHeaders = m.table.GetHeaders()
previousData = m.table.GetData()
)
m.table = t.Headers(previousHeaders...).Data(previousData)
if len(t.GetHeaders()) == 0 {
t = t.Headers(previousHeaders...)
}
if t.GetData() == nil || t.GetData().Rows() == 0 {
t = t.Rows(table.DataToMatrix(previousData)...)
}
m.table = t
m.useStyleFunc = true
}
+33 -16
View File
@@ -368,28 +368,45 @@ func TestNewFromTemplate(t *testing.T) {
golden.RequireEqual(t, []byte(bubblesTable.View()))
}
func TestOverwriteStylesFromLipgloss(t *testing.T) {
baseStyle := lipgloss.NewStyle().Padding(0, 1)
headerStyle := baseStyle.Foreground(lipgloss.Color("252")).Bold(true)
lipglossTable := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("238"))).
Width(80).
StyleFunc(func(row, col int) lipgloss.Style {
func TestLipglossTable(t *testing.T) {
var (
baseStyle = lipgloss.NewStyle().Padding(0, 1)
headerStyle = baseStyle.Foreground(lipgloss.Color("252")).Bold(true)
borderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("238"))
styleFunc = func(row, col int) lipgloss.Style {
if row == table.HeaderRow {
return headerStyle
}
even := row%2 == 0
if even {
if row%2 == 0 {
return baseStyle.Foreground(lipgloss.Color("245"))
}
return baseStyle.Foreground(lipgloss.Color("252"))
})
bubblesTable := New().SetHeaders(headers...).SetRows(rows...)
bubblesTable.OverwriteStylesFromLipgloss(lipglossTable)
golden.RequireEqual(t, []byte(bubblesTable.View()))
}
)
t.Run("WithoutPreviousData", func(t *testing.T) {
lipglossTable := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(borderStyle).
Width(80).
StyleFunc(styleFunc)
bubblesTable := New().SetHeaders(headers...).SetRows(rows...)
bubblesTable.LipglossTable(lipglossTable)
golden.RequireEqual(t, []byte(bubblesTable.View()))
})
t.Run("WithPreviousData", func(t *testing.T) {
lipglossTable := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(borderStyle).
Width(80).
StyleFunc(styleFunc).
Headers(headers...).
Rows(rows...)
bubblesTable := New()
bubblesTable.LipglossTable(lipglossTable)
golden.RequireEqual(t, []byte(bubblesTable.View()))
})
}
// Examples
@@ -0,0 +1,20 @@
┌───────────────────┬───────────────────┬───────────────────┬──────────────────┐
│ Rank  │ City  │ Country  │ Population  │
├───────────────────┼───────────────────┼───────────────────┼──────────────────┤
│ 1  │ Tokyo  │ Japan  │ 37,274,000  │
│ 2  │ Delhi  │ India  │ 32,065,760  │
│ 3  │ Shanghai  │ China  │ 28,516,904  │
│ 4  │ Dhaka  │ Bangladesh  │ 22,478,116  │
│ 5  │ São Paulo  │ Brazil  │ 22,429,800  │
│ 6  │ Mexico City  │ Mexico  │ 22,085,140  │
│ 7  │ Cairo  │ Egypt  │ 21,750,020  │
│ 8  │ Beijing  │ China  │ 21,333,332  │
│ 9  │ Mumbai  │ India  │ 20,961,472  │
│ 10  │ Osaka  │ Japan  │ 19,059,856  │
│ 11  │ Chongqing  │ China  │ 16,874,740  │
│ 12  │ Karachi  │ Pakistan  │ 16,839,950  │
│ 13  │ Istanbul  │ Turkey  │ 15,636,243  │
│ 14  │ Kinshasa  │ DR Congo  │ 15,628,085  │
│ 15  │ Lagos  │ Nigeria  │ 15,387,639  │
│ 16  │ Buenos Aires  │ Argentina  │ 15,369,919  │
└───────────────────┴───────────────────┴───────────────────┴──────────────────┘