From d19965d46e11d2e88a5ea6507d6c795652b933b9 Mon Sep 17 00:00:00 2001 From: Tim Heckman Date: Tue, 14 Dec 2021 17:55:45 -0800 Subject: [PATCH] Reset the buffer when constructing the spinner This needs to be done, otherwise the buffer will print null bytes at the beginning of the first line. --- spinner.go | 5 ++++- spinner_test.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/spinner.go b/spinner.go index ec48349..e5ba29a 100644 --- a/spinner.go +++ b/spinner.go @@ -256,8 +256,11 @@ func New(cfg Config) (*Spinner, error) { cfg.NotTTY = true } + buf := bytes.NewBuffer(make([]byte, 2048)) + buf.Reset() + s := &Spinner{ - buffer: bytes.NewBuffer(make([]byte, 2048)), + buffer: buf, mu: &sync.Mutex{}, frequency: cfg.Frequency, status: uint32Ptr(0), diff --git a/spinner_test.go b/spinner_test.go index ebb47b6..2c78cd8 100644 --- a/spinner_test.go +++ b/spinner_test.go @@ -132,6 +132,10 @@ func TestNew(t *testing.T) { t.Fatal("spinner is nil") } + if n := spinner.buffer.Len(); n != 0 { + t.Fatalf("spinner.buffer.Len() = %d, want 0", n) + } + if spinner.colorAll != tt.cfg.ColorAll { t.Fatalf("spinner.colorAll = %t, want %t", spinner.colorAll, tt.cfg.ColorAll) }