refactor: tidy; make Options bodies call setters

This commit is contained in:
bashbunni
2024-09-11 15:38:29 -07:00
parent 87b61f8646
commit 1c621af57d
2 changed files with 81 additions and 53 deletions
+28 -24
View File
@@ -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)
}
}
+53 -29
View File
@@ -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()