8000 feat: Introduce paginator options · charmbracelet/bubbles@5110925 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5110925

Browse files
nervomeowgorithm
authored andcommitted
feat: Introduce paginator options
1 parent f25096d commit 5110925

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

paginator/paginator.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ func (m Model) OnFirstPage() bool {
130130
return m.Page == 0
131131
}
132132

133+
// Option is used to set options in New.
134+
type Option func(*Model)
135+
133136
// New creates a new model with defaults.
134-
func New() Model {
135-
return Model{
137+
func New(opts ...Option) Mo 8000 del {
138+
m := Model{
136139
Type: Arabic,
137140
Page: 0,
138141
PerPage: 1,
@@ -142,13 +145,33 @@ func New() Model {
142145
InactiveDot: "○",
143146
ArabicFormat: "%d/%d",
144147
}
148+
149+
for _, opt := range opts {
150+
opt(&m)
151+
}
152+
153+
return m
145154
}
146155

147156
// NewModel creates a new model with defaults.
148157
//
149158
// Deprecated: use [New] instead.
150159
var NewModel = New
151160

161+
// WithTotalPages sets the total pages.
162+
func WithTotalPages(totalPages int) Option {
163+
return func(m *Model) {
164+
m.TotalPages = totalPages
165+
}
166+
}
167+
168+
// WithPerPage sets the total pages.
169+
func WithPerPage(perPage int) Option {
170+
return func(m *Model) {
171+
m.PerPage = perPage
172+
}
173+
}
174+
152175
// Update is the Tea update function which binds keystrokes to pagination.
153176
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
154177
switch msg := msg.(type) {

paginator/paginator_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ import (
66
tea "github.com/charmbracelet/bubbletea"
77
)
88

9+
func TestNew(t *testing.T) {
10+
model := New()
11+
12+
if model.PerPage != 1 {
13+
t.Errorf("PerPage = %d, expected %d", model.PerPage, 1)
14+
}
15+
if model.TotalPages != 1 {
16+
t.Errorf("TotalPages = %d, expected %d", model.TotalPages, 1)
17+
}
18+
19+
perPage := 42
20+
totalPages := 42
21+
22+
model = New(
23+
WithPerPage(perPage),
24+
WithTotalPages(totalPages),
25+
)
26+
27+
if model.PerPage != perPage {
28+
t.Errorf("PerPage = %d, expected %d", model.PerPage, perPage)
29+
}
30+
if model.TotalPages != totalPages {
31+
t.Errorf("TotalPages = %d, expected %d", model.TotalPages, totalPages)
32+
}
33+
}
34+
935
func TestSetTotalPages(t *testing.T) {
1036
tests := []struct {
1137
name string

0 commit comments

Comments
 (0)
0