From e1b102469c5892ffdcfc6547087e2030ba06584d Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Thu, 18 May 2023 15:53:46 +0800 Subject: [PATCH 1/2] code optimization --- .gitignore | 1 + format_test.go | 15 +++++++-------- pb.go | 9 ++++----- pool.go | 10 +++++----- 4 files changed, 17 insertions(+), 18 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/format_test.go b/format_test.go index 645709e..a27d805 100644 --- a/format_test.go +++ b/format_test.go @@ -1,7 +1,6 @@ package pb import ( - "fmt" "strconv" "testing" "time" @@ -13,7 +12,7 @@ func Test_DefaultsToInteger(t *testing.T) { actual := Format(value).String() if actual != expected { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual)) + t.Errorf("Expected {%s} was {%s}", expected, actual) } } @@ -23,7 +22,7 @@ func Test_CanFormatAsInteger(t *testing.T) { actual := Format(value).To(U_NO).String() if actual != expected { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual)) + t.Errorf("Expected {%s} was {%s}", expected, actual) } } @@ -42,7 +41,7 @@ func Test_CanFormatAsBytes(t *testing.T) { for _, input := range inputs { actual := Format(input.v).To(U_BYTES).String() if actual != input.e { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual)) + t.Errorf("Expected {%s} was {%s}", input.e, actual) } } } @@ -62,7 +61,7 @@ func Test_CanFormatAsBytesDec(t *testing.T) { for _, input := range inputs { actual := Format(input.v).To(U_BYTES_DEC).String() if actual != input.e { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual)) + t.Errorf("Expected {%s} was {%s}", input.e, actual) } } } @@ -72,7 +71,7 @@ func Test_CanFormatDuration(t *testing.T) { expected := "10m00s" actual := Format(int64(value)).To(U_DURATION).String() if actual != expected { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual)) + t.Errorf("Expected {%s} was {%s}", expected, actual) } } @@ -81,7 +80,7 @@ func Test_CanFormatLongDuration(t *testing.T) { expected := "2d14h00m13s" actual := Format(int64(value)).To(U_DURATION).String() if actual != expected { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual)) + t.Errorf("Expected {%s} was {%s}", expected, actual) } } @@ -90,6 +89,6 @@ func Test_DefaultUnitsWidth(t *testing.T) { expected := " 10" actual := Format(int64(value)).Width(7).String() if actual != expected { - t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual)) + t.Errorf("Expected {%s} was {%s}", expected, actual) } } diff --git a/pb.go b/pb.go index afae752..48cfd42 100644 --- a/pb.go +++ b/pb.go @@ -322,7 +322,7 @@ func (pb *ProgressBar) write(total, current int64) { // time left currentFromStart := current - pb.startValue - fromStart := time.Now().Sub(pb.startTime) + fromStart := time.Since(pb.startTime) lastChangeTime := pb.changeTime fromChange := lastChangeTime.Sub(pb.startTime) @@ -333,8 +333,7 @@ func (pb *ProgressBar) write(total, current int64) { select { case <-pb.finish: if pb.ShowFinalTime { - var left time.Duration - left = (fromStart / time.Second) * time.Second + var left = (fromStart / time.Second) * time.Second timeLeftBox = fmt.Sprintf(" %s", left.String()) } default: @@ -359,7 +358,7 @@ func (pb *ProgressBar) write(total, current int64) { // speed if pb.ShowSpeed && currentFromStart > 0 { - fromStart := time.Now().Sub(pb.startTime) + fromStart := time.Since(pb.startTime) speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second)) speedBox = " " + Format(int64(speed)).To(pb.Units).Width(pb.UnitsWidth).PerSec().String() } @@ -466,7 +465,7 @@ func (pb *ProgressBar) Update() { if c == 0 { pb.startTime = time.Now() pb.startValue = 0 - } else if c >= t && pb.isFinish != true { + } else if c >= t && !pb.isFinish{ pb.Finish() } } diff --git a/pool.go b/pool.go index 58df31a..4ac11fa 100644 --- a/pool.go +++ b/pool.go @@ -64,7 +64,7 @@ func (p *Pool) Start() (err error) { func (p *Pool) writer() { var first = true defer func() { - if first == false { + if !first { p.print(false) } else { p.print(true) @@ -87,7 +87,7 @@ func (p *Pool) writer() { } } -// Restore terminal state and close pool +// Stop Restore terminal state and close pool func (p *Pool) Stop() error { p.finishOnce.Do(func() { if p.shutdownCh != nil { @@ -96,9 +96,9 @@ func (p *Pool) Stop() error { }) // Wait for the worker to complete - select { - case <-p.workerCh: - } + + <-p.workerCh + return unlockEcho() } From 8228b60badb941f9c13df6af79ea596bbd5d5718 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Thu, 18 May 2023 15:54:57 +0800 Subject: [PATCH 2/2] code optimization --- pool.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pool.go b/pool.go index 4ac11fa..d641a7e 100644 --- a/pool.go +++ b/pool.go @@ -96,7 +96,6 @@ func (p *Pool) Stop() error { }) // Wait for the worker to complete - <-p.workerCh