-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
55 lines (42 loc) · 922 Bytes
/
progress.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 main
import (
"fmt"
"time"
)
type Progress struct {
transfer *transfer
}
func (progress *Progress) start() {
go progress.transfer.startTransfer()
progress.show()
progress.transfer.Response.Body.Close()
progress.transfer.File.Close()
}
func (progress *Progress) show() {
var downlod int
totalBytes := int(progress.transfer.ContentLength)
lastTime := false
for !progress.transfer.Done || lastTime {
fmt.Print("\r[")
bytesDone := progress.transfer.bytesTransfered()
downlod = 40 * bytesDone / totalBytes
for i := 0; i < 40; i++ {
if i < downlod {
fmt.Print("=")
} else if i == downlod {
fmt.Print(">")
} else {
fmt.Print(" ")
}
}
fmt.Print("] ")
fmt.Printf("%d/%dkB", bytesDone/1000, totalBytes/1000)
time.Sleep(100 * time.Millisecond)
if progress.transfer.Done && !lastTime {
lastTime = true
} else {
lastTime = false
}
}
fmt.Println()
}