-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
42 lines (37 loc) · 847 Bytes
/
filter.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
package peco
type Filter struct {
*Ctx
jobs chan string
}
func (f *Filter) Work(cancel chan struct{}, q string) {
if q == "" {
f.DrawMatches(nil)
return
}
results := f.Matcher().Match(cancel, q, f.Buffer())
f.statusMessage = ""
f.selection.Clear()
f.DrawMatches(results)
}
func (f *Filter) Loop() {
defer f.ReleaseWaitGroup()
// previous holds a channel that can cancel the previous
// query. This is used when multiple queries come in succession
// and the previous query is discarded anyway
var previous chan struct{}
for {
select {
case <-f.LoopCh():
return
case q := <-f.QueryCh():
if previous != nil {
// Tell the previous query to stop
previous <- struct{}{}
}
previous = make(chan struct{}, 1)
f.statusMessage = "Running query..."
f.DrawMatches(nil)
go f.Work(previous, q)
}
}
}