Helper function for paginating slices

This commit is contained in:
Christian Rocha 2020-04-09 18:21:17 -04:00
parent 2953608151
commit 363c2522f2
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -49,6 +49,20 @@ func (m *Model) SetTotalPages(items int) int {
return n
}
// 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:
//
// bunchOfStuff := []stuff{...}
// start, end := model.GetSliceBounds(len(bunchOfStuff))
// sliceToRender := bunchOfStuff[start:end]
//
func (m *Model) GetSliceBounds(length int) (start int, end int) {
start = m.Page * m.PerPage
end = min(m.Page*m.PerPage+m.PerPage, length)
return start, end
}
func (m *Model) prevPage() {
if m.Page > 0 {
m.Page--
@ -148,3 +162,10 @@ func dotsView(m Model) string {
func arabicView(m Model) string {
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}