forked from camh-/systemd-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressreader.go
55 lines (52 loc) · 1.54 KB
/
progressreader.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
package utils
import (
"io"
"time"
)
// Reader with progress bar
type progressReader struct {
reader io.ReadCloser // Stream to read from
output io.Writer // Where to send progress bar to
progress JSONProgress
lastUpdate int // How many bytes read at least update
ID string
action string
sf *StreamFormatter
newLine bool
}
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := r.reader.Read(p)
r.progress.Current += read
updateEvery := 1024 * 512 //512kB
if r.progress.Total > 0 {
// Update progress for every 1% read if 1% < 512kB
if increment := int(0.01 * float64(r.progress.Total)); increment < updateEvery {
updateEvery = increment
}
}
if r.progress.Current-r.lastUpdate > updateEvery || err != nil {
r.output.Write(r.sf.FormatProgress(r.ID, r.action, &r.progress))
r.lastUpdate = r.progress.Current
}
// Send newline when complete
if r.newLine && err != nil && read == 0 {
r.output.Write(r.sf.FormatStatus("", ""))
}
return read, err
}
func (r *progressReader) Close() error {
r.progress.Current = r.progress.Total
r.output.Write(r.sf.FormatProgress(r.ID, r.action, &r.progress))
return r.reader.Close()
}
func ProgressReader(r io.ReadCloser, size int, output io.Writer, sf *StreamFormatter, newline bool, ID, action string) *progressReader {
return &progressReader{
reader: r,
output: NewWriteFlusher(output),
ID: ID,
action: action,
progress: JSONProgress{Total: size, Start: time.Now().UTC().Unix()},
sf: sf,
newLine: newline,
}
}