-
Notifications
You must be signed in to change notification settings - Fork 0
/
peco.go
706 lines (582 loc) · 14.9 KB
/
peco.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package peco
import (
"bytes"
"io"
"os"
"reflect"
"sync"
"time"
"unicode/utf8"
"golang.org/x/net/context"
"github.com/google/btree"
"github.com/lestrrat/go-pdebug"
"github.com/peco/peco/hub"
"github.com/peco/peco/internal/util"
"github.com/peco/peco/pipeline"
"github.com/peco/peco/sig"
"github.com/pkg/errors"
)
const version = "v0.4.5"
type errIgnorable struct {
err error
}
func (e errIgnorable) Ignorable() bool { return true }
func (e errIgnorable) Cause() error {
return e.err
}
func (e errIgnorable) Error() string {
return e.err.Error()
}
func makeIgnorable(err error) error {
return errIgnorable{err: err}
}
// Inputseq is a list of keys that the user typed
type Inputseq []string
func (is *Inputseq) Add(s string) {
*is = append(*is, s)
}
func (is Inputseq) KeyNames() []string {
return is
}
func (is Inputseq) Len() int {
return len(is)
}
func (is *Inputseq) Reset() {
*is = []string(nil)
}
func newIDGen() *idgen {
return &idgen{
ch: make(chan uint64),
}
}
func (ig *idgen) Run(ctx context.Context) {
var i uint64
for ; ; i++ {
select {
case <-ctx.Done():
return
case ig.ch <- i:
}
if i >= uint64(1<<63)-1 {
// If this happens, it's a disaster, but what can we do...
i = 0
}
}
}
func (ig *idgen) next() uint64 {
return <-ig.ch
}
var idGenerator = newIDGen()
func New() *Peco {
return &Peco{
Argv: os.Args,
Stderr: os.Stderr,
Stdin: os.Stdin,
Stdout: os.Stdout,
currentLineBuffer: NewMemoryBuffer(), // XXX revisit this
idgen: newIDGen(),
queryExecDelay: 50 * time.Millisecond,
readyCh: make(chan struct{}),
screen: &Termbox{},
selection: NewSelection(),
}
}
func (p *Peco) Ready() <-chan struct{} {
return p.readyCh
}
func (p *Peco) Screen() Screen {
return p.screen
}
func (p *Peco) Styles() *StyleSet {
return &p.styles
}
func (p *Peco) Prompt() string {
return p.prompt
}
func (p *Peco) Inputseq() *Inputseq {
return &p.inputseq
}
func (p *Peco) LayoutType() string {
return p.layoutType
}
func (p *Peco) Location() *Location {
return &p.location
}
func (p *Peco) ResultCh() chan Line {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.resultCh
}
func (p *Peco) SetResultCh(ch chan Line) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.resultCh = ch
}
func (p *Peco) Selection() *Selection {
return p.selection
}
func (s RangeStart) Valid() bool {
return s.valid
}
func (s RangeStart) Value() int {
return s.val
}
func (s *RangeStart) SetValue(n int) {
s.val = n
s.valid = true
}
func (s *RangeStart) Reset() {
s.valid = false
}
func (p *Peco) SelectionRangeStart() *RangeStart {
return &p.selectionRangeStart
}
func (p *Peco) SingleKeyJumpShowPrefix() bool {
return p.singleKeyJumpShowPrefix
}
func (p *Peco) SingleKeyJumpPrefixes() []rune {
return p.singleKeyJumpPrefixes
}
func (p *Peco) SingleKeyJumpMode() bool {
return p.singleKeyJumpMode
}
func (p *Peco) SetSingleKeyJumpMode(b bool) {
p.singleKeyJumpMode = b
}
func (p *Peco) ToggleSingleKeyJumpMode() {
p.singleKeyJumpMode = !p.singleKeyJumpMode
go p.Hub().SendDraw(&DrawOptions{DisableCache: true})
}
func (p *Peco) SingleKeyJumpIndex(ch rune) (uint, bool) {
n, ok := p.singleKeyJumpPrefixMap[ch]
return n, ok
}
func (p *Peco) Source() pipeline.Source {
return p.source
}
func (p *Peco) Filters() *FilterSet {
return &p.filters
}
func (p *Peco) Query() *Query {
return &p.query
}
func (p *Peco) QueryExecDelay() time.Duration {
return p.queryExecDelay
}
func (p *Peco) Caret() *Caret {
return &p.caret
}
func (p *Peco) Hub() MessageHub {
return p.hub
}
func (p *Peco) Err() error {
return p.err
}
func (p *Peco) Exit(err error) {
p.err = err
if cf := p.cancelFunc; cf != nil {
cf()
}
}
func (p *Peco) Keymap() Keymap {
return p.keymap
}
func (p *Peco) Setup() (err error) {
if pdebug.Enabled {
g := pdebug.Marker("Peco.Setup").BindError(&err)
defer g.End()
}
if err := p.config.Init(); err != nil {
return errors.Wrap(err, "failed to initialize config")
}
var opts CLIOptions
if err := p.parseCommandLine(&opts, &p.args, p.Argv); err != nil {
return errors.Wrap(err, "failed to parse command line")
}
// Read config
if !p.skipReadConfig { // This can only be set via test
if err := readConfig(&p.config, opts.OptRcfile); err != nil {
return errors.Wrap(err, "failed to setup configuration")
}
}
// Take Args, Config, Options, and apply the configuration to
// the peco object
if err := p.ApplyConfig(opts); err != nil {
return errors.Wrap(err, "failed to apply configuration")
}
// XXX p.Keymap et al should be initialized around here
p.hub = hub.New(5)
return nil
}
func (p *Peco) Run(ctx context.Context) (err error) {
if pdebug.Enabled {
g := pdebug.Marker("Peco.Run").BindError(&err)
defer g.End()
}
// do this only once
var readyOnce sync.Once
defer readyOnce.Do(func() { close(p.readyCh) })
if err := p.Setup(); err != nil {
return errors.Wrap(err, "failed to setup peco")
}
// screen.Init must be called within Run() because we
// want to make sure to call screen.Close() after getting
// out of Run()
p.screen.Init()
defer p.screen.Close()
var _cancelOnce sync.Once
var _cancel func()
ctx, _cancel = context.WithCancel(ctx)
cancel := func() {
_cancelOnce.Do(func() {
if pdebug.Enabled {
pdebug.Printf("Peco.Run cancel called")
}
_cancel()
})
}
// start the ID generator
go p.idgen.Run(ctx)
// remember this cancel func so p.Exit works (XXX requires locking?)
p.cancelFunc = cancel
loopers := []interface {
Loop(ctx context.Context, cancel func()) error
}{
NewInput(p, p.Keymap(), p.screen.PollEvent()),
NewView(p),
NewFilter(p),
sig.New(sig.SigReceivedHandlerFunc(func(sig os.Signal) {
p.Exit(errors.New("received signal: " + sig.String()))
})),
}
for _, l := range loopers {
go l.Loop(ctx, cancel)
}
// SetupSource is done AFTER other components are ready, otherwise
// we can't draw onto the screen while we are reading a really big
// buffer.
// Setup source buffer
src, err := p.SetupSource(ctx)
if err != nil {
return errors.Wrap(err, "failed to setup input source")
}
p.source = src
if p.Query().Len() <= 0 {
// Re-set the source only if there are no queries
p.ResetCurrentLineBuffer()
}
if pdebug.Enabled {
pdebug.Printf("peco is now ready, go go go!")
}
// If this is enabled, we need to check if we have 1 line only
// in the buffer. If we do, we select that line and bail out
if p.selectOneAndExit {
go func() {
// Wait till source has read all lines. We should not wait
// source.Ready(), because Ready returns as soon as we get
// a line, where as SetupDone waits until we're completely
// done reading the input
<-p.source.SetupDone()
// If we have only one line, we just want to bail out
// printing that one line as the result
if b := p.CurrentLineBuffer(); b.Size() == 1 {
if l, err := b.LineAt(0); err == nil {
p.resultCh = make(chan Line)
p.Exit(nil)
p.resultCh <- l
close(p.resultCh)
}
}
}()
}
readyOnce.Do(func() { close(p.readyCh) })
// This has tobe AFTER close(p.readyCh), otherwise the query is
// ignored by us (queries are not run until peco thinks it's ready)
if q := p.initialQuery; q != "" {
p.Query().Set(q)
p.Caret().SetPos(utf8.RuneCountInString(q))
}
if p.Query().Len() > 0 {
p.ExecQuery()
}
// Alright, done everything we need to do automatically. We'll let
// the user play with peco, and when we receive notification to
// bail out, the context should be canceled appropriately
<-ctx.Done()
// ...and we return any errors that we might have been informed about.
return p.Err()
}
func (p *Peco) parseCommandLine(opts *CLIOptions, args *[]string, argv []string) error {
remaining, err := opts.parse(argv)
if err != nil {
return errors.Wrap(err, "failed to parse command line options")
}
if opts.OptHelp {
p.Stdout.Write(opts.help())
return makeIgnorable(errors.New("user asked to show help message"))
}
if opts.OptVersion {
p.Stdout.Write([]byte("peco version " + version + "\n"))
return makeIgnorable(errors.New("user asked to show version"))
}
if opts.OptRcfile == "" {
if file, err := LocateRcfile(locateRcfileIn); err == nil {
opts.OptRcfile = file
}
}
if opts.OptTTY != "" {
p.Stderr.Write([]byte("Warning: --tty was never supported, and it will be removed in 0.5.x\n"))
time.Sleep(500 * time.Millisecond) // Wait, so that the user can see it
}
*args = remaining
return nil
}
func (p *Peco) SetupSource(ctx context.Context) (s *Source, err error) {
if pdebug.Enabled {
g := pdebug.Marker("Peco.SetupSource").BindError(&err)
defer g.End()
}
var in io.Reader
switch {
case len(p.args) > 1:
f, err := os.Open(p.args[1])
if err != nil {
return nil, errors.Wrap(err, "failed to open file for input")
}
if pdebug.Enabled {
pdebug.Printf("Using %s as input", p.args[1])
}
in = f
case !util.IsTty(p.Stdin):
if pdebug.Enabled {
pdebug.Printf("Using p.Stdin as input")
}
in = p.Stdin
default:
return nil, errors.New("you must supply something to work with via filename or stdin")
}
src := NewSource(in, p.idgen, p.bufferSize, p.enableSep)
// Block until we receive something from `in`
if pdebug.Enabled {
pdebug.Printf("Blocking until we read something in source...")
}
go src.Setup(ctx, p)
<-src.Ready()
return src, nil
}
func readConfig(cfg *Config, filename string) error {
if filename != "" {
if err := cfg.ReadFilename(filename); err != nil {
return errors.Wrap(err, "failed to read config file")
}
}
return nil
}
func (p *Peco) populateCommandList() error {
for _, v := range p.config.Command {
if len(v.Args) == 0 {
continue
}
makeCommandAction(p, &v).Register("ExecuteCommand." + v.Name)
}
return nil
}
func (p *Peco) ApplyConfig(opts CLIOptions) error {
// If layoutType is not set and is set in the config, set it
if p.layoutType == "" {
if v := p.config.Layout; v != "" {
p.layoutType = v
} else {
p.layoutType = DefaultLayoutType
}
}
p.enableSep = opts.OptEnableNullSep
if i := opts.OptInitialIndex; i >= 0 {
p.Location().SetLineNumber(i)
}
if v := opts.OptLayout; v != "" {
p.layoutType = v
}
p.prompt = p.config.Prompt
if v := opts.OptPrompt; len(v) > 0 {
p.prompt = v
} else if v := p.config.Prompt; len(v) > 0 {
p.prompt = v
}
p.bufferSize = opts.OptBufferSize
p.selectOneAndExit = opts.OptSelect1
p.initialQuery = opts.OptQuery
p.initialFilter = opts.OptInitialFilter
if len(p.initialFilter) <= 0 {
p.initialFilter = p.config.InitialFilter
}
if len(p.initialFilter) <= 0 {
p.initialFilter = opts.OptInitialMatcher
}
if err := p.populateCommandList(); err != nil {
return errors.Wrap(err, "failed to populate command list")
}
if err := p.populateFilters(); err != nil {
return errors.Wrap(err, "failed to populate filters")
}
if err := p.populateKeymap(); err != nil {
return errors.Wrap(err, "failed to populate keymap")
}
if err := p.populateStyles(); err != nil {
return errors.Wrap(err, "failed to populate styles")
}
if err := p.populateInitialFilter(); err != nil {
return errors.Wrap(err, "failed to populate initial filter")
}
if err := p.populateSingleKeyJump(); err != nil {
return errors.Wrap(err, "failed to populate single key jump configuration")
}
return nil
}
func (p *Peco) populateInitialFilter() error {
if v := p.initialFilter; len(v) > 0 {
if err := p.filters.SetCurrentByName(v); err != nil {
return errors.Wrap(err, "failed to set filter")
}
}
return nil
}
func (p *Peco) populateSingleKeyJump() error {
p.singleKeyJumpShowPrefix = p.config.SingleKeyJump.ShowPrefix
jumpMap := make(map[rune]uint)
chrs := "asdfghjklzxcvbnmqwertyuiop"
for i := 0; i < len(chrs); i++ {
jumpMap[rune(chrs[i])] = uint(i)
}
p.singleKeyJumpPrefixMap = jumpMap
p.singleKeyJumpPrefixes = make([]rune, len(jumpMap))
for k, v := range p.singleKeyJumpPrefixMap {
p.singleKeyJumpPrefixes[v] = k
}
return nil
}
func (p *Peco) populateFilters() error {
p.filters.Add(NewIgnoreCaseFilter())
p.filters.Add(NewCaseSensitiveFilter())
p.filters.Add(NewSmartCaseFilter())
p.filters.Add(NewRegexpFilter())
for name, c := range p.config.CustomFilter {
f := NewExternalCmdFilter(name, c.Cmd, c.Args, c.BufferThreshold, p.idgen, p.enableSep)
p.filters.Add(f)
}
return nil
}
func (p *Peco) populateKeymap() error {
// Create a new keymap object
k := NewKeymap(p.config.Keymap, p.config.Action)
if err := k.ApplyKeybinding(); err != nil {
return errors.Wrap(err, "failed to apply key bindings")
}
p.keymap = k
return nil
}
func (p *Peco) populateStyles() error {
p.styles = p.config.Style
return nil
}
func (p *Peco) CurrentLineBuffer() Buffer {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.currentLineBuffer
}
func (p *Peco) SetCurrentLineBuffer(b Buffer) {
p.mutex.Lock()
defer p.mutex.Unlock()
if pdebug.Enabled {
g := pdebug.Marker("Peco.SetCurrentLineBuffer %s", reflect.TypeOf(b).String())
defer g.End()
}
p.currentLineBuffer = b
go p.Hub().SendDraw(nil)
}
func (p *Peco) ResetCurrentLineBuffer() {
p.SetCurrentLineBuffer(p.source)
}
func (p *Peco) ExecQuery() bool {
if pdebug.Enabled {
g := pdebug.Marker("Peco.ExecQuery")
defer g.End()
}
select {
case <-p.Ready():
default:
if pdebug.Enabled {
pdebug.Printf("peco is not ready yet, ignoring.")
}
return false
}
// If this is an empty query, reset the display to show
// the raw source buffer
q := p.Query()
if q.Len() <= 0 {
if pdebug.Enabled {
pdebug.Printf("empty query, reset buffer")
}
p.ResetCurrentLineBuffer()
p.Hub().SendDraw(&DrawOptions{DisableCache: true})
return true
}
delay := p.QueryExecDelay()
if delay <= 0 {
if pdebug.Enabled {
pdebug.Printf("sending query (immediate)")
}
// No delay, execute immediately
p.Hub().SendQuery(q.String())
return true
}
p.queryExecMutex.Lock()
defer p.queryExecMutex.Unlock()
if p.queryExecTimer != nil {
if pdebug.Enabled {
pdebug.Printf("timer is non-nil")
}
return true
}
// Wait $delay millisecs before sending the query
// if a new input comes in, batch them up
if pdebug.Enabled {
pdebug.Printf("sending query with delay)")
}
p.queryExecTimer = time.AfterFunc(delay, func() {
if pdebug.Enabled {
pdebug.Printf("delayed query sent")
}
p.Hub().SendQuery(q.String())
p.queryExecMutex.Lock()
defer p.queryExecMutex.Unlock()
p.queryExecTimer = nil
})
return true
}
func (p *Peco) PrintResults() {
if pdebug.Enabled {
g := pdebug.Marker("Peco.PrintResults")
defer g.End()
}
selection := p.Selection()
if selection.Len() == 0 {
if l, err := p.CurrentLineBuffer().LineAt(p.Location().LineNumber()); err == nil {
selection.Add(l)
}
}
p.SetResultCh(make(chan Line))
go func() {
defer close(p.resultCh)
p.selection.Ascend(func(it btree.Item) bool {
p.ResultCh() <- it.(Line)
return true
})
}()
var buf bytes.Buffer
for line := range p.ResultCh() {
buf.WriteString(line.Output())
buf.WriteByte('\n')
}
p.Stdout.Write(buf.Bytes())
}