Skip to content

Commit

Permalink
Some extra sort docs for clarity (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Feb 23, 2022
1 parent 53a0074 commit 0d920d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
18 changes: 14 additions & 4 deletions examples/sorting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const (
columnKeyName = "name"
columnKeyType = "type"
columnKeyWins = "element"
columnKeyWins = "wins"
)

type Model struct {
Expand All @@ -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",
Expand Down Expand Up @@ -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())

Expand Down
15 changes: 15 additions & 0 deletions table/model_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions table/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand Down

0 comments on commit 0d920d0

Please sign in to comment.