mirror of https://github.com/docker/docs.git
add progressbar and time
This commit is contained in:
parent
597e0e69b4
commit
ebc36b879d
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,8 +18,9 @@ func (e *JSONError) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONProgress struct {
|
type JSONProgress struct {
|
||||||
Current int `json:"current,omitempty"`
|
Current int `json:"current,omitempty"`
|
||||||
Total int `json:"total,omitempty"`
|
Total int `json:"total,omitempty"`
|
||||||
|
Start int64 `json:"start,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *JSONProgress) String() string {
|
func (p *JSONProgress) String() string {
|
||||||
|
@ -30,8 +32,13 @@ func (p *JSONProgress) String() string {
|
||||||
return fmt.Sprintf("%8v/?", current)
|
return fmt.Sprintf("%8v/?", current)
|
||||||
}
|
}
|
||||||
total := HumanSize(int64(p.Total))
|
total := HumanSize(int64(p.Total))
|
||||||
percentage := float64(p.Current) / float64(p.Total) * 100
|
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
|
||||||
return fmt.Sprintf("%8v/%v (%.0f%%)", current, total, percentage)
|
|
||||||
|
fromStart := time.Now().UTC().Sub(time.Unix(int64(p.Start), 0))
|
||||||
|
perEntry := fromStart / time.Duration(p.Current)
|
||||||
|
left := time.Duration(p.Total-p.Current) * perEntry
|
||||||
|
left = (left / time.Second) * time.Second
|
||||||
|
return fmt.Sprintf("[%s>%s] %8v/%v %s", strings.Repeat("=", percentage), strings.Repeat(" ", 50-percentage), current, total, left.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONMessage struct {
|
type JSONMessage struct {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reader with progress bar
|
// Reader with progress bar
|
||||||
|
@ -48,7 +49,7 @@ func ProgressReader(r io.ReadCloser, size int, output io.Writer, sf *StreamForma
|
||||||
output: NewWriteFlusher(output),
|
output: NewWriteFlusher(output),
|
||||||
ID: ID,
|
ID: ID,
|
||||||
action: action,
|
action: action,
|
||||||
progress: JSONProgress{Total: size},
|
progress: JSONProgress{Total: size, Start: time.Now().UTC().Unix()},
|
||||||
sf: sf,
|
sf: sf,
|
||||||
newLine: newline,
|
newLine: newline,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue