From 63869e38890871c58563e39301d17a1a45d37cd7 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 11 Sep 2024 14:34:32 -0700 Subject: [PATCH] feat: add BorderHeader to Styles --- table/table.go | 23 ++++++++++++----------- table/table_test.go | 26 +++++++++++--------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/table/table.go b/table/table.go index 9c13d98..3684167 100644 --- a/table/table.go +++ b/table/table.go @@ -113,29 +113,30 @@ func DefaultKeyMap() KeyMap { // Styles contains style definitions for this list component. By default, these // values are generated by DefaultStyles. type Styles struct { - // TODO is there a way to extract the border from the style? - Border lipgloss.Border - // Why doesn't setting the BorderStyle overwrite the default lip gloss table border? - BorderStyle lipgloss.Style - Header lipgloss.Style - Cell lipgloss.Style - Selected lipgloss.Style + Border lipgloss.Border + BorderStyle lipgloss.Style + BorderHeader bool + Header lipgloss.Style + Cell lipgloss.Style + Selected lipgloss.Style } // DefaultStyles returns a set of default style definitions for this table. func DefaultStyles() Styles { return Styles{ - BorderStyle: lipgloss.NewStyle().BorderStyle(lipgloss.HiddenBorder()), - Selected: lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212")), - Header: lipgloss.NewStyle().Bold(true).Padding(0, 1), - Cell: lipgloss.NewStyle().Padding(0, 1), + BorderHeader: true, + Selected: lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212")), + Header: lipgloss.NewStyle().Bold(true).Padding(0, 1), + Cell: lipgloss.NewStyle().Margin(0, 1), } } // SetStyles sets the table styles. func (m *Model) SetStyles(s Styles) { m.styles = s + m.table.Border(s.Border) m.table.BorderStyle(s.BorderStyle) + m.table.BorderHeader(s.BorderHeader) m.table.StyleFunc(func(row, col int) lipgloss.Style { if row == HEADER { return s.Header diff --git a/table/table_test.go b/table/table_test.go index df8e97b..80c6bc2 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -61,14 +61,16 @@ func deepEqual(a, b []Row) bool { return true } -var cols = []Column{ - {Title: "col1", Width: 10}, - {Title: "col2", Width: 10}, - {Title: "col3", Width: 10}, -} +// func TestWithColumns(t *testing.T) { +// t.Run("With columns") +// +// t.Run("set headers directly") +// } func TestTableAlignment(t *testing.T) { t.Run("No border", func(t *testing.T) { + s := DefaultStyles() + s.BorderHeader = false biscuits := New( WithHeight(5), WithColumns([]Column{ @@ -81,21 +83,15 @@ func TestTableAlignment(t *testing.T) { {"Tim Tams", "Australia", "No"}, {"Hobnobs", "UK", "Yes"}, }), - WithStyles(Styles{BorderStyle: lipgloss.NewStyle().BorderStyle(lipgloss.HiddenBorder())}), + WithStyles(s), ) got := ansi.Strip(biscuits.View()) golden.RequireEqual(t, []byte(got)) }) t.Run("With border", func(t *testing.T) { - baseStyle := lipgloss.NewStyle(). - BorderStyle(lipgloss.NormalBorder()). - BorderForeground(lipgloss.Color("240")) - s := DefaultStyles() - - s.Header = s.Header. - BorderBottom(true). - Bold(false) + s.Border = lipgloss.NormalBorder() + s.BorderStyle = lipgloss.NewStyle().BorderForeground(lipgloss.Color("240")) biscuits := New( WithHeight(5), @@ -111,7 +107,7 @@ func TestTableAlignment(t *testing.T) { }), WithStyles(s), ) - got := ansi.Strip(baseStyle.Render(biscuits.View())) + got := ansi.Strip(biscuits.View()) golden.RequireEqual(t, []byte(got)) }) }