-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: provide model interface for table data * refactor: rename Model to Data * refactor: add `Row` method * refactor: use `Row` method * fix: examples to Data interface --------- Co-authored-by: Christian Muehlhaeuser <muesli@gmail.com>
- Loading branch information
1 parent
2687d82
commit 160eb4f
Showing
7 changed files
with
376 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
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. | ||
type StringData struct { | ||
rows []Row | ||
columns int | ||
} | ||
|
||
// Rows creates a new StringData with the given number of columns. | ||
func Rows(rows ...[]string) *StringData { | ||
m := StringData{columns: 0} | ||
|
||
for _, row := range rows { | ||
m.columns = max(m.columns, len(row)) | ||
m.rows = append(m.rows, StringRow(row)) | ||
} | ||
|
||
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] | ||
} | ||
|
||
// Columns returns the number of columns in the table. | ||
func (m *StringData) Columns() int { | ||
return m.columns | ||
} | ||
|
||
// Item appends the given row to the table. | ||
func (m *StringData) Item(rows ...string) *StringData { | ||
m.columns = max(m.columns, len(rows)) | ||
m.rows = append(m.rows, StringRow(rows)) | ||
return m | ||
} | ||
|
||
// Count returns the number of rows in the table. | ||
func (m *StringData) Count() int { | ||
return len(m.rows) | ||
} | ||
|
||
// StringRow is a simple implementation of the Row interface. | ||
type StringRow []string | ||
|
||
// Value returns the value of the column at the given index. | ||
func (r StringRow) Column(col int) string { | ||
if col >= len(r) { | ||
return "" | ||
} | ||
|
||
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 | ||
filter func(row Row) bool | ||
} | ||
|
||
// NewFilter initializes a new Filter. | ||
func NewFilter(data Data) *Filter { | ||
return &Filter{data: data} | ||
} | ||
|
||
// Filter applies the given filter function to the data. | ||
func (m *Filter) Filter(f func(row Row) bool) *Filter { | ||
m.filter = f | ||
return m | ||
} | ||
|
||
// Row returns the row at the given index. | ||
func (m *Filter) Row(row int) Row { | ||
j := 0 | ||
for i := 0; i < m.data.Count(); i++ { | ||
if m.filter(m.data.Row(i)) { | ||
if j == row { | ||
return m.data.Row(i) | ||
} | ||
|
||
j++ | ||
} | ||
} | ||
|
||
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() | ||
} | ||
|
||
// Count returns the number of rows in the table. | ||
func (m *Filter) Count() int { | ||
j := 0 | ||
for i := 0; i < m.data.Count(); i++ { | ||
if m.filter(m.data.Row(i)) { | ||
j++ | ||
} | ||
} | ||
|
||
return j | ||
} |
Oops, something went wrong.