@@ -2,7 +2,7 @@ package meter
2
2
3
3
import (
4
4
"fmt"
5
- "os "
5
+ "io "
6
6
"sync"
7
7
"sync/atomic"
8
8
"time"
@@ -30,9 +30,10 @@ type Progress interface {
30
30
var Spinners = []string {"|" , "(" , "<" , "-" , "<" , "(" , "|" , ")" , ">" , "-" , ">" , ")" }
31
31
32
32
// progressMeter is a `Progress` that reports the current state every
33
- // `period`.
33
+ // `period` to an `io.Writer` .
34
34
type progressMeter struct {
35
35
lock sync.Mutex
36
+ w io.Writer
36
37
format string
37
38
period time.Duration
38
39
lastShownCount int64
@@ -48,8 +49,9 @@ type progressMeter struct {
48
49
// NewProgressMeter returns a progress meter that can be used to show
49
50
// progress to a TTY periodically, including an increasing int64
50
51
// value.
51
- func NewProgressMeter (period time.Duration ) Progress {
52
+ func NewProgressMeter (w io. Writer , period time.Duration ) Progress {
52
53
return & progressMeter {
54
+ w : w ,
53
55
period : period ,
54
56
}
55
57
}
@@ -81,7 +83,7 @@ func (p *progressMeter) Start(format string) {
81
83
} else {
82
84
s = ""
83
85
}
84
- fmt .Fprintf (os . Stderr , p .format , c , s , "\r " )
86
+ fmt .Fprintf (p . w , p .format , c , s , "\r " )
85
87
p .lock .Unlock ()
86
88
}
87
89
}()
@@ -100,7 +102,7 @@ func (p *progressMeter) Done() {
100
102
defer p .lock .Unlock ()
101
103
p .ticker = nil
102
104
c := atomic .LoadInt64 (& p .count )
103
- fmt .Fprintf (os . Stderr , p .format , c , " " , "\n " )
105
+ fmt .Fprintf (p . w , p .format , c , " " , "\n " )
104
106
}
105
107
106
108
// NoProgressMeter is a `Progress` that doesn't actually report
0 commit comments