-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
333 lines (282 loc) · 7.11 KB
/
list.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"fmt"
"log"
ui "github.com/awesome-gocui/gocui"
)
// Page is used to hold info about a list based view
type Page struct {
offset, limit int
}
// List overlads the gocui.View by implementing list specific functionalitys
type List struct {
*ui.View
code string
title string
items []string
pages []Page
pageIndex int
ordered bool
}
// CreateList initializes a List object with an existing View by applying some
// basic configuration
func CreateList(v *ui.View, ordered bool) *List {
list := &List{}
list.View = v
list.Autoscroll = true
list.ordered = ordered
return list
}
// IsEmpty indicates whether a list has items or not
func (l *List) IsEmpty() bool {
return l.length() == 0
}
// Focus hightlights the View of the current List
func (l *List) Focus(g *ui.Gui) {
l.Highlight = true
l.SelFgColor = ui.ColorBlack
l.SelBgColor = ui.ColorGreen
l.FrameColor = ui.ColorGreen
l.TitleColor = ui.ColorGreen
_, err := g.SetCurrentView(l.Name())
if err != nil {
log.Panicln("Error on SetCurrentView", err)
}
}
// Unfocus is used to remove highlighting from the current list
func (l *List) Unfocus() {
l.FrameColor = ui.ColorDefault
l.TitleColor = ui.ColorDefault
l.Highlight = false
}
// Reset zeros the list's slices out and clears the underlying View
func (l *List) Reset() {
l.items = make([]string, 0)
l.pages = []Page{}
l.Clear()
l.ResetCursor()
}
// Change the project code means old data will be gone
func (l *List) SetCode(code string) {
// only do if code is new
if l.code != code {
l.code = code
l.Reset()
}
}
// SetTitle will set the title of the View and display paging information of the
// list if there are more than one pages
func (l *List) SetTitle(title string) {
l.title = title
if l.pagesNum() > 1 {
l.Title = fmt.Sprintf("%d/%d - %s", l.currPageNum(), l.pagesNum(), title)
} else {
l.Title = title
}
}
// SetItems will (re)evaluates the list's items with the given data and redraws
// the View
func (l *List) SetItems(data []string) {
l.items = data
l.ResetPages()
err := l.Draw()
if err != nil {
log.Panicln("Error on SetItems", err)
}
}
// AddItem appends a given item to the existing list
func (l *List) AddItem(g *ui.Gui, item string) {
l.items = append(l.items, item)
l.ResetPages()
if err := l.Draw(); err != nil {
log.Panicln("Error on AddItem", err)
}
}
func (l *List) UpdateCurrentItem(item string) {
page := l.currPage()
data := l.items[page.offset : page.offset+page.limit]
data[l.currentCursorY()] = item
}
// Draw calculates the pages and draws the first one
func (l *List) Draw() error {
if l.IsEmpty() {
return nil
}
return l.displayPage(0)
}
// Draw calculates the pages and draws the first one
func (l *List) DrawCurrentPage() error {
if l.IsEmpty() {
return nil
}
return l.displayPage(l.pageIndex)
}
// MoveDown moves the cursor to the line below or the next page if any
func (l *List) MoveDown() error {
if l.IsEmpty() {
return nil
}
y := l.currentCursorY() + 1
if l.atBottomOfPage() {
y = 0
if l.hasMultiplePages() {
return l.displayPage(l.nextPageIdx())
}
}
err := l.SetCursor(0, y)
if err != nil {
return err
}
return nil
}
// MoveUp moves the cursor to the line above on the previous page if any
func (l *List) MoveUp() error {
if l.IsEmpty() {
return nil
}
y := l.currentCursorY() - 1
if l.atTopOfPage() {
y = l.pages[l.prevPageIdx()].limit - 1
if l.hasMultiplePages() {
return l.displayPage(l.prevPageIdx())
}
}
err := l.SetCursor(0, y)
if err != nil {
return err
}
return nil
}
// MovePgDown displays the next page
func (l *List) MovePgDown() error {
if l.IsEmpty() {
return nil
}
err := l.displayPage(l.nextPageIdx())
if err != nil {
return err
}
return l.SetCursor(0, 0)
}
// MovePgUp displays the previous page
func (l *List) MovePgUp() error {
if l.IsEmpty() {
return nil
}
err := l.displayPage(l.prevPageIdx())
if err != nil {
log.Panicln(err)
}
return l.SetCursor(0, 0)
}
// CurrentItem returns the currently selected item of the list no matter what
// page is being displayed
func (l *List) CurrentItem() string {
if l.IsEmpty() {
return ""
}
page := l.currPage()
data := l.items[page.offset : page.offset+page.limit]
return data[l.currentCursorY()]
}
// ResetCursor puts the cirson back at the beginning of the View
func (l *List) ResetCursor() {
err := l.SetCursor(0, 0)
if err != nil {
log.Panicln("Error in ResetCursor", err)
}
}
// ResetPages (re)calculates the pages data based on the current length of the
// list and the current height of the View
func (l *List) ResetPages() {
l.pages = []Page{}
for offset := 0; offset < l.length(); offset += l.height() {
limit := l.height()
if offset+limit > l.length() {
limit = l.length() % l.height()
}
l.pages = append(l.pages, Page{offset, limit})
}
}
// currPageNum returns the current page number to display
func (l *List) currPageNum() int {
if l.IsEmpty() {
return 0
}
return l.pageIndex + 1
}
// currentCursorY returns the current Y of the cursor
func (l *List) currentCursorY() int {
_, y := l.Cursor()
return y
}
// currPage returns the current page being displayd
func (l *List) currPage() Page {
return l.pages[l.pageIndex]
}
// height ewturns the current height of the View
func (l *List) height() int {
_, y := l.Size()
return y - 1
}
// width ewturns the current width of the View
func (l *List) width() int {
x, _ := l.Size()
return x - 1
}
// length returns the length of the list
func (l *List) length() int {
return len(l.items)
}
// pageNum returns the number of the pages
func (l *List) pagesNum() int {
return len(l.pages)
}
// nextPageIdx returns the index of the next page to be displayed circularlt
func (l *List) nextPageIdx() int {
return (l.pageIndex + 1) % l.pagesNum()
}
// prevPageIdx returns the index of the prev page to be displayed circularlt
func (l *List) prevPageIdx() int {
pidx := (l.pageIndex - 1) % l.pagesNum()
if l.pageIndex == 0 {
pidx = l.pagesNum() - 1
}
return pidx
}
// sidplayItem displays the text of the item with index i and fills with spaces
// the remaining space until the border of the View
func (l *List) displayItem(i int) string {
item := fmt.Sprint(l.items[i])
sp := spaces(l.width() - len(item) + 1)
if l.ordered {
return fmt.Sprintf("%2d. %v%s", i+1, item, sp)
} else {
return fmt.Sprintf("%s%s", item, sp)
}
}
// displayPage resets the currentPageIdx and displays the requested page
func (l *List) displayPage(p int) error {
l.Clear()
l.pageIndex = p
page := l.pages[l.pageIndex]
for i := page.offset; i < page.offset+page.limit; i++ {
if _, err := fmt.Fprintln(l.View, l.displayItem(i)); err != nil {
return err
}
}
l.SetTitle(l.title)
return nil
}
// atBottomOfPage determines whether the cursor is at the bottom of the current page
func (l *List) atBottomOfPage() bool {
return l.currentCursorY() == l.currPage().limit-1
}
// atTopOfPage determines whether the cursor is at the top of the current page
func (l *List) atTopOfPage() bool {
return l.currentCursorY() == 0
}
// hasMultiplePages determines whether there is more than one page to be displayed
func (l *List) hasMultiplePages() bool {
return l.pagesNum() > 1
}