-
Notifications
You must be signed in to change notification settings - Fork 4
/
wizard.pagination.go
56 lines (40 loc) · 1.13 KB
/
wizard.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
package wizard
import (
"bunnyshell.com/cli/pkg/interactive"
)
// @see lib/pagination.go
type Pagination interface {
GetPage() int32
GetItemsPerPage() int32
GetTotalItems() int32
}
func getPaginationInfo(pagination Pagination) (int32, int32) {
page := pagination.GetPage()
pages := 1 + (pagination.GetTotalItems()-1)/pagination.GetItemsPerPage()
return page, pages
}
func addPagination(items []string, page int32, pages int32) []string {
if page > pages {
return items
}
if pages == 1 {
return items
}
items = append(items, paginationOptions[selectPage])
return items
}
func chooseOrNavigate(question string, items []string, page int32, pages int32) (*int, *int32, error) {
selector := addPagination(items, page, pages)
index, answer, err := interactive.ChooseWithSize(paginationDisplaySize, question, selector)
if err != nil {
return nil, nil, err
}
if index >= len(items) {
if answer == paginationOptions[selectPage] {
page, err = interactive.AskInt32("Select a page", interactive.AssertBetween(1, pages))
return nil, &page, err
}
return nil, nil, interactive.ErrInvalidValue
}
return &index, nil, nil
}