-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.go
117 lines (98 loc) · 3.08 KB
/
pagination.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
package pagination
import (
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type PageLabel string
const (
FirstPageLabel PageLabel = `« {}`
PreviousPageLabel PageLabel = `‹ {}`
NextPageLabel PageLabel = `{} ›`
LastPageLabel PageLabel = `{} »`
CurrentPageLabel PageLabel = `·{}·`
)
func (l PageLabel) Page(page int) string {
return strings.Replace(string(l), "{}", strconv.Itoa(page), 1)
}
type InlineKeyboardPaginator struct {
page int
all int
data string
}
func NewInlineKeyboardPaginator(page, all int, data string) []tgbotapi.InlineKeyboardButton {
if page < 1 {
page = 1
}
if all < 1 {
all = 1
}
if len(data) == 0 {
data = "{page}"
}
return (&InlineKeyboardPaginator{
page: page,
all: all,
data: data,
}).buttons()
}
func (p *InlineKeyboardPaginator) buttons() []tgbotapi.InlineKeyboardButton {
if p.all == 1 {
return nil
} else if p.all <= 5 {
return p.lessKeyboard()
} else if p.page <= 3 {
return p.startKeyboard()
} else if p.page > p.all-3 {
return p.finishKeyboard()
} else {
return p.middleKeyboard()
}
}
func (p *InlineKeyboardPaginator) lessKeyboard() []tgbotapi.InlineKeyboardButton {
keyboardDict := make([]tgbotapi.InlineKeyboardButton, 0, p.all)
for page := 1; page <= p.all; page++ {
keyboardDict = append(keyboardDict, p.isCurrentKeyboard(page))
}
return keyboardDict
}
func (p *InlineKeyboardPaginator) startKeyboard() []tgbotapi.InlineKeyboardButton {
keyboardDict := make([]tgbotapi.InlineKeyboardButton, 0, 5)
for page := 1; page <= 3; page++ {
keyboardDict = append(keyboardDict, p.isCurrentKeyboard(page))
}
keyboardDict = append(keyboardDict, p.btnText(NextPageLabel.Page(4), 4))
keyboardDict = append(keyboardDict, p.btnText(LastPageLabel.Page(p.all), p.all))
return keyboardDict
}
func (p *InlineKeyboardPaginator) middleKeyboard() []tgbotapi.InlineKeyboardButton {
return []tgbotapi.InlineKeyboardButton{
p.btnText(FirstPageLabel.Page(1), 1),
p.btnText(PreviousPageLabel.Page(p.page-1), p.page-1),
p.btnText(CurrentPageLabel.Page(p.page), p.page),
p.btnText(NextPageLabel.Page(p.page+1), p.page+1),
p.btnText(LastPageLabel.Page(p.all), p.all),
}
}
func (p *InlineKeyboardPaginator) finishKeyboard() []tgbotapi.InlineKeyboardButton {
keyboardDict := make([]tgbotapi.InlineKeyboardButton, 0, 5)
keyboardDict = append(keyboardDict,
p.btnText(FirstPageLabel.Page(1), 1),
p.btnText(PreviousPageLabel.Page(p.all-3), p.all-3))
for i := 3; i <= 5; i++ {
keyboardDict = append(keyboardDict, p.isCurrentKeyboard(p.all-5+i))
}
return keyboardDict
}
func (p *InlineKeyboardPaginator) isCurrentKeyboard(page int) tgbotapi.InlineKeyboardButton {
if page == p.page {
return p.btnText(CurrentPageLabel.Page(page), page)
}
return p.btn(page)
}
func (p *InlineKeyboardPaginator) btn(page int) tgbotapi.InlineKeyboardButton {
return p.btnText(strconv.Itoa(page), page)
}
func (p *InlineKeyboardPaginator) btnText(text string, page int) tgbotapi.InlineKeyboardButton {
return tgbotapi.NewInlineKeyboardButtonData(text, strings.ReplaceAll(p.data, "{page}", strconv.Itoa(page)))
}