From 0d920d08fea6840e2597692ae84850137f837db1 Mon Sep 17 00:00:00 2001 From: Brandon Fulljames Date: Wed, 23 Feb 2022 18:24:33 +0900 Subject: [PATCH] Some extra sort docs for clarity (#28) --- examples/sorting/main.go | 18 ++++++++++++++---- table/model_test.go | 15 +++++++++++++++ table/sort.go | 12 ++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 table/model_test.go diff --git a/examples/sorting/main.go b/examples/sorting/main.go index d115455..5a065e9 100644 --- a/examples/sorting/main.go +++ b/examples/sorting/main.go @@ -11,7 +11,7 @@ import ( const ( columnKeyName = "name" columnKeyType = "type" - columnKeyWins = "element" + columnKeyWins = "wins" ) type Model struct { @@ -34,15 +34,25 @@ func NewModel() Model { columnKeyWins: 4, }), table.NewRow(table.RowData{ - columnKeyName: "Alphonse", + columnKeyName: "Zapmouse", columnKeyType: "Pikachu", - columnKeyWins: 13, + columnKeyWins: 3, }), table.NewRow(table.RowData{ columnKeyName: "Burninator", columnKeyType: "Charmander", columnKeyWins: 8, }), + table.NewRow(table.RowData{ + columnKeyName: "Alphonse", + columnKeyType: "Pikachu", + columnKeyWins: 13, + }), + table.NewRow(table.RowData{ + columnKeyName: "Trogdor", + columnKeyType: "Charmander", + columnKeyWins: 9, + }), table.NewRow(table.RowData{ columnKeyName: "Dihydrogen Monoxide", columnKeyType: "Squirtle", @@ -92,7 +102,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) View() string { body := strings.Builder{} - body.WriteString("A sorted simple default table\nSort by (n)ame, (t)ype, or (w)ins\nCurrently sorting by: " + m.columnSortKey + "\nPress q or ctrl+c to quit\n\n") + body.WriteString("A sorted simple default table\nSort by (n)ame, (t)ype->wins combo, or (w)ins\nCurrently sorting by: " + m.columnSortKey + "\nPress q or ctrl+c to quit\n\n") body.WriteString(m.simpleTable.View()) diff --git a/table/model_test.go b/table/model_test.go new file mode 100644 index 0000000..531526e --- /dev/null +++ b/table/model_test.go @@ -0,0 +1,15 @@ +package table + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestModelInitReturnsNil(t *testing.T) { + model := New(nil) + + cmd := model.Init() + + assert.Nil(t, cmd) +} diff --git a/table/sort.go b/table/sort.go index 4ffebae..88c5be0 100644 --- a/table/sort.go +++ b/table/sort.go @@ -17,6 +17,10 @@ type sortColumn struct { direction sortDirection } +// SortByAsc sets the main sorting column to the given key, in ascending order. +// If a previous sort was used, it is replaced by the given column each time +// this function is called. Values are sorted as numbers if possible, or just +// as simple string comparisons if not numbers. func (m Model) SortByAsc(columnKey string) Model { m.sortOrder = []sortColumn{ { @@ -30,6 +34,10 @@ func (m Model) SortByAsc(columnKey string) Model { return m } +// SortByDesc sets the main sorting column to the given key, in descending order. +// If a previous sort was used, it is replaced by the given column each time +// this function is called. Values are sorted as numbers if possible, or just +// as simple string comparisons if not numbers. func (m Model) SortByDesc(columnKey string) Model { m.sortOrder = []sortColumn{ { @@ -43,6 +51,8 @@ func (m Model) SortByDesc(columnKey string) Model { return m } +// ThenSortByAsc provides a secondary sort after the first, in ascending order. +// Can be chained multiple times, applying to smaller subgroups each time. func (m Model) ThenSortByAsc(columnKey string) Model { m.sortOrder = append([]sortColumn{ { @@ -56,6 +66,8 @@ func (m Model) ThenSortByAsc(columnKey string) Model { return m } +// ThenSortByDesc provides a secondary sort after the first, in descending order. +// Can be chained multiple times, applying to smaller subgroups each time. func (m Model) ThenSortByDesc(columnKey string) Model { m.sortOrder = append([]sortColumn{ {