Helper function for navigating sets + comments

This commit is contained in:
Christian Rocha 2020-04-09 22:18:31 -04:00
parent fc64be59d6
commit 5dd3605496
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -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++