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 ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) Page() int
- func (m *Model) SetPage(p int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) Width() int
- func (m Model) WithTokens(tok theme.Tokens) Model
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 ¶
New returns a pager over total pages, on page 1, showing one page either side of the current one.
func (Model) Bindings ¶
Bindings returns the actions the pager honours, with descriptions, for help text. A disabled pager has none.
func (*Model) Focus ¶
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) LayoutNode ¶
LayoutNode adapts the widget to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
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 ¶
Page returns the current page, counted from 1 and never past Total. It is 0 when there are no pages.
func (*Model) SetPage ¶
SetPage goes to page p, held between 1 and Total, without a key press: no ChangedMsg is delivered.
func (Model) SetTheme ¶
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 ¶
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 ¶
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 ¶
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 "".