Skip to content

Commit

Permalink
Add option to toggle whether pagination wraps (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Jun 24, 2022
1 parent 3802e8a commit 5cbb890
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
7 changes: 5 additions & 2 deletions table/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ type Model struct {
staticFooter string

// Pagination
pageSize int
currentPage int
pageSize int
currentPage int
paginationWrapping bool

// Sorting, where a stable sort is applied from first element to last so
// that elements are grouped by the later elements.
Expand Down Expand Up @@ -96,6 +97,8 @@ func New(columns []Column) Model {

filterTextInput: filterInput,
baseStyle: lipgloss.NewStyle().Align(lipgloss.Right),

paginationWrapping: true,
}

// Do a full deep copy to avoid unexpected edits
Expand Down
10 changes: 9 additions & 1 deletion table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,21 @@ func (m Model) WithPageSize(pageSize int) Model {
return m
}

// WithNoPagination disable pagination in the table.
// WithNoPagination disables pagination in the table.
func (m Model) WithNoPagination() Model {
m.pageSize = 0

return m
}

// WithPaginationWrapping sets whether to wrap around from the beginning to the
// end when navigating through pages. Defaults to true.
func (m Model) WithPaginationWrapping(wrapping bool) Model {
m.paginationWrapping = wrapping

return m
}

// WithSelectedText describes what text to show when selectable rows are enabled.
// The selectable column header will use the selected text string.
func (m Model) WithSelectedText(unselected, selected string) Model {
Expand Down
12 changes: 10 additions & 2 deletions table/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func (m *Model) pageDown() {
maxPageIndex := m.MaxPages() - 1

if m.currentPage > maxPageIndex {
m.currentPage = 0
if m.paginationWrapping {
m.currentPage = 0
} else {
m.currentPage = maxPageIndex
}
}

m.rowCursorIndex = m.currentPage * m.pageSize
Expand All @@ -75,7 +79,11 @@ func (m *Model) pageUp() {
maxPageIndex := m.MaxPages() - 1

if m.currentPage < 0 {
m.currentPage = maxPageIndex
if m.paginationWrapping {
m.currentPage = maxPageIndex
} else {
m.currentPage = 0
}
}

m.rowCursorIndex = m.currentPage * m.pageSize
Expand Down
7 changes: 7 additions & 0 deletions table/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ func (m *Model) GetHorizontalScrollColumnOffset() int {
func (m *Model) GetHeaderVisibility() bool {
return m.headerVisible
}

// GetPaginationWrapping returns true if pagination wrapping is enabled, or false
// if disabled. If disabled, navigating through pages will stop at the first
// and last pages.
func (m *Model) GetPaginationWrapping() bool {
return m.paginationWrapping
}
10 changes: 10 additions & 0 deletions table/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,13 @@ func TestGetHeaderVisibility(t *testing.T) {

assert.False(t, model.GetHeaderVisibility(), "Header was not set to hidden")
}

func TestGetPaginationWrapping(t *testing.T) {
model := New([]Column{})

assert.True(t, model.GetPaginationWrapping(), "Pagination wrapping should default to true")

model = model.WithPaginationWrapping(false)

assert.False(t, model.GetPaginationWrapping(), "Pagination wrapping setting did not update after setting option")
}
9 changes: 9 additions & 0 deletions table/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ func TestFocusedMovesWhenMoveKeysPressedPaged(t *testing.T) {

model, _ = model.Update(keyEnd)
assert.Equal(t, "third", curID(), "Hitting end a second time should not move pages")

// Disable pagination wrapping and ensure it sticks
model = model.WithPaginationWrapping(false)
model, _ = model.Update(keyRight)
assert.Equal(t, "third", curID(), "Did not stay on last page, may have wrapped")

model, _ = model.Update(keyHome)
model, _ = model.Update(keyLeft)
assert.Equal(t, "first", curID(), "Did not stay on first page, may have wrapped")
}

func TestFocusedMovesWithCustomKeyMap(t *testing.T) {
Expand Down

0 comments on commit 5cbb890

Please sign in to comment.