bubbles/pager/pager.go

172 lines
3.4 KiB
Go
Raw Normal View History

2020-04-09 22:43:55 +03:00
// package pager provides a Tea package for calulating pagination and rendering
// pagination info. Note that this package does not render actual pages: it's
// purely for handling keystrokes related to pagination, and rendering
// pagination status.
package pager
import (
"fmt"
"github.com/charmbracelet/tea"
)
// PagerType specifies the way we render pagination
type PagerType int
// Pagination rendering options
const (
Arabic PagerType = iota
Dots
2020-04-09 22:43:55 +03:00
)
// Model is the Tea model for this user interface
type Model struct {
Type PagerType
2020-04-09 23:47:12 +03:00
Page int
PerPage int
TotalPages int
ActiveDot string
InactiveDot string
ArabicFormat string
UseLeftRightKeys bool
UseUpDownKeys bool
UseHLKeys bool
UseJKKeys bool
2020-04-09 22:43:55 +03:00
}
// 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.
func (m *Model) SetTotalPages(items int) int {
if items == 0 {
return 0
}
n := items / m.PerPage
if items%m.PerPage > 0 {
n += 1
}
m.TotalPages = n
return n
}
2020-04-10 01:21:17 +03:00
// 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
}
2020-04-09 23:47:12 +03:00
func (m *Model) prevPage() {
if m.Page > 0 {
m.Page--
}
}
func (m *Model) nextPage() {
if m.Page < m.TotalPages-1 {
m.Page++
}
}
2020-04-09 22:43:55 +03:00
// NewModel creates a new model with defaults
func NewModel() Model {
return Model{
Type: Arabic,
2020-04-09 23:47:12 +03:00
Page: 0,
PerPage: 1,
TotalPages: 1,
ActiveDot: "•",
InactiveDot: "○",
ArabicFormat: "%d/%d",
UseLeftRightKeys: true,
UseUpDownKeys: false,
UseHLKeys: true,
UseJKKeys: false,
2020-04-09 22:43:55 +03:00
}
}
// Update is the Tea update function which binds keystrokes to pagination
2020-04-09 23:47:12 +03:00
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
2020-04-09 22:43:55 +03:00
switch msg := msg.(type) {
case tea.KeyMsg:
2020-04-09 23:47:12 +03:00
if m.UseLeftRightKeys {
switch msg.String() {
case "left":
m.prevPage()
case "right":
m.nextPage()
}
}
if m.UseUpDownKeys {
switch msg.String() {
case "up":
m.prevPage()
case "down":
m.nextPage()
2020-04-09 22:43:55 +03:00
}
2020-04-09 23:47:12 +03:00
}
if m.UseHLKeys {
switch msg.String() {
case "h":
m.prevPage()
case "l":
m.nextPage()
}
}
if m.UseJKKeys {
switch msg.String() {
case "j":
m.prevPage()
case "k":
m.nextPage()
2020-04-09 22:43:55 +03:00
}
}
}
2020-04-09 23:47:12 +03:00
2020-04-09 22:43:55 +03:00
return m, nil
}
// View renders the pagination to a string
func View(model tea.Model) string {
m, ok := model.(Model)
if !ok {
return ""
}
switch m.Type {
case Dots:
return dotsView(m)
default:
return arabicView(m)
}
2020-04-09 22:43:55 +03:00
}
func dotsView(m Model) string {
var s string
for i := 0; i < m.TotalPages; i++ {
if i == m.Page {
s += m.ActiveDot
continue
}
s += m.InactiveDot
}
return s
}
func arabicView(m Model) string {
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
}
2020-04-10 01:21:17 +03:00
func min(a, b int) int {
if a < b {
return a
}
return b
}