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
|
package gui
import (
"fmt"
"github.com/jesseduffield/gocui"
)
type ListContext struct {
GetItemsLength func() int
GetDisplayStrings func(startIdx int, length int) [][]string
OnFocus func(...OnFocusOpts) error
OnRenderToMain func(...OnFocusOpts) error
OnFocusLost func() error
OnClickSelectedItem func() error
// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
SelectedItem func() (ListItem, bool)
OnGetPanelState func() IListPanelState
// if this is true, we'll call GetDisplayStrings for just the visible part of the
// view and re-render that. This is useful when you need to render different
// content based on the selection (e.g. for showing the selected commit)
RenderSelection bool
Gui *Gui
*BasicContext
}
type IListContext interface {
GetSelectedItem() (ListItem, bool)
GetSelectedItemId() string
handlePrevLine() error
handleNextLine() error
handleScrollLeft() error
handleScrollRight() error
handleLineChange(change int) error
handleNextPage() error
handleGotoTop() error
handleGotoBottom() error
handlePrevPage() error
handleClick() error
onSearchSelect(selectedLineIdx int) error
FocusLine()
HandleRenderToMain() error
GetPanelState() IListPanelState
Context
}
func (self *ListContext) GetPanelState() IListPanelState {
return self.OnGetPanelState()
}
type IListPanelState interface {
SetSelectedLineIdx(int)
GetSelectedLineIdx() int
}
type ListItem interface {
// ID is a SHA when the item is a commit, a filename when the item is a file, 'stash@{4}' when it's a stash entry, 'my_branch' when it's a branch
ID() string
// Description is something we would show in a message e.g. '123as14: push blah' for a commit
Description() string
}
func (self *ListContext) FocusLine() {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
// ignoring error for now
return
}
// we need a way of knowing whether we've rendered to the view yet.
view.FocusPoint(view.OriginX(), self.GetPanelState().GetSelectedLineIdx())
if self.RenderSelection {
_, originY := view.Origin()
displayStrings := self.GetDisplayStrings(originY, view.InnerHeight())
self.Gui.renderDisplayStringsAtPos(view, originY, displayStrings)
}
view.Footer = formatListFooter(self.GetPanelState().GetSelectedLineIdx(), self.GetItemsLength())
}
func formatListFooter(selectedLineIdx int, length int) string {
return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
}
func (self *ListContext) GetSelectedItem() (ListItem, bool) {
return self.SelectedItem()
}
func (self *ListContext) GetSelectedItemId() string {
item, ok := self.GetSelectedItem()
if !ok {
return ""
}
return item.ID()
}
// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func (self *ListContext) HandleRender() error {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
if self.GetDisplayStrings != nil {
self.Gui.refreshSelectedLine(self.GetPanelState(), self.GetItemsLength())
self.Gui.renderDisplayStrings(view, self.GetDisplayStrings(0, self.GetItemsLength()))
self.Gui.render()
}
return nil
}
func (self *ListContext) HandleFocusLost() error {
if self.OnFocusLost != nil {
return self.OnFocusLost()
}
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
_ = view.SetOriginX(0)
return nil
}
func (self *ListContext) HandleFocus(opts ...OnFocusOpts) error {
if self.Gui.popupPanelFocused() {
return nil
}
self.FocusLine()
if self.Gui.State.Modes.Diffing.Active() {
return self.Gui.renderDiff()
}
if self.OnFocus != nil {
if err := self.OnFocus(opts...); err != nil {
return err
}
}
if self.OnRenderToMain != nil {
if err := self.OnRenderToMain(opts...); err != nil {
return err
}
}
return nil
}
func (self *ListContext) handlePrevLine() error {
return self.handleLineChange(-1)
}
func (self *ListContext) handleNextLine() error {
return self.handleLineChange(1)
}
func (self *ListContext) handleScrollLeft() error {
return self.scroll(self.Gui.scrollLeft)
}
func (self *ListContext) handleScrollRight() error {
return self.scroll(self.Gui.scrollRight)
}
func (self *ListContext) scroll(scrollFunc func(*gocui.View)) error {
if self.ignoreKeybinding() {
return nil
}
// get the view, move the origin
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
scrollFunc(view)
return self.HandleFocus()
}
func (self *ListContext) ignoreKeybinding() bool {
return !self.Gui.isPopupPanel(self.ViewName) && self.Gui.popupPanelFocused()
}
func (self *ListContext) handleLineChange(change int) error {
if self.ignoreKeybinding() {
return nil
}
selectedLineIdx := self.GetPanelState().GetSelectedLineIdx()
if (change < 0 && selectedLineIdx == 0) || (change > 0 && selectedLineIdx == self.GetItemsLength()-1) {
return nil
}
self.Gui.changeSelectedLine(self.GetPanelState(), self.GetItemsLength(), change)
return self.HandleFocus()
}
func (self *ListContext) handleNextPage() error {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
delta := self.Gui.pageDelta(view)
return self.handleLineChange(delta)
}
func (self *ListContext) handleGotoTop() error {
return self.handleLineChange(-self.GetItemsLength())
}
func (self *ListContext) handleGotoBottom() error {
return self.handleLineChange(self.GetItemsLength())
}
func (self *ListContext) handlePrevPage() error {
view, err := self.Gui.g.View(self.ViewName)
if err != nil {
return nil
}
delta := self.Gui.pageDelta(view)
return self.handleLineChange(-delta)
}
func (self *ListContext) handleClick() error {
if self.ignoreKeybinding() {
return nil
}
view, err := self.Gui.g.View(self.ViewName)
if err !=
|