paginator

package
v0.16.423 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package paginator provides a Bubble Tea package for calculating 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.

Index

Constants

This section is empty.

Variables

View Source
var DefaultKeyMap = KeyMap{
	PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")),
	NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")),
}

DefaultKeyMap is the default set of key bindings for navigating and acting upon the paginator.

View Source
var NewModel = New

NewModel creates a new model with defaults.

Deprecated: use New instead.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	PrevPage key.Binding
	NextPage key.Binding
}

KeyMap is the key bindings for different actions within the paginator.

type Model

type Model struct {
	// Type configures how the pagination is rendered (Arabic, Dots).
	Type Type
	// Page is the current page number.
	Page int
	// PerPage is the number of items per page.
	PerPage int
	// TotalPages is the total number of pages.
	TotalPages int
	// ActiveDot is used to mark the current page under the Dots display type.
	ActiveDot string
	// InactiveDot is used to mark inactive pages under the Dots display type.
	InactiveDot string
	// ArabicFormat is the printf-style format to use for the Arabic display type.
	ArabicFormat string

	// KeyMap encodes the keybindings recognized by the widget.
	KeyMap KeyMap

	// Deprecated: customize [KeyMap] instead.
	UsePgUpPgDownKeys bool
	// Deprecated: customize [KeyMap] instead.
	UseLeftRightKeys bool
	// Deprecated: customize [KeyMap] instead.
	UseUpDownKeys bool
	// Deprecated: customize [KeyMap] instead.
	UseHLKeys bool
	// Deprecated: customize [KeyMap] instead.
	UseJKKeys bool
}

Model is the Bubble Tea model for this user interface.

func New

func New() Model

New creates a new model with defaults.

func (*Model) GetSliceBounds

func (m *Model) GetSliceBounds(length int) (start int, end int)

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 to the pagination. For example:

bunchOfStuff := []stuff{...}
start, end := model.GetSliceBounds(len(bunchOfStuff))
sliceToRender := bunchOfStuff[start:end]

func (Model) ItemsOnPage

func (m Model) ItemsOnPage(totalItems int) int

ItemsOnPage is a helper function for returning the number of items on the current page given the total number of items passed as an argument.

func (*Model) NextPage

func (m *Model) NextPage()

NextPage is a helper function for navigating one page forward. It will not page beyond the last page (i.e. totalPages - 1).

func (Model) OnLastPage

func (m Model) OnLastPage() bool

OnLastPage returns whether or not we're on the last page.

func (*Model) PrevPage

func (m *Model) PrevPage()

PrevPage is a helper function for navigating one page backward. It will not page beyond the first page (i.e. page 0).

func (*Model) SetTotalPages

func (m *Model) SetTotalPages(items int) int

SetTotalPages is a helper function for calculating the total number of pages from a given number of items. Its 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 (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update is the Tea update function which binds keystrokes to pagination.

func (Model) View

func (m Model) View() string

View renders the pagination to a string.

type Type

type Type int

Type specifies the way we render pagination.

const (
	Arabic Type = iota
	Dots
)

Pagination rendering options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL