pagination

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pagination is a row of page numbers with the current one marked: Left and Right go to the page before and after, Home and End to the first and last, and with the mouse on a click on a number goes to that page. Pages far from the current one are folded into an ellipsis, so the row stays short however many pages there are.

Stability: experimental. Its API may change in any minor release.

Example

Right goes to the next page; far pages fold into an ellipsis.

package main

import (
	"fmt"
	"strings"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/pagination"
)

func main() {
	pages := pagination.New(20)
	pages.SetPage(9)
	pages.Focus()
	pages, _ = pages.Update(tui.Key{Type: tui.KeyRight})
	// The row has a blank cell at each end, trimmed here for the comparison.
	fmt.Println(strings.TrimSpace(ansi.StripANSI(pages.View())))
	fmt.Println(pages.Page())
}
Output:
1  …  9 <10> 11  …  20
10

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangedMsg

type ChangedMsg struct {
	// ID is the pager's ID.
	ID string
	// Page is the new page, counted from 1.
	Page int
}

ChangedMsg is delivered (via the Cmd Update returns) when the page changes.

type KeyMap

type KeyMap struct {
	Prev  keymap.Binding // go to the page before
	Next  keymap.Binding // go to the page after
	First keymap.Binding // go to the first page
	Last  keymap.Binding // go to the last page
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Left and Right, Home and End.

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one pager's change from
	// another's.
	ID string
	// Total is the number of pages. With a Total under 1 there is nothing
	// to draw.
	Total int
	// Siblings is how many pages are shown on each side of the current one,
	// besides the first and the last, which are always shown. A negative
	// Siblings is read as 0.
	Siblings int
	// Disabled stops the page being changed; the pager is drawn faint.
	Disabled bool
	Theme    theme.Theme

	// KeyMap holds the keys for each action. New fills it with DefaultKeyMap;
	// a Model built as a struct literal with a zero KeyMap behaves as if it
	// held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
	// a page number goes to that page. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the pager.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is one pager. The zero value is not ready to use; build one with New.

func New

func New(total int) Model

New returns a pager over total pages, on page 1, showing one page either side of the current one.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the pager honours, with descriptions, for help text. A disabled pager has none.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

Focus gives the pager keyboard focus. It returns no Cmd; the result is there so a pager can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the pager has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the widget to a layout.Node of its own width on one row. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the pager as plain text for accessible output (see tui.Linearizer): "page 5 of 20", with ", unavailable" when disabled, and "no pages" when there are none.

func (Model) Page

func (m Model) Page() int

Page returns the current page, counted from 1 and never past Total. It is 0 when there are no pages.

func (*Model) SetPage

func (m *Model) SetPage(p int)

SetPage goes to page p, held between 1 and Total, without a key press: no ChangedMsg is delivered.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentPagination, ...) and then by WithTokens.

func (Model) Update

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

Update changes the page on Left, Right, Home and End when the pager has focus, and, with Mouse on, on a left press on a page number. A disabled pager ignores both.

func (Model) View

func (m Model) View() string

View renders the page numbers on one row, each between two cells. The current page is in square brackets, "[5]", so it reads without colour, and in angle brackets, "<5>", when the pager has focus. A disabled pager is faint. With no pages it renders "".

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes. It changes with the page, as numbers gain digits and ellipses come and go.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

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