-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager.go
125 lines (102 loc) · 2.9 KB
/
pager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package pager
// Pager allows paging without having to deal
// with the data that is to be paged.
type Pager interface {
// Next selects the next item. Returns wether the selected item has changed.
Next() (changed bool)
// Prev selects the previous item. Returns wether the selected item has changed.
Prev() (changed bool)
// PageDown selects the next page. Returns wether the selected item has changed.
PageDown() (changed bool)
// PageUp selects the previous page. Returns wether the selected item has changed.
PageUp() (changed bool)
// Indexes returns the from, to and selected index.
// To get the current data, use data[from:to].
// The seleceted index is the position within data[from:to],
// so the position within data would be from+selected.
// If selected is -1, there is no selection.
// If from is -1, there is no data to be shown.
Indexes() (from, to, selected int)
}
type pager struct {
dataLen, selected, height int
dataLenDivHeight int
style func(*pager) (from, to, selected int)
}
// New creates a new pager.
// Each time the height or dataLen changes, a new pager should be created.
func New(height, dataLen int, opts ...Option) Pager {
p := &pager{height: height, dataLen: dataLen}
p.dataLenDivHeight = dataLen / height
for _, opt := range opts {
opt(p)
}
if p.style == nil {
FixPage()(p)
}
if dataLen == 0 {
p.selected = -1
}
return p
}
// Next selects the next item. Returns wether the selected item has changed.
func (p *pager) Next() (changed bool) {
if p.dataLen-1 > p.selected {
p.selected++
changed = true
}
return
}
// Prev selects the previous item. Returns wether the selected item has changed.
func (p *pager) Prev() (changed bool) {
if p.selected > 0 {
p.selected--
changed = true
}
return
}
// PageDown selects the next page. Returns wether the selected item has changed.
func (p *pager) PageDown() (changed bool) {
if p.dataLen == 0 {
return
}
page := p.currentPage()
if page >= p.dataLenDivHeight {
return
}
page++
p.selected = (page+1)*p.height - 1
changed = true
if p.selected > p.dataLen-1 {
p.selected = p.dataLen - 1
}
return
}
// PageUp selects the previous page. Returns wether the selected item has changed.
func (p *pager) PageUp() (changed bool) {
if p.currentPage() == 0 {
return
}
p.selected -= p.height
changed = true
return
}
// Indexes returns the from, to and selected index.
// To get the current data, use data[from:to].
// The seleceted index is the position within data[from:to],
// so the position within data would be from+selected.
// If selected is -1, there is no selection.
// If from is -1, there is no data to be shown.
func (p *pager) Indexes() (from, to, selected int) {
if p.dataLen == 0 || p.selected > p.dataLen-1 {
return -1, -1, -1
}
return p.style(p)
}
func (p *pager) currentPage() (page int) {
if p.selected < 0 {
return
}
page = p.selected / p.height
return
}