-
Notifications
You must be signed in to change notification settings - Fork 44
/
progressbar.go
59 lines (51 loc) · 1.65 KB
/
progressbar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package desync
import (
"fmt"
"os"
"time"
"golang.org/x/crypto/ssh/terminal"
pb "gopkg.in/cheggaaa/pb.v1"
)
// NewProgressBar initializes a wrapper for a https://github.com/cheggaaa/pb
// progressbar that implements desync.ProgressBar
func NewProgressBar(prefix string) ProgressBar {
if !terminal.IsTerminal(int(os.Stderr.Fd())) &&
os.Getenv("DESYNC_PROGRESSBAR_ENABLED") == "" &&
os.Getenv("DESYNC_ENABLE_PARSABLE_PROGRESS") == "" {
return NullProgressBar{}
}
bar := pb.New(0).Prefix(prefix)
bar.ShowCounters = false
bar.Output = os.Stderr
if os.Getenv("DESYNC_ENABLE_PARSABLE_PROGRESS") != "" {
// This is likely going to a journal or redirected to a file, lower the
// refresh rate from the default 200ms to a more manageable 500ms.
bar.SetRefreshRate(time.Millisecond * 500)
bar.ShowBar = false
// Write every progress update in a separate line, instead of using
// the default carriage returns.
bar.Callback = func(s string) { fmt.Fprintln(os.Stderr, s) }
bar.Output = nil
}
return DefaultProgressBar{bar}
}
// DefaultProgressBar wraps https://github.com/cheggaaa/pb and implements desync.ProgressBar
type DefaultProgressBar struct {
*pb.ProgressBar
}
// SetTotal sets the upper bounds for the progress bar
func (p DefaultProgressBar) SetTotal(total int) {
p.ProgressBar.SetTotal(total)
}
// Start displaying the progress bar
func (p DefaultProgressBar) Start() {
p.ProgressBar.Start()
}
// Set the current value
func (p DefaultProgressBar) Set(current int) {
p.ProgressBar.Set(current)
}
// Write the current state of the progressbar
func (p DefaultProgressBar) Write(b []byte) (n int, err error) {
return p.ProgressBar.Write(b)
}