lazy init CancelableStdin() (#78)

This commit is contained in:
chzyer 2016-09-04 21:02:24 +08:00 committed by GitHub
parent edfa7c9dbf
commit 6cbf970b0b
2 changed files with 7 additions and 8 deletions

View File

@ -87,7 +87,7 @@ func (c *Config) Init() error {
} }
c.inited = true c.inited = true
if c.Stdin == nil { if c.Stdin == nil {
c.Stdin = Stdin c.Stdin = NewCancelableStdin(Stdin)
} }
if c.Stdout == nil { if c.Stdout == nil {
c.Stdout = Stdout c.Stdout = Stdout

13
std.go
View File

@ -7,7 +7,7 @@ import (
) )
var ( var (
Stdin io.ReadCloser = NewCancelableStdin() Stdin io.ReadCloser = os.Stdin
Stdout io.WriteCloser = os.Stdout Stdout io.WriteCloser = os.Stdout
Stderr io.WriteCloser = os.Stderr Stderr io.WriteCloser = os.Stderr
) )
@ -66,6 +66,7 @@ func Line(prompt string) (string, error) {
} }
type CancelableStdin struct { type CancelableStdin struct {
r io.Reader
mutex sync.Mutex mutex sync.Mutex
stop chan struct{} stop chan struct{}
notify chan struct{} notify chan struct{}
@ -75,11 +76,13 @@ type CancelableStdin struct {
ioloopFired bool ioloopFired bool
} }
func NewCancelableStdin() *CancelableStdin { func NewCancelableStdin(r io.Reader) *CancelableStdin {
c := &CancelableStdin{ c := &CancelableStdin{
r: r,
notify: make(chan struct{}), notify: make(chan struct{}),
stop: make(chan struct{}), stop: make(chan struct{}),
} }
go c.ioloop()
return c return c
} }
@ -88,7 +91,7 @@ loop:
for { for {
select { select {
case <-c.notify: case <-c.notify:
c.read, c.err = os.Stdin.Read(c.data) c.read, c.err = c.r.Read(c.data)
c.notify <- struct{}{} c.notify <- struct{}{}
case <-c.stop: case <-c.stop:
break loop break loop
@ -99,10 +102,6 @@ loop:
func (c *CancelableStdin) Read(b []byte) (n int, err error) { func (c *CancelableStdin) Read(b []byte) (n int, err error) {
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
if !c.ioloopFired {
c.ioloopFired = true
go c.ioloop()
}
c.data = b c.data = b
c.notify <- struct{}{} c.notify <- struct{}{}