-
Notifications
You must be signed in to change notification settings - Fork 4
/
pagination.go
159 lines (119 loc) · 2.88 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package lib
import (
"errors"
"fmt"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/interactive"
"github.com/spf13/cobra"
)
const (
PaginationQuit = -1
PaginationOther = -2
ShowOtherMinPages = 4
)
var errQuit = errors.New("quit")
type ModelWithPagination interface {
GetPage() int32
GetItemsPerPage() int32
GetTotalItems() int32
}
type Options interface {
SetPage(int32)
}
type CollectionGenerator func() (ModelWithPagination, error)
func ShowCollection(cmd *cobra.Command, options Options, generator CollectionGenerator) error {
var page int32
for {
model, err := generator()
if err != nil {
return err
}
if err = FormatCommandData(cmd, model); err != nil {
return err
}
page, err = interactivePagination(cmd, model)
if err != nil {
if errors.Is(err, errQuit) {
return nil
}
return err
}
options.SetPage(page)
}
}
func interactivePagination(cmd *cobra.Command, model ModelWithPagination) (int32, error) {
if !config.GetSettings().IsStylish() {
return 0, errQuit
}
if config.GetSettings().NonInteractive {
return 0, errQuit
}
navPage, err := ProcessPagination(cmd, model)
if err != nil {
return 0, err
}
if navPage == PaginationQuit {
return 0, errQuit
}
return navPage, nil
}
func ProcessPagination(cmd *cobra.Command, m ModelWithPagination) (int32, error) {
totalItems := m.GetTotalItems()
if totalItems == 0 {
return PaginationQuit, nil
}
page := m.GetPage()
pages := 1 + (m.GetTotalItems()-1)/m.GetItemsPerPage()
if page > pages {
return PaginationQuit, nil
}
if pages == 1 {
return PaginationQuit, nil
}
nav, pageNo := getPaginationOptions(page, pages)
index, _, err := interactive.Choose("Navigate to a different page?", nav)
if err != nil {
return PaginationQuit, err
}
target := pageNo[index]
if target == PaginationOther {
target, err = interactive.AskInt32("Go to page:", interactive.AssertBetween(1, pages))
if err != nil {
return PaginationQuit, err
}
}
return target, nil
}
func getPaginationOptions(page int32, pages int32) ([]string, []int32) {
nav := []string{}
pageNo := []int32{}
var (
firstPage int32 = 1
lastPage = pages
)
if page != firstPage {
prevPage := page - 1
if prevPage != firstPage {
nav = append(nav, fmt.Sprintf("First (%d)", firstPage))
pageNo = append(pageNo, firstPage)
}
nav = append(nav, fmt.Sprintf("Prev (%d)", prevPage))
pageNo = append(pageNo, prevPage)
}
if page != lastPage {
nextPage := page + 1
nav = append(nav, fmt.Sprintf("Next (%d)", nextPage))
pageNo = append(pageNo, nextPage)
if nextPage != lastPage {
nav = append(nav, fmt.Sprintf("Last (%d)", lastPage))
pageNo = append(pageNo, lastPage)
}
}
if pages > ShowOtherMinPages {
nav = append(nav, "Other")
pageNo = append(pageNo, PaginationOther)
}
nav = append(nav, "Quit")
pageNo = append(pageNo, PaginationQuit)
return nav, pageNo
}