From 5dd360549610b7dcb3ef9abcd513551ad007628b Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 9 Apr 2020 22:18:31 -0400 Subject: [PATCH] Helper function for navigating sets + comments --- pager/pager.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pager/pager.go b/pager/pager.go index d2f6271..6cf8b94 100644 --- a/pager/pager.go +++ b/pager/pager.go @@ -34,9 +34,10 @@ type Model struct { UseJKKeys bool } -// SetTotalPages is a helper method for calculatng the total number of pages -// from a given number of items. It's use is optional. Note that it both -// returns the number of total pages and alters the model. +// SetTotalPages is a helper function for calculatng the total number of pages +// from a given number of items. It's use is optional since this pager can be +// used for other things beyond navigating sets. Note that it both returns the +// number of total pages and alters the model. func (m *Model) SetTotalPages(items int) int { if items == 0 { return 0 @@ -49,6 +50,13 @@ func (m *Model) SetTotalPages(items int) int { return n } +// ItemsOnPage is a helper function fro returning the numer of items on the +// current page given the total numer of items passed as an argument. +func (m Model) ItemsOnPage(totalItems int) int { + start, end := m.GetSliceBounds(totalItems) + return end - start +} + // GetSliceBounds is a helper function for paginating slices. Pass the length // of the slice you're rendering and you'll receive the start and end bounds // corresponding the to pagination. For example: @@ -63,12 +71,16 @@ func (m *Model) GetSliceBounds(length int) (start int, end int) { return start, end } +// PrevPage is a number function for navigating one page backward. It will not +// page beyond the first page (i.e. page 0). func (m *Model) PrevPage() { if m.Page > 0 { m.Page-- } } +// NextPage is a helper function for navigating one page forward. It will not +// page beyond the last page (i.e. totalPages - 1). func (m *Model) NextPage() { if m.Page < m.TotalPages-1 { m.Page++