diff --git a/interface.go b/interface.go index 32d3acc..c70db8a 100644 --- a/interface.go +++ b/interface.go @@ -63,7 +63,7 @@ type Peco struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer - hub *hub.Hub + hub MessageHub args []string bufferSize int @@ -534,3 +534,20 @@ type ExternalCmdFilter struct { query string thresholdBufsiz int } + +// MessageHub is the interface that must be satisfied by the +// message hub component. Unless we're in testing, github.com/peco/peco/hub.Hub +// is used. +type MessageHub interface { + Batch(func(), bool) + DrawCh() chan hub.Payload + PagingCh() chan hub.Payload + QueryCh() chan hub.Payload + SendDraw(interface{}) + SendDrawPrompt() + SendPaging(interface{}) + SendQuery(string) + SendStatusMsg(string) + SendStatusMsgAndClear(string, time.Duration) + StatusMsgCh() chan hub.Payload +} diff --git a/peco.go b/peco.go index a0f23cb..5063afb 100644 --- a/peco.go +++ b/peco.go @@ -209,7 +209,7 @@ func (p *Peco) Caret() *Caret { return &p.caret } -func (p *Peco) Hub() *hub.Hub { +func (p *Peco) Hub() MessageHub { return p.hub } diff --git a/peco_test.go b/peco_test.go index f5d6277..bc8a05a 100644 --- a/peco_test.go +++ b/peco_test.go @@ -11,11 +11,26 @@ import ( "time" "github.com/nsf/termbox-go" + "github.com/peco/peco/hub" "github.com/peco/peco/internal/util" "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) +type nullHub struct{} + +func (h nullHub) Batch(_ func(), _ bool) {} +func (h nullHub) DrawCh() chan hub.Payload { return nil } +func (h nullHub) PagingCh() chan hub.Payload { return nil } +func (h nullHub) QueryCh() chan hub.Payload { return nil } +func (h nullHub) SendDraw(_ interface{}) {} +func (h nullHub) SendDrawPrompt() {} +func (h nullHub) SendPaging(_ interface{}) {} +func (h nullHub) SendQuery(_ string) {} +func (h nullHub) SendStatusMsg(_ string) {} +func (h nullHub) SendStatusMsgAndClear(_ string, _ time.Duration) {} +func (h nullHub) StatusMsgCh() chan hub.Payload { return nil } + type interceptorArgs []interface{} type interceptor struct { m sync.Mutex diff --git a/source.go b/source.go index aeb0dfe..9587a4d 100644 --- a/source.go +++ b/source.go @@ -41,12 +41,7 @@ func (s *Source) Setup(ctx context.Context, state *Peco) { defer close(s.setupDone) draw := func(state *Peco) { - // Not a great thing to do, allowing nil to be passed - // as state, but for testing I couldn't come up with anything - // better for the moment - if state != nil { - state.Hub().SendDraw(nil) - } + state.Hub().SendDraw(nil) } go func() { diff --git a/source_test.go b/source_test.go index 602f14f..506e231 100644 --- a/source_test.go +++ b/source_test.go @@ -44,7 +44,7 @@ func TestSource(t *testing.T) { r := addReadDelay(strings.NewReader(strings.Join(lines, "\n")), 2*time.Second) s := NewSource(r, ig, 0, false) - go s.Setup(ctx, nil) + go s.Setup(ctx, &Peco{hub: nullHub{}}) timeout := time.After(5 * time.Second) waitout := time.After(1 * time.Second)