From fe39af2a4de5342f1e4cb3285e755e5fe0c5b60d Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 29 May 2025 17:41:43 -0300 Subject: [PATCH] refactor(table): rename `OverwriteStylesFromLipgloss` to `LipglossTable` Also, keeps any previously set data on the Lip Gloss table, if set. --- table/table.go | 13 +++-- table/table_test.go | 49 +++++++++++++------ .../WithPreviousData.golden} | 0 .../WithoutPreviousData.golden | 20 ++++++++ 4 files changed, 62 insertions(+), 20 deletions(-) rename table/testdata/{TestOverwriteStylesFromLipgloss.golden => TestLipglossTable/WithPreviousData.golden} (100%) create mode 100644 table/testdata/TestLipglossTable/WithoutPreviousData.golden diff --git a/table/table.go b/table/table.go index 0e80154..69f39c8 100644 --- a/table/table.go +++ b/table/table.go @@ -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 } diff --git a/table/table_test.go b/table/table_test.go index 606ff9a..1008170 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -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 diff --git a/table/testdata/TestOverwriteStylesFromLipgloss.golden b/table/testdata/TestLipglossTable/WithPreviousData.golden similarity index 100% rename from table/testdata/TestOverwriteStylesFromLipgloss.golden rename to table/testdata/TestLipglossTable/WithPreviousData.golden diff --git a/table/testdata/TestLipglossTable/WithoutPreviousData.golden b/table/testdata/TestLipglossTable/WithoutPreviousData.golden new file mode 100644 index 0000000..3cb3da3 --- /dev/null +++ b/table/testdata/TestLipglossTable/WithoutPreviousData.golden @@ -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  │ +└───────────────────┴───────────────────┴───────────────────┴──────────────────┘ \ No newline at end of file