From 1c621af57d032361f1f600cd10bb3d04e5dc3ec4 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 11 Sep 2024 15:38:29 -0700 Subject: [PATCH] refactor: tidy; make Options bodies call setters --- table/table.go | 52 +++++++++++++++------------- table/table_test.go | 82 +++++++++++++++++++++++++++++---------------- 2 files changed, 81 insertions(+), 53 deletions(-) diff --git a/table/table.go b/table/table.go index b5b6551..636fc3a 100644 --- a/table/table.go +++ b/table/table.go @@ -151,6 +151,8 @@ func (m *Model) SetStyles(s Styles) { // Option is used to set options in New. For example: // +// TODO change this to WithRows as the example instead +// // table := New(WithColumns([]Column{{Title: "ID", Width: 10}})) type Option func(*Model) @@ -178,14 +180,30 @@ func (m *Model) Headers(headers ...string) *Model { return m } +// WithHeaders sets the table headers. +func WithHeaders(headers []string) Option { + return func(m *Model) { + m.Headers(headers...) + } +} + // WithColumns sets the table columns (headers). -// Deprecated: use Headers instead. +// Deprecated: use WithHeaders instead. func WithColumns(cols []Column) Option { return func(m *Model) { m.Headers(colToString(cols)...) } } +// colToString helper to unwrap the Column type. +func colToString(cols []Column) []string { + var out []string + for _, col := range cols { + out = append(out, col.Title) + } + return out +} + // Rows appends rows to the table func (m *Model) Rows(rows ...[]string) *Model { m.rows = append(m.rows, rows...) @@ -193,6 +211,13 @@ func (m *Model) Rows(rows ...[]string) *Model { return m } +// WithRows sets the table rows (data). +func WithRows(rows []Row) Option { + return func(m *Model) { + m.SetRows(rows) + } +} + // rowToString helper to unwrap the Row type. func rowToString(rows []Row) [][]string { var out [][]string @@ -206,38 +231,17 @@ func rowToString(rows []Row) [][]string { return out } -// rowToString helper to unwrap the Row type. -func colToString(cols []Column) []string { - var out []string - for _, col := range cols { - out = append(out, col.Title) - } - return out -} - -// WithRows sets the table rows (data). -// Deprecated: use Rows instead. -func WithRows(rows []Row) Option { - return func(m *Model) { - rows := rowToString(rows) - m.rows = rows - m.table.Rows(rows...) - } -} - -/* options */ - // WithHeight sets the height of the table. func WithHeight(h int) Option { return func(m *Model) { - m.table.Height(h) + m.SetHeight(h) } } // WithWidth sets the width of the table. func WithWidth(w int) Option { return func(m *Model) { - m.viewport.Width = w + m.SetWidth(w) } } diff --git a/table/table_test.go b/table/table_test.go index 1765049..2a59f3a 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -10,23 +10,61 @@ import ( ) func TestFromValues(t *testing.T) { - input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" - table := New(). - Headers("Foo", "Bar") - table.FromValues(input, ",") + t.Run("Headers", func(t *testing.T) { + input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" + table := New(). + Headers("Foo", "Bar") + table.FromValues(input, ",") - if len(table.rows) != 3 { - t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows)) - } + if len(table.rows) != 3 { + t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows)) + } - expect := [][]string{ - {"foo1", "bar1"}, - {"foo2", "bar2"}, - {"foo3", "bar3"}, - } - if !reflect.DeepEqual(table.rows, expect) { - t.Fatal("table rows is not equals to the input") - } + expect := [][]string{ + {"foo1", "bar1"}, + {"foo2", "bar2"}, + {"foo3", "bar3"}, + } + if !reflect.DeepEqual(table.rows, expect) { + t.Fatal("table rows is not equals to the input") + } + }) + t.Run("WithColumns", func(t *testing.T) { + input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" + table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}})) + table.FromValues(input, ",") + + if len(table.rows) != 3 { + t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows)) + } + + expect := [][]string{ + {"foo1", "bar1"}, + {"foo2", "bar2"}, + {"foo3", "bar3"}, + } + if !reflect.DeepEqual(table.rows, expect) { + t.Fatal("table rows is not equals to the input") + } + }) + t.Run("WithHeaders", func(t *testing.T) { + input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" + table := New(WithHeaders([]Column{{Title: "Foo"}, {Title: "Bar"}})) + table.FromValues(input, ",") + + if len(table.rows) != 3 { + t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows)) + } + + expect := [][]string{ + {"foo1", "bar1"}, + {"foo2", "bar2"}, + {"foo3", "bar3"}, + } + if !reflect.DeepEqual(table.rows, expect) { + t.Fatal("table rows is not equals to the input") + } + }) } func TestFromValuesWithTabSeparator(t *testing.T) { @@ -47,20 +85,6 @@ func TestFromValuesWithTabSeparator(t *testing.T) { } } -func deepEqual(a, b []Row) bool { - if len(a) != len(b) { - return false - } - for i, r := range a { - for j, f := range r { - if f != b[i][j] { - return false - } - } - } - return true -} - func TestTableAlignment(t *testing.T) { t.Run("No border", func(t *testing.T) { s := DefaultStyles()