Skip to content

Commit

Permalink
refactor: add Row method
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 4, 2023
1 parent 1a1ea08 commit 801645e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
22 changes: 20 additions & 2 deletions table/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package table
// Data is the interface that wraps the basic methods of a table model.
type Data interface {
Row(row int) Row
Append(row Row)
Count() int
Columns() int
}

// Row represents one line in the table.
type Row interface {
Column(col int) string
Length() int
}

// StringData is a string-based implementation of the Data interface.
Expand All @@ -18,8 +20,8 @@ type StringData struct {
columns int
}

// StringRows creates a new StringData with the given number of columns.
func StringRows(rows ...[]string) *StringData {
// Rows creates a new StringData with the given number of columns.
func Rows(rows ...[]string) *StringData {
m := StringData{columns: 0}

for _, row := range rows {
Expand All @@ -30,6 +32,12 @@ func StringRows(rows ...[]string) *StringData {
return &m
}

// Append appends the given row to the table.
func (m *StringData) Append(row Row) {
m.columns = max(m.columns, row.Length())
m.rows = append(m.rows, row)
}

// Row returns the row at the given index.
func (m *StringData) Row(row int) Row {
return m.rows[row]
Expand Down Expand Up @@ -65,6 +73,11 @@ func (r StringRow) Column(col int) string {
return r[col]
}

// Value returns the value of the column at the given index.
func (r StringRow) Length() int {
return len(r)
}

// Filter applies a filter on some data.
type Filter struct {
data Data
Expand Down Expand Up @@ -98,6 +111,11 @@ func (m *Filter) Row(row int) Row {
return nil
}

// Append appends the given row to the table.
func (m *Filter) Append(row Row) {
m.data.Append(row)
}

// Columns returns the number of columns in the table.
func (m *Filter) Columns() int {
return m.data.Columns()
Expand Down
11 changes: 10 additions & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ func (t *Table) style(row, col int) lipgloss.Style {
return t.styleFunc(row, col)
}

// Rows sets the table model.
// Rows sets the table data.
func (t *Table) Rows(data Data) *Table {
t.data = data
return t
}

// Row appends a row to the table data.
func (t *Table) Row(data ...string) *Table {
if t.data == nil {
t.data = Rows()
}
t.data.Append(StringRow(data))
return t
}

// Headers sets the table headers.
func (t *Table) Headers(headers ...any) *Table {
t.headers = headers
Expand Down
61 changes: 29 additions & 32 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ var TableStyle = func(row, col int) lipgloss.Style {
}

func TestTable(t *testing.T) {
rows := StringRows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Item("Russian", "Zdravstvuyte", "Privet").
Item("Spanish", "Hola", "ΒΏQuΓ© tal?")

table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TableStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows)
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにけは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")

expected := strings.TrimSpace(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
Expand All @@ -50,7 +47,7 @@ func TestTable(t *testing.T) {
}

func TestTableOffset(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -80,8 +77,8 @@ func TestTableOffset(t *testing.T) {
}
}

func TestFilterModel(t *testing.T) {
rows := StringRows().
func TestFilter(t *testing.T) {
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -115,7 +112,7 @@ func TestFilterModel(t *testing.T) {
}

func TestTableBorder(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -153,7 +150,7 @@ func TestTableSetRows(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Border(lipgloss.NormalBorder()).
Expand Down Expand Up @@ -186,7 +183,7 @@ func TestMoreCellsThanHeaders(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Border(lipgloss.NormalBorder()).
Expand Down Expand Up @@ -219,7 +216,7 @@ func TestMoreCellsThanHeadersExtra(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet", "Privet", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Border(lipgloss.NormalBorder()).
Expand All @@ -245,7 +242,7 @@ func TestMoreCellsThanHeadersExtra(t *testing.T) {
}

func TestTableNoHeaders(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -273,7 +270,7 @@ func TestTableNoHeaders(t *testing.T) {
}

func TestTableNoColumnSeparators(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -302,7 +299,7 @@ func TestTableNoColumnSeparators(t *testing.T) {
}

func TestTableNoColumnSeparatorsWithHeaders(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -341,7 +338,7 @@ func TestBorderColumnsWithExtraRows(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet", "Privet", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Border(lipgloss.NormalBorder()).
Expand Down Expand Up @@ -376,7 +373,7 @@ func TestNew(t *testing.T) {
}

func TestTableUnsetBorders(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -408,7 +405,7 @@ func TestTableUnsetBorders(t *testing.T) {
}

func TestTableUnsetHeaderSeparator(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -440,7 +437,7 @@ func TestTableUnsetHeaderSeparator(t *testing.T) {
}

func TestTableUnsetHeaderSeparatorWithBorder(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -471,7 +468,7 @@ func TestTableUnsetHeaderSeparatorWithBorder(t *testing.T) {
}

func TestTableRowSeparators(t *testing.T) {
rows := StringRows().
rows := Rows().
Item("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Item("French", "Bonjour", "Salut").
Item("Japanese", "こんにけは", "やあ").
Expand Down Expand Up @@ -517,7 +514,7 @@ func TestTableHeights(t *testing.T) {
return lipgloss.NewStyle().Width(25).Padding(1, 2)
}

rows := StringRows().
rows := Rows().
Item("Chutar o balde", `Literally translates to "kick the bucket." It's used when someone gives up or loses patience.`).
Item("Engolir sapos", `Literally means "to swallow frogs." It's used to describe someone who has to tolerate or endure unpleasant situations.`).
Item("Arroz de festa", `Literally means "party rice." ItΒ΄s used to refer to someone who shows up everywhere.`)
Expand Down Expand Up @@ -574,7 +571,7 @@ func TestTableMultiLineRowSeparator(t *testing.T) {
return lipgloss.NewStyle().Width(25).Padding(1, 2)
}

rows := StringRows().
rows := Rows().
Item("Chutar o balde", `Literally translates to "kick the bucket." It's used when someone gives up or loses patience.`).
Item("Engolir sapos", `Literally means "to swallow frogs." It's used to describe someone who has to tolerate or endure unpleasant situations.`).
Item("Arroz de festa", `Literally means "party rice." ItΒ΄s used to refer to someone who shows up everywhere.`)
Expand Down Expand Up @@ -631,7 +628,7 @@ func TestTableWidthExpand(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Width(80).
Expand Down Expand Up @@ -669,7 +666,7 @@ func TestTableWidthShrink(t *testing.T) {
{"Russian", "Zdravstvuyte", "Privet"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Width(30).
Expand Down Expand Up @@ -701,7 +698,7 @@ func TestTableWidthSmartCrop(t *testing.T) {
{"Eli", "30", "London"},
{"Iris", "20", "Paris"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Width(25).
Expand Down Expand Up @@ -734,7 +731,7 @@ func TestTableWidthSmartCropExtensive(t *testing.T) {
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}
rows := StringRows(data...)
rows := Rows(data...)

table := New().
Width(18).
Expand Down Expand Up @@ -775,7 +772,7 @@ func TestTableWidthSmartCropTiny(t *testing.T) {
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(StringRows(data...))
Rows(Rows(data...))

expected := strings.TrimSpace(`
β”Œ
Expand Down Expand Up @@ -811,7 +808,7 @@ func TestTableWidths(t *testing.T) {
Border(lipgloss.NormalBorder()).
BorderColumn(false).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(StringRows(rows...))
Rows(Rows(rows...))

expected := strings.TrimSpace(`
──────────────────────────────
Expand Down Expand Up @@ -847,7 +844,7 @@ func TestTableWidthShrinkNoBorders(t *testing.T) {
Border(lipgloss.NormalBorder()).
BorderColumn(false).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(StringRows(rows...))
Rows(Rows(rows...))

expected := strings.TrimSpace(`
──────────────────────────────
Expand Down

0 comments on commit 801645e

Please sign in to comment.