Browse code

add progressbar and time

Victor Vieux authored on 2013/11/29 05:37:07
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6 6
 	"io"
7
+	"strings"
7 8
 	"time"
8 9
 )
9 10
 
... ...
@@ -17,8 +18,9 @@ func (e *JSONError) Error() string {
17 17
 }
18 18
 
19 19
 type JSONProgress struct {
20
-	Current int `json:"current,omitempty"`
21
-	Total   int `json:"total,omitempty"`
20
+	Current int   `json:"current,omitempty"`
21
+	Total   int   `json:"total,omitempty"`
22
+	Start   int64 `json:"start,omitempty"`
22 23
 }
23 24
 
24 25
 func (p *JSONProgress) String() string {
... ...
@@ -30,8 +32,13 @@ func (p *JSONProgress) String() string {
30 30
 		return fmt.Sprintf("%8v/?", current)
31 31
 	}
32 32
 	total := HumanSize(int64(p.Total))
33
-	percentage := float64(p.Current) / float64(p.Total) * 100
34
-	return fmt.Sprintf("%8v/%v (%.0f%%)", current, total, percentage)
33
+	percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
34
+
35
+	fromStart := time.Now().UTC().Sub(time.Unix(int64(p.Start), 0))
36
+	perEntry := fromStart / time.Duration(p.Current)
37
+	left := time.Duration(p.Total-p.Current) * perEntry
38
+	left = (left / time.Second) * time.Second
39
+	return fmt.Sprintf("[%s>%s] %8v/%v %s", strings.Repeat("=", percentage), strings.Repeat(" ", 50-percentage), current, total, left.String())
35 40
 }
36 41
 
37 42
 type JSONMessage struct {
... ...
@@ -2,6 +2,7 @@ package utils
2 2
 
3 3
 import (
4 4
 	"io"
5
+	"time"
5 6
 )
6 7
 
7 8
 // Reader with progress bar
... ...
@@ -48,7 +49,7 @@ func ProgressReader(r io.ReadCloser, size int, output io.Writer, sf *StreamForma
48 48
 		output:   NewWriteFlusher(output),
49 49
 		ID:       ID,
50 50
 		action:   action,
51
-		progress: JSONProgress{Total: size},
51
+		progress: JSONProgress{Total: size, Start: time.Now().UTC().Unix()},
52 52
 		sf:       sf,
53 53
 		newLine:  newline,
54 54
 	}