-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
589 lines (515 loc) · 12.7 KB
/
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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package peco
import (
"bufio"
"bytes"
"os/exec"
"regexp"
"sort"
"sync"
"time"
"github.com/lestrrat/go-pdebug"
"github.com/peco/peco/hub"
"github.com/peco/peco/internal/util"
"github.com/peco/peco/pipeline"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
func (fs *FilterSet) Reset() {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.current = 0
}
func (fs *FilterSet) Size() int {
fs.mutex.Lock()
defer fs.mutex.Unlock()
return len(fs.filters)
}
func (fs *FilterSet) Add(lf LineFilter) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.filters = append(fs.filters, lf)
return nil
}
func (fs *FilterSet) Rotate() {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.current++
if fs.current >= len(fs.filters) {
fs.current = 0
}
if pdebug.Enabled {
pdebug.Printf("FilterSet.Rotate: now filter in effect is %s", fs.filters[fs.current])
}
}
func (fs *FilterSet) SetCurrentByName(name string) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
for i, f := range fs.filters {
if f.String() == name {
fs.current = i
return nil
}
}
return ErrFilterNotFound
}
func (fs *FilterSet) Index() int {
fs.mutex.Lock()
defer fs.mutex.Unlock()
return fs.current
}
func (fs *FilterSet) Current() LineFilter {
fs.mutex.Lock()
defer fs.mutex.Unlock()
return fs.filters[fs.current]
}
func NewFilter(state *Peco) *Filter {
return &Filter{
state: state,
}
}
// Work is the actual work horse that that does the matching
// in a goroutine of its own. It wraps Matcher.Match().
func (f *Filter) Work(ctx context.Context, q hub.Payload) {
defer q.Done()
query, ok := q.Data().(string)
if !ok {
return
}
if pdebug.Enabled {
g := pdebug.Marker("Filter.Work query '%s'", query)
defer g.End()
}
state := f.state
if query == "" {
state.ResetCurrentLineBuffer()
if !state.config.StickySelection {
state.Selection().Reset()
}
return
}
// Create a new pipeline
p := pipeline.New()
p.SetSource(state.Source())
thisf := state.Filters().Current().Clone()
thisf.SetQuery(query)
p.Add(thisf)
buf := NewMemoryBuffer()
p.SetDestination(buf)
state.SetCurrentLineBuffer(buf)
go func() {
defer state.Hub().SendDraw(&DrawOptions{RunningQuery: true})
ctx = context.WithValue(ctx, "query", query)
if err := p.Run(ctx); err != nil {
state.Hub().SendStatusMsg(err.Error())
}
}()
go func() {
if pdebug.Enabled {
g := pdebug.Marker("Periodic draw request for '%s'", query)
defer g.End()
}
t := time.NewTicker(5*time.Millisecond)
defer t.Stop()
defer state.Hub().SendStatusMsg("")
defer state.Hub().SendDraw(&DrawOptions{RunningQuery: true})
for {
select {
case <-p.Done():
return
case <-t.C:
state.Hub().SendDraw(&DrawOptions{RunningQuery: true})
}
}
}()
<-p.Done()
if !state.config.StickySelection {
state.Selection().Reset()
}
}
// Loop keeps watching for incoming queries, and upon receiving
// a query, spawns a goroutine to do the heavy work. It also
// checks for previously running queries, so we can avoid
// running many goroutines doing the grep at the same time
func (f *Filter) Loop(ctx context.Context, cancel func()) error {
defer cancel()
// previous holds the function that can cancel the previous
// query. This is used when multiple queries come in succession
// and the previous query is discarded anyway
var mutex sync.Mutex
var previous func()
for {
select {
case <-ctx.Done():
return nil
case q := <-f.state.Hub().QueryCh():
workctx, workcancel := context.WithCancel(ctx)
mutex.Lock()
if previous != nil {
if pdebug.Enabled {
pdebug.Printf("Canceling previous query")
}
previous()
}
previous = workcancel
mutex.Unlock()
f.state.Hub().SendStatusMsg("Running query...")
go f.Work(workctx, q)
}
}
}
func NewRegexpFilter() *RegexpFilter {
return &RegexpFilter{
flags: regexpFlagList(defaultFlags),
name: "Regexp",
outCh: pipeline.OutputChannel(make(chan interface{})),
}
}
func (rf *RegexpFilter) OutCh() <-chan interface{} {
rf.mutex.Lock()
defer rf.mutex.Unlock()
return rf.outCh
}
func (rf RegexpFilter) Clone() LineFilter {
return &RegexpFilter{
flags: rf.flags,
quotemeta: rf.quotemeta,
query: rf.query,
name: rf.name,
outCh: pipeline.OutputChannel(make(chan interface{})),
}
}
const filterBufSize = 1000
var filterBufPool = sync.Pool{
New: func() interface{} {
return make([]Line, 0, filterBufSize)
},
}
func releaseRegexpFilterBuf(l []Line) {
if l == nil {
return
}
l = l[0:0]
filterBufPool.Put(l)
}
func getRegexpFilterBuf() []Line {
l := filterBufPool.Get().([]Line)
return l
}
func (rf *RegexpFilter) Accept(ctx context.Context, in chan interface{}, out pipeline.OutputChannel) {
if pdebug.Enabled {
g := pdebug.Marker("RegexpFilter.Accept")
defer g.End()
}
flush := make(chan []Line)
flushDone := make(chan struct{})
go func() {
if pdebug.Enabled {
g := pdebug.Marker("RegexpFilter.Accept flusher goroutine")
defer g.End()
}
defer close(flushDone)
defer out.SendEndMark("end of RegexpFilter")
for buf := range flush {
for _, in := range buf {
if l, err := rf.filter(in); err == nil {
out.Send(l)
}
}
releaseRegexpFilterBuf(buf)
}
}()
buf := getRegexpFilterBuf()
defer func() { releaseRegexpFilterBuf(buf) }()
defer func() { <-flushDone }() // Wait till the flush goroutine is done
defer close(flush) // Kill the flush goroutine
flushTicker := time.NewTicker(50*time.Millisecond)
defer flushTicker.Stop()
start := time.Now()
lines := 0
for {
select {
case <-ctx.Done():
if pdebug.Enabled {
pdebug.Printf("RegexpFilter received done")
}
return
case v := <-in:
switch v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pdebug.Enabled {
pdebug.Printf("RegexpFilter received end mark (read %d lines, %s since starting accept loop)", lines+len(buf), time.Since(start).String())
}
if len(buf) > 0 {
flush <- buf
buf = nil
}
}
return
case Line:
if pdebug.Enabled {
lines++
}
// We buffer the lines so that we can receive more lines to
// process while we filter what we already have. The buffer
// size is fairly big, because this really only makes a
// difference if we have a lot of lines to process.
buf = append(buf, v.(Line))
select {
case <-flushTicker.C:
flush <- buf
buf = getRegexpFilterBuf()
default:
if len(buf) >= cap(buf) {
flush <- buf
buf = getRegexpFilterBuf()
}
}
}
}
}
}
func (rf *RegexpFilter) filter(l Line) (Line, error) {
regexps, err := rf.getQueryAsRegexps()
if err != nil {
return nil, errors.Wrap(err, "failed to compile queries as regular expression")
}
v := l.DisplayString()
allMatched := true
matches := [][]int{}
TryRegexps:
for _, rx := range regexps {
match := rx.FindAllStringSubmatchIndex(v, -1)
if match == nil {
allMatched = false
break TryRegexps
}
matches = append(matches, match...)
}
if !allMatched {
return nil, errors.New("filter did not match against given line")
}
sort.Sort(byMatchStart(matches))
// We need to "dedupe" the results. For example, if we matched the
// same region twice, we don't want that to be drawn
deduped := make([][]int, 0, len(matches))
for i, m := range matches {
// Always push the first one
if i == 0 {
deduped = append(deduped, m)
continue
}
prev := deduped[len(deduped)-1]
switch {
case matchContains(prev, m):
// If the previous match contains this one, then
// don't do anything
continue
case matchOverlaps(prev, m):
// If the previous match overlaps with this one,
// merge the results and make it a bigger one
deduped[len(deduped)-1] = mergeMatches(prev, m)
default:
deduped = append(deduped, m)
}
}
return NewMatchedLine(l, deduped), nil
}
func (rf *RegexpFilter) getQueryAsRegexps() ([]*regexp.Regexp, error) {
rf.mutex.Lock()
defer rf.mutex.Unlock()
if q := rf.compiledQuery; q != nil {
return q, nil
}
q, err := queryToRegexps(rf.flags, rf.quotemeta, rf.query)
if err != nil {
return nil, errors.Wrap(err, "failed to compile queries as regular expression")
}
rf.compiledQuery = q
return q, nil
}
func (rf *RegexpFilter) SetQuery(q string) {
rf.mutex.Lock()
defer rf.mutex.Unlock()
rf.query = q
rf.compiledQuery = nil
}
func (rf RegexpFilter) String() string {
return rf.name
}
var ErrFilterNotFound = errors.New("specified filter was not found")
func NewIgnoreCaseFilter() *RegexpFilter {
rf := NewRegexpFilter()
rf.flags = ignoreCaseFlags
rf.quotemeta = true
rf.name = "IgnoreCase"
return rf
}
func NewCaseSensitiveFilter() *RegexpFilter {
rf := NewRegexpFilter()
rf.quotemeta = true
rf.name = "CaseSensitive"
return rf
}
// SmartCaseFilter turns ON the ignore-case flag in the regexp
// if the query contains a upper-case character
func NewSmartCaseFilter() *RegexpFilter {
rf := NewRegexpFilter()
rf.quotemeta = true
rf.name = "SmartCase"
rf.flags = regexpFlagFunc(func(q string) []string {
if util.ContainsUpper(q) {
return defaultFlags
}
return []string{"i"}
})
return rf
}
func NewExternalCmdFilter(name string, cmd string, args []string, threshold int, idgen lineIDGenerator, enableSep bool) *ExternalCmdFilter {
if len(args) == 0 {
args = []string{"$QUERY"}
}
if threshold <= 0 {
threshold = DefaultCustomFilterBufferThreshold
}
return &ExternalCmdFilter{
args: args,
cmd: cmd,
enableSep: enableSep,
idgen: idgen,
name: name,
outCh: pipeline.OutputChannel(make(chan interface{})),
thresholdBufsiz: threshold,
}
}
func (ecf ExternalCmdFilter) Clone() LineFilter {
return &ExternalCmdFilter{
args: ecf.args,
cmd: ecf.cmd,
enableSep: ecf.enableSep,
idgen: ecf.idgen,
name: ecf.name,
outCh: pipeline.OutputChannel(make(chan interface{})),
thresholdBufsiz: ecf.thresholdBufsiz,
}
}
func (ecf *ExternalCmdFilter) Verify() error {
if ecf.cmd == "" {
return errors.Errorf("no executable specified for custom matcher '%s'", ecf.name)
}
if _, err := exec.LookPath(ecf.cmd); err != nil {
return errors.Wrap(err, "failed to locate command")
}
return nil
}
func (ecf *ExternalCmdFilter) Accept(ctx context.Context, in chan interface{}, out pipeline.OutputChannel) {
if pdebug.Enabled {
g := pdebug.Marker("ExternalCmdFilter.Accept")
defer g.End()
}
defer out.SendEndMark("end of ExternalCmdFilter")
buf := make([]Line, 0, ecf.thresholdBufsiz)
for {
select {
case <-ctx.Done():
if pdebug.Enabled {
pdebug.Printf("ExternalCmdFilter received done")
}
return
case v := <-in:
switch v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pdebug.Enabled {
pdebug.Printf("ExternalCmdFilter received end mark")
}
if len(buf) > 0 {
ecf.launchExternalCmd(ctx, buf, out)
}
}
return
case Line:
if pdebug.Enabled {
pdebug.Printf("ExternalCmdFilter received new line")
}
buf = append(buf, v.(Line))
if len(buf) < ecf.thresholdBufsiz {
continue
}
ecf.launchExternalCmd(ctx, buf, out)
buf = buf[0:0]
}
}
}
}
func (ecf *ExternalCmdFilter) SetQuery(q string) {
ecf.query = q
}
func (ecf ExternalCmdFilter) String() string {
return ecf.name
}
func (ecf *ExternalCmdFilter) launchExternalCmd(ctx context.Context, buf []Line, out pipeline.OutputChannel) {
defer func() { recover() }() // ignore errors
if pdebug.Enabled {
g := pdebug.Marker("ExternalCmdFilter.launchExternalCmd")
defer g.End()
}
args := append([]string(nil), ecf.args...)
for i, v := range args {
if v == "$QUERY" {
args[i] = ecf.query
}
}
cmd := exec.Command(ecf.cmd, args...)
if pdebug.Enabled {
pdebug.Printf("Executing command %s %v", cmd.Path, cmd.Args)
}
inbuf := &bytes.Buffer{}
for _, l := range buf {
inbuf.WriteString(l.DisplayString() + "\n")
}
cmd.Stdin = inbuf
r, err := cmd.StdoutPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return
}
go cmd.Wait()
cmdCh := make(chan Line)
go func(cmdCh chan Line, rdr *bufio.Reader) {
defer func() { recover() }()
defer close(cmdCh)
for {
b, _, err := rdr.ReadLine()
if len(b) > 0 {
// TODO: need to redo the spec for custom matchers
// This is the ONLY location where we need to actually
// RECREATE a RawLine, and thus the only place where
// ctx.enableSep is required.
cmdCh <- NewMatchedLine(NewRawLine(ecf.idgen.next(), string(b), ecf.enableSep), nil)
}
if err != nil {
break
}
}
}(cmdCh, bufio.NewReader(r))
defer func() {
if p := cmd.Process; p != nil {
p.Kill()
}
}()
for {
select {
case <-ctx.Done():
return
case l, ok := <-cmdCh:
if l == nil || !ok {
return
}
out.Send(l)
}
}
}