-
Notifications
You must be signed in to change notification settings - Fork 991
Expand file tree
/
Copy pathprogress_bar.go
More file actions
47 lines (36 loc) · 858 Bytes
/
progress_bar.go
File metadata and controls
47 lines (36 loc) · 858 Bytes
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
package v7action
import (
"io"
"os"
"time"
"github.com/cheggaaa/pb/v3"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . SimpleProgressBar
type SimpleProgressBar interface {
Initialize(path string) (io.Reader, int64, error)
Terminate()
}
type ProgressBar struct {
bar *pb.ProgressBar
}
func NewProgressBar() *ProgressBar {
return &ProgressBar{}
}
func (p *ProgressBar) Initialize(path string) (io.Reader, int64, error) {
file, err := os.Open(path)
if err != nil {
return nil, 0, err
}
fileInfo, err := file.Stat()
if err != nil {
return nil, 0, err
}
p.bar = pb.New(int(fileInfo.Size())).Set(pb.Bytes, true)
p.bar.Start()
return p.bar.NewProxyReader(file), fileInfo.Size(), nil
}
func (p *ProgressBar) Terminate() {
// Adding sleep to ensure UI has finished drawing
time.Sleep(time.Second)
p.bar.Finish()
}