Skip to content

Commit 944dab3

Browse files
authored
Merge pull request #102 from jesseduffield/make-search-callbacks-global
Change search related callbacks so that they are set on gui rather than each view
2 parents 5e45e57 + 1213312 commit 944dab3

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

gui.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,23 @@ type Gui struct {
139139
ReplayedEvents replayedEvents
140140
playRecording bool
141141

142-
tabClickBindings []*tabClickBinding
143-
viewMouseBindings []*ViewMouseBinding
144-
lastClick *clickInfo
145-
gEvents chan GocuiEvent
146-
userEvents chan userEvent
147-
views []*View
148-
currentView *View
149-
managers []Manager
150-
keybindings []*keybinding
151-
focusHandler func(bool) error
152-
openHyperlink func(string, string) error
153-
maxX, maxY int
154-
outputMode OutputMode
155-
stop chan struct{}
156-
blacklist []Key
142+
tabClickBindings []*tabClickBinding
143+
viewMouseBindings []*ViewMouseBinding
144+
lastClick *clickInfo
145+
gEvents chan GocuiEvent
146+
userEvents chan userEvent
147+
views []*View
148+
currentView *View
149+
managers []Manager
150+
keybindings []*keybinding
151+
focusHandler func(bool) error
152+
openHyperlink func(string, string) error
153+
onSelectSearchResultFunc func(*View, int)
154+
renderSearchStatusFunc func(*View, int, int)
155+
maxX, maxY int
156+
outputMode OutputMode
157+
stop chan struct{}
158+
blacklist []Key
157159

158160
// BgColor and FgColor allow to configure the background and foreground
159161
// colors of the GUI.
@@ -355,11 +357,26 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int, overlaps byte) (*View, er
355357
v.Overlaps = overlaps
356358
g.views = append(g.views, v)
357359

360+
v.setOnSelectResult(g.onSelectSearchItem)
361+
v.setRenderSearchStatus(g.renderSearchStatus)
362+
358363
g.Mutexes.ViewsMutex.Unlock()
359364

360365
return v, errors.Wrap(ErrUnknownView, 0)
361366
}
362367

368+
func (g *Gui) onSelectSearchItem(v *View, selectedLineIdx int) {
369+
if g.onSelectSearchResultFunc != nil {
370+
g.onSelectSearchResultFunc(v, selectedLineIdx)
371+
}
372+
}
373+
374+
func (g *Gui) renderSearchStatus(v *View, selected int, total int) {
375+
if g.renderSearchStatusFunc != nil {
376+
g.renderSearchStatusFunc(v, selected, total)
377+
}
378+
}
379+
363380
// SetViewBeneath sets a view stacked beneath another view
364381
func (g *Gui) SetViewBeneath(name string, aboveViewName string, height int) (*View, error) {
365382
aboveView, err := g.View(aboveViewName)
@@ -643,6 +660,14 @@ func (g *Gui) SetOpenHyperlinkFunc(openHyperlinkFunc func(string, string) error)
643660
g.openHyperlink = openHyperlinkFunc
644661
}
645662

663+
func (g *Gui) SetOnSelectSearchResultFunc(onSelectSearchResultFunc func(*View, int)) {
664+
g.onSelectSearchResultFunc = onSelectSearchResultFunc
665+
}
666+
667+
func (g *Gui) SetRenderSearchStatusFunc(renderSearchStatusFunc func(*View, int, int)) {
668+
g.renderSearchStatusFunc = renderSearchStatusFunc
669+
}
670+
646671
// getKey takes an empty interface with a key and returns the corresponding
647672
// typed Key or rune.
648673
func getKey(key any) (Key, rune, error) {

view.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,21 @@ type searcher struct {
214214
searchPositions []SearchPosition
215215
modelSearchResults []SearchPosition
216216
currentSearchIndex int
217-
onSelectItem func(int)
218-
renderSearchStatus func(int, int)
217+
onSelectItem func(*View, int)
218+
renderSearchStatus func(*View, int, int)
219219
}
220220

221-
func (v *View) SetRenderSearchStatus(renderSearchStatus func(int, int)) {
221+
func (v *View) setRenderSearchStatus(renderSearchStatus func(*View, int, int)) {
222222
v.searcher.renderSearchStatus = renderSearchStatus
223223
}
224224

225-
func (v *View) SetOnSelectItem(onSelectItem func(int)) {
225+
func (v *View) setOnSelectResult(onSelectItem func(*View, int)) {
226226
v.searcher.onSelectItem = onSelectItem
227227
}
228228

229229
func (v *View) renderSearchStatus(index int, itemCount int) {
230230
if v.searcher.renderSearchStatus != nil {
231-
v.searcher.renderSearchStatus(index, itemCount)
231+
v.searcher.renderSearchStatus(v, index, itemCount)
232232
}
233233
}
234234

@@ -286,7 +286,7 @@ func (v *View) SelectSearchResult(index int) {
286286
v.FocusPoint(v.ox, y, true)
287287
v.renderSearchStatus(index, itemCount)
288288
if v.searcher.onSelectItem != nil {
289-
v.searcher.onSelectItem(y)
289+
v.searcher.onSelectItem(v, y)
290290
}
291291
}
292292

0 commit comments

Comments
 (0)