-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathui.go
318 lines (266 loc) · 7.37 KB
/
ui.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
/*
Terminews is a terminal based (TUI) RSS feed manager.
Copyright (C) 2017 Alexandros Ntavelos, a[dot]ntavelos[at]gmail[dot]com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"fmt"
c "github.com/jroimartin/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 {
*c.View
title string
items []interface{}
pages []Page
currPageIdx int
ordered bool
}
// CreateList initializes a List object with an existing View by applying some
// basic configuration
func CreateList(v *c.View, ordered bool) *List {
list := &List{}
list.View = v
list.SelBgColor = c.ColorBlack
list.SelFgColor = c.ColorWhite | c.AttrBold
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 *c.Gui) error {
l.Highlight = true
_, err := g.SetCurrentView(l.Name())
return err
}
// Unfocus is used to remove highlighting from the current list
func (l *List) Unfocus() {
l.Highlight = false
}
// Reset zeros the list's slices out and clears the underlying View
func (l *List) Reset() {
l.items = make([]interface{}, 0)
l.pages = []Page{}
l.Clear()
l.ResetCursor()
}
// 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 - %v ", l.currPageNum(), l.pagesNum(), title)
} else {
l.Title = fmt.Sprintf(" %v ", title)
}
}
// SetItems will (re)evaluates the list's items with the given data and redraws
// the View
func (l *List) SetItems(data []interface{}) error {
l.items = data
l.ResetPages()
return l.Draw()
}
// AddItem appends a given item to the existing list
func (l *List) AddItem(g *c.Gui, item interface{}) error {
l.items = append(l.items, item)
l.ResetPages()
return l.Draw()
}
func (l *List) UpdateCurrentItem(item interface{}) {
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.currPageIdx)
}
// 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() {
l.displayPage(l.nextPageIdx())
}
}
return l.SetCursor(0, y)
}
// 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() {
l.displayPage(l.prevPageIdx())
}
}
return l.SetCursor(0, y)
}
// MovePgDown displays the next page
func (l *List) MovePgDown() error {
if l.IsEmpty() {
return nil
}
l.displayPage(l.nextPageIdx())
return l.SetCursor(0, 0)
}
// MovePgUp displays the previous page
func (l *List) MovePgUp() error {
if l.IsEmpty() {
return nil
}
l.displayPage(l.prevPageIdx())
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() interface{} {
if l.IsEmpty() {
return nil
}
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() {
l.SetCursor(0, 0)
}
// 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.currPageIdx + 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.currPageIdx]
}
// 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.currPageIdx + 1) % l.pagesNum()
}
// prevPageIdx returns the index of the prev page to be displayed circularlt
func (l *List) prevPageIdx() int {
pidx := (l.currPageIdx - 1) % l.pagesNum()
if l.currPageIdx == 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) - 3)
if l.ordered {
return fmt.Sprintf("%2d. %v%v", i+1, item, sp)
} else {
return fmt.Sprintf(" %v%v", item, sp)
}
}
// displayPage resets the currentPageIdx and displays the requested page
func (l *List) displayPage(p int) error {
l.Clear()
l.currPageIdx = p
page := l.pages[l.currPageIdx]
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
}
func spaces(n int) string {
var s bytes.Buffer
for i := 0; i < n; i++ {
s.WriteString(" ")
}
return s.String()
}