Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code optimization #205

Merged
merged 2 commits into from
May 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
15 changes: 7 additions & 8 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pb

import (
"fmt"
"strconv"
"testing"
"time"
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
9 changes: 4 additions & 5 deletions pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}
}
Expand Down
9 changes: 4 additions & 5 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -96,9 +96,8 @@ func (p *Pool) Stop() error {
})

// Wait for the worker to complete
select {
case <-p.workerCh:
}
<-p.workerCh


return unlockEcho()
}