Skip to content

Commit aa5c836

Browse files
authored
Merge pull request #205 from testwill/master
code optimization
2 parents 7932cf5 + 8228b60 commit aa5c836

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

format_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pb
22

33
import (
4-
"fmt"
54
"strconv"
65
"testing"
76
"time"
@@ -13,7 +12,7 @@ func Test_DefaultsToInteger(t *testing.T) {
1312
actual := Format(value).String()
1413

1514
if actual != expected {
16-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
15+
t.Errorf("Expected {%s} was {%s}", expected, actual)
1716
}
1817
}
1918

@@ -23,7 +22,7 @@ func Test_CanFormatAsInteger(t *testing.T) {
2322
actual := Format(value).To(U_NO).String()
2423

2524
if actual != expected {
26-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
25+
t.Errorf("Expected {%s} was {%s}", expected, actual)
2726
}
2827
}
2928

@@ -42,7 +41,7 @@ func Test_CanFormatAsBytes(t *testing.T) {
4241
for _, input := range inputs {
4342
actual := Format(input.v).To(U_BYTES).String()
4443
if actual != input.e {
45-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
44+
t.Errorf("Expected {%s} was {%s}", input.e, actual)
4645
}
4746
}
4847
}
@@ -62,7 +61,7 @@ func Test_CanFormatAsBytesDec(t *testing.T) {
6261
for _, input := range inputs {
6362
actual := Format(input.v).To(U_BYTES_DEC).String()
6463
if actual != input.e {
65-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
64+
t.Errorf("Expected {%s} was {%s}", input.e, actual)
6665
}
6766
}
6867
}
@@ -72,7 +71,7 @@ func Test_CanFormatDuration(t *testing.T) {
7271
expected := "10m00s"
7372
actual := Format(int64(value)).To(U_DURATION).String()
7473
if actual != expected {
75-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
74+
t.Errorf("Expected {%s} was {%s}", expected, actual)
7675
}
7776
}
7877

@@ -81,7 +80,7 @@ func Test_CanFormatLongDuration(t *testing.T) {
8180
expected := "2d14h00m13s"
8281
actual := Format(int64(value)).To(U_DURATION).String()
8382
if actual != expected {
84-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
83+
t.Errorf("Expected {%s} was {%s}", expected, actual)
8584
}
8685
}
8786

@@ -90,6 +89,6 @@ func Test_DefaultUnitsWidth(t *testing.T) {
9089
expected := " 10"
9190
actual := Format(int64(value)).Width(7).String()
9291
if actual != expected {
93-
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
92+
t.Errorf("Expected {%s} was {%s}", expected, actual)
9493
}
9594
}

pb.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (pb *ProgressBar) write(total, current int64) {
322322

323323
// time left
324324
currentFromStart := current - pb.startValue
325-
fromStart := time.Now().Sub(pb.startTime)
325+
fromStart := time.Since(pb.startTime)
326326
lastChangeTime := pb.changeTime
327327
fromChange := lastChangeTime.Sub(pb.startTime)
328328

@@ -333,8 +333,7 @@ func (pb *ProgressBar) write(total, current int64) {
333333
select {
334334
case <-pb.finish:
335335
if pb.ShowFinalTime {
336-
var left time.Duration
337-
left = (fromStart / time.Second) * time.Second
336+
var left = (fromStart / time.Second) * time.Second
338337
timeLeftBox = fmt.Sprintf(" %s", left.String())
339338
}
340339
default:
@@ -359,7 +358,7 @@ func (pb *ProgressBar) write(total, current int64) {
359358

360359
// speed
361360
if pb.ShowSpeed && currentFromStart > 0 {
362-
fromStart := time.Now().Sub(pb.startTime)
361+
fromStart := time.Since(pb.startTime)
363362
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
364363
speedBox = " " + Format(int64(speed)).To(pb.Units).Width(pb.UnitsWidth).PerSec().String()
365364
}
@@ -466,7 +465,7 @@ func (pb *ProgressBar) Update() {
466465
if c == 0 {
467466
pb.startTime = time.Now()
468467
pb.startValue = 0
469-
} else if c >= t && pb.isFinish != true {
468+
} else if c >= t && !pb.isFinish{
470469
pb.Finish()
471470
}
472471
}

pool.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (p *Pool) Start() (err error) {
6464
func (p *Pool) writer() {
6565
var first = true
6666
defer func() {
67-
if first == false {
67+
if !first {
6868
p.print(false)
6969
} else {
7070
p.print(true)
@@ -87,7 +87,7 @@ func (p *Pool) writer() {
8787
}
8888
}
8989

90-
// Restore terminal state and close pool
90+
// Stop Restore terminal state and close pool
9191
func (p *Pool) Stop() error {
9292
p.finishOnce.Do(func() {
9393
if p.shutdownCh != nil {
@@ -96,9 +96,8 @@ func (p *Pool) Stop() error {
9696
})
9797

9898
// Wait for the worker to complete
99-
select {
100-
case <-p.workerCh:
101-
}
99+
<-p.workerCh
100+
102101

103102
return unlockEcho()
104103
}

0 commit comments

Comments
 (0)