mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-01-11 22:41:03 +03:00
Helper function for paginating slices
This commit is contained in:
parent
2953608151
commit
363c2522f2
@ -49,6 +49,20 @@ func (m *Model) SetTotalPages(items int) int {
|
|||||||
return n
|
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() {
|
func (m *Model) prevPage() {
|
||||||
if m.Page > 0 {
|
if m.Page > 0 {
|
||||||
m.Page--
|
m.Page--
|
||||||
@ -148,3 +162,10 @@ func dotsView(m Model) string {
|
|||||||
func arabicView(m Model) string {
|
func arabicView(m Model) string {
|
||||||
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user