/
list.go
349 lines (288 loc) · 9.13 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package views
import (
"context"
"fmt"
"strings"
"github.com/lawrencegripper/azbrowse/internal/pkg/eventing"
"github.com/lawrencegripper/azbrowse/internal/pkg/expanders"
"github.com/lawrencegripper/azbrowse/internal/pkg/style"
"github.com/stuartleeks/gocui"
)
// ListWidget hosts the left panel showing resources and controls the navigation
type ListWidget struct {
x, y int
w, h int
g *gocui.Gui
items []*expanders.TreeNode
filteredItems []*expanders.TreeNode
filterString string
contentView *ItemWidget
statusView *StatusbarWidget
navStack Stack
title string
ctx context.Context
selected int
expandedNodeItem *expanders.TreeNode
view *gocui.View
enableTracing bool
FullscreenKeyBinding string
ActionKeyBinding string
lastTopIndex int
}
// NewListWidget creates a new instance
func NewListWidget(ctx context.Context, x, y, w, h int, items []string, selected int, contentView *ItemWidget, status *StatusbarWidget, enableTracing bool, title string, g *gocui.Gui) *ListWidget {
listWidget := &ListWidget{ctx: ctx, x: x, y: y, w: w, h: h, contentView: contentView, statusView: status, enableTracing: enableTracing, lastTopIndex: 0, filterString: "", title: title, g: g}
go func() {
filterChannel := eventing.SubscribeToTopic("filter")
for {
filterStringInterface := <-filterChannel
filterString := strings.ToLower(strings.TrimSpace(strings.Replace(filterStringInterface.(string), "/", "", 1)))
filteredItems := []*expanders.TreeNode{}
for _, item := range listWidget.items {
if strings.Contains(strings.ToLower(item.Display), filterString) {
filteredItems = append(filteredItems, item)
}
}
listWidget.selected = 0
listWidget.filterString = filterString
listWidget.filteredItems = filteredItems
g.Update(func(gui *gocui.Gui) error {
return nil
})
}
}()
return listWidget
}
func (w *ListWidget) itemCount() int {
if w.filterString == "" {
return len(w.items)
}
return len(w.filteredItems)
}
// ClearFilter clears a filter if applied
func (w *ListWidget) ClearFilter() {
w.filterString = ""
}
func (w *ListWidget) itemsToShow() []*expanders.TreeNode {
if w.filterString == "" {
return w.items
}
return w.filteredItems
}
func highlightText(displayText string, highlight string) string {
if highlight == "" {
return displayText
}
index := strings.Index(strings.ToLower(displayText), highlight)
if index < 0 {
return displayText
}
return displayText[:index] + style.Highlight(displayText[index:index+len(highlight)]) + displayText[index+len(highlight):]
}
// Layout draws the widget in the gocui view
func (w *ListWidget) Layout(g *gocui.Gui) error {
v, err := g.SetView("listWidget", w.x, w.y, w.x+w.w, w.y+w.h)
if err != nil && err != gocui.ErrUnknownView {
return err
}
v.Clear()
w.view = v
if w.itemCount() < 1 {
return nil
}
linesUsedCount := 0
renderedItems := make([]string, 0, w.itemCount())
renderedItems = append(renderedItems, style.Separator(" ---\n"))
for i, s := range w.itemsToShow() {
var itemToShow string
if i == w.selected {
itemToShow = "▶ "
} else {
itemToShow = " "
}
itemToShow = itemToShow + highlightText(s.Display, w.filterString) + " " + s.StatusIndicator + "\n" + style.Separator(" ---") + "\n"
linesUsedCount = linesUsedCount + strings.Count(itemToShow, "\n")
renderedItems = append(renderedItems, itemToShow)
}
linesPerItem := linesUsedCount / w.itemCount()
maxItemsCanShow := (w.h / linesPerItem) - 1 // minus 1 to be on the safe side
topIndex := w.lastTopIndex
bottomIndex := w.lastTopIndex + maxItemsCanShow
if w.selected >= bottomIndex {
// need to adjust down
diff := w.selected - bottomIndex + 1
topIndex += diff
bottomIndex += diff
}
if w.selected < topIndex {
// need to adjust up
diff := topIndex - w.selected
topIndex -= diff
bottomIndex -= diff
}
w.lastTopIndex = topIndex
if bottomIndex > len(renderedItems) {
bottomIndex = len(renderedItems) - 1
}
for index := topIndex; index < bottomIndex+1; index++ {
if index < len(renderedItems) {
item := renderedItems[index]
fmt.Fprint(v, item)
}
}
// If the title is getting too long trim things
// down from the front
title := w.title
if w.filterString != "" {
title += "[filter=" + w.filterString + "]"
}
if len(title) > w.w {
trimLength := len(title) - w.w + 5 // Add five for spacing and elipsis
title = ".." + title[trimLength:]
}
w.view.Title = title
return nil
}
// Refresh refreshes the current view
func (w *ListWidget) Refresh() {
w.statusView.Status("Refreshing", true)
currentSelection := w.CurrentSelection()
w.GoBack()
w.ExpandCurrentSelection()
w.ChangeSelection(currentSelection)
w.statusView.Status("Done refreshing", false)
w.ClearFilter()
}
// GoBack takes the user back to preview view
func (w *ListWidget) GoBack() {
if w.filterString != "" {
// initial Back action is to clear filter, subsequent Back actions are normal
w.ClearFilter()
return
}
previousPage := w.navStack.Pop()
if previousPage == nil {
return
}
w.contentView.SetContent(previousPage.Data, previousPage.DataType, "Response")
w.selected = 0
w.items = previousPage.Value
w.title = previousPage.Title
w.selected = previousPage.Selection
w.expandedNodeItem = previousPage.ExpandedNodeItem
}
// ExpandCurrentSelection opens the resource Sub->RG for example
func (w *ListWidget) ExpandCurrentSelection() {
if w.title == "Subscriptions" {
w.title = ""
}
currentItem := w.CurrentItem()
w.ClearFilter()
newTitle := fmt.Sprintf("[%s-> Fullscreen|%s -> Actions] %s", strings.ToUpper(w.FullscreenKeyBinding), strings.ToUpper(w.ActionKeyBinding), currentItem.Name)
newContent, newItems, err := expanders.ExpandItem(w.ctx, currentItem)
if err != nil {
// Shouldn't be possible
panic(err)
}
w.Navigate(newItems, newContent, newTitle)
}
// Navigate updates the currently selected list nodes, title and details content
func (w *ListWidget) Navigate(nodes []*expanders.TreeNode, content *expanders.ExpanderResponse, title string) {
currentItem := w.CurrentItem()
if len(nodes) > 0 {
w.SetNodes(nodes)
}
w.expandedNodeItem = currentItem
w.contentView.SetContent(content.Response, content.ResponseType, title)
if currentItem != nil {
w.title = w.title + ">" + currentItem.Name
}
eventing.Publish("list.navigated", nodes)
}
// SetNodes allows others to set the list nodes
func (w *ListWidget) SetNodes(nodes []*expanders.TreeNode) {
// Capture current view to navstack
if w.HasCurrentItem() {
w.navStack.Push(&Page{
Data: w.contentView.GetContent(),
DataType: w.contentView.GetContentType(),
Value: w.items,
Title: w.title,
Selection: w.selected,
ExpandedNodeItem: w.CurrentItem(),
})
currentID := w.CurrentItem().ID
for _, node := range nodes {
if node.ID == currentID {
panic(fmt.Errorf("ids must be unique or the navigate command breaks"))
}
}
}
w.selected = 0
w.items = nodes
w.ClearFilter()
}
// ChangeSelection updates the selected item
func (w *ListWidget) ChangeSelection(i int) {
if i >= w.itemCount() {
i = w.itemCount() - 1
} else if i < 0 {
i = 0
}
w.selected = i
}
// CurrentSelection returns the current selection int
func (w *ListWidget) CurrentSelection() int {
return w.selected
}
// HasCurrentItem indicates whether there is a current item
func (w *ListWidget) HasCurrentItem() bool {
return w.selected < len(w.items)
}
// CurrentItem returns the selected item as a treenode
func (w *ListWidget) CurrentItem() *expanders.TreeNode {
if w.HasCurrentItem() {
return w.itemsToShow()[w.selected]
}
return nil
}
// CurrentExpandedItem returns the currently expanded item as a treenode
func (w *ListWidget) CurrentExpandedItem() *expanders.TreeNode {
return w.expandedNodeItem
}
// MovePageDown changes the selection to be a page further down the list
func (w *ListWidget) MovePageDown() {
i := w.selected
for remainingLinesToPage := w.h; remainingLinesToPage > 0 && i < w.itemCount(); i++ {
item := w.itemsToShow()[i]
remainingLinesToPage -= strings.Count(item.Display, "\n") + 1 // +1 as there is an implicit newline
remainingLinesToPage-- // separator
}
w.ChangeSelection(i)
}
// MovePageUp changes the selection to be a page further up the list
func (w *ListWidget) MovePageUp() {
i := w.selected
for remainingLinesToPage := w.h; remainingLinesToPage > 0 && i >= 0; i-- {
item := w.itemsToShow()[i]
remainingLinesToPage -= strings.Count(item.Display, "\n") + 1 // +1 as there is an implicit newline
remainingLinesToPage-- // separator
}
w.ChangeSelection(i)
}
// MoveHome changes the selection to the top of the list
func (w *ListWidget) MoveHome() {
w.ChangeSelection(0)
}
// MoveEnd changes the selection to the bottom of the list
func (w *ListWidget) MoveEnd() {
w.ChangeSelection(w.itemCount() - 1)
}
// MoveUp moves the selection up one item
func (w *ListWidget) MoveUp() {
w.ChangeSelection(w.CurrentSelection() - 1)
}
// MoveDown moves the selection down one item
func (w *ListWidget) MoveDown() {
w.ChangeSelection(w.CurrentSelection() + 1)
}