Skip to content

Commit

Permalink
Merge pull request #205 from testwill/master
Browse files Browse the repository at this point in the history
code optimization
  • Loading branch information
cheggaaa committed May 18, 2023
2 parents 7932cf5 + 8228b60 commit aa5c836
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.idea/
15 changes: 7 additions & 8 deletions format_test.go
@@ -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
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
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()
}

0 comments on commit aa5c836

Please sign in to comment.