mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
protect peco from race
This commit is contained in:
parent
a6589820d0
commit
9cbe49c72d
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/lestrrat/go-pdebug"
|
||||
"github.com/peco/peco/internal/util"
|
||||
"github.com/peco/peco/pipeline"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
|
|
@ -180,6 +181,14 @@ func (s *Source) Setup(state *Peco) {
|
|||
close(s.ready)
|
||||
}
|
||||
scanner := bufio.NewScanner(s.in)
|
||||
defer func() {
|
||||
if util.IsTty(s.in) {
|
||||
return
|
||||
}
|
||||
if closer, ok := s.in.(io.Closer); ok {
|
||||
closer.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
readCount := 0
|
||||
for scanner.Scan() {
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ type Peco struct {
|
|||
inputseq Inputseq // current key sequence (just the names)
|
||||
layoutType string
|
||||
location Location
|
||||
mutex sync.Mutex
|
||||
prompt string
|
||||
query Query
|
||||
queryExecDelay time.Duration
|
||||
|
|
|
|||
9
peco.go
9
peco.go
|
|
@ -97,10 +97,14 @@ func (p *Peco) Location() *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
|
||||
}
|
||||
|
||||
|
|
@ -358,7 +362,6 @@ func (p *Peco) SetupSource() (s *Source, err error) {
|
|||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to open file for input")
|
||||
}
|
||||
defer f.Close() // ONLY do this here, you don't want to close Stdin
|
||||
in = f
|
||||
case !util.IsTty(p.Stdin):
|
||||
in = p.Stdin
|
||||
|
|
@ -483,10 +486,14 @@ func (p *Peco) populateStyles() error {
|
|||
}
|
||||
|
||||
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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue