diff --git a/writer.go b/writer.go index 3202236..d76542a 100644 --- a/writer.go +++ b/writer.go @@ -107,7 +107,7 @@ func (w *Writer) Flush() error { func (w *Writer) Start() { if w.ticker == nil { w.ticker = time.NewTicker(w.RefreshInterval) - w.tdone = make(chan bool, 1) + w.tdone = make(chan bool) } go w.Listen() @@ -115,8 +115,9 @@ func (w *Writer) Start() { // Stop stops the listener that updates the terminal func (w *Writer) Stop() { - _ = w.Flush() - close(w.tdone) + w.Flush() + w.tdone <- true + <-w.tdone } // Listen listens for updates to the writer's buffer and flushes to the out provided. It blocks the runtime. @@ -132,6 +133,7 @@ func (w *Writer) Listen() { w.ticker.Stop() w.ticker = nil w.mtx.Unlock() + close(w.tdone) return } } diff --git a/writer_test.go b/writer_test.go index 3825144..793136b 100644 --- a/writer_test.go +++ b/writer_test.go @@ -22,3 +22,14 @@ func TestWriter(t *testing.T) { t.Fatalf("want %q, got %q", want, b.String()) } } + +func TestStartCalledTwice(t *testing.T) { + w := New() + b := &bytes.Buffer{} + w.Out = b + + w.Start() + w.Stop() + w.Start() + w.Stop() +}