这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ 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()
}

// 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.
Expand All @@ -132,6 +133,7 @@ func (w *Writer) Listen() {
w.ticker.Stop()
w.ticker = nil
w.mtx.Unlock()
close(w.tdone)
return
}
}
Expand Down
11 changes: 11 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}