rkt: progress bar when Content-Length is unknown

This commit is contained in:
Jimmy Zelinskie
2015-04-30 19:44:32 -04:00
parent 798e0ea158
commit 153a17b419
8 changed files with 29 additions and 21 deletions
+4 -4
View File
@@ -106,6 +106,10 @@
"Comment": "v2-41-g415a79c4bfa3",
"Rev": "415a79c4bfa377b484b16c632347b3925a0f35ff"
},
{
"ImportPath": "github.com/coreos/ioprogress",
"Rev": "326b3ee6276a33c8bdcf480ca9c99228942ab970"
},
{
"ImportPath": "github.com/cznic/b",
"Rev": "e2e747ce049fb910cff6b1fd7ad8faf3900939d5"
@@ -164,10 +168,6 @@
"Comment": "release.r56-25-g05017fc",
"Rev": "05017fcccf23c823bfdea560dcc958a136e54fb7"
},
{
"ImportPath": "github.com/mitchellh/ioprogress",
"Rev": "c2b2845d63abaf335647a8b9c7437e6c11a2a613"
},
{
"ImportPath": "github.com/petar/GoLLRB/llrb",
"Rev": "53be0d36a84c2a886ca057d34b6aa4468df9ccb4"
@@ -34,7 +34,7 @@ func DrawTerminalf(w io.Writer, f DrawTextFormatFunc) DrawFunc {
var maxLength int
return func(progress, total int64) error {
if progress == -1 || total == -1 {
if progress == -1 && total == -1 {
_, err := fmt.Fprintf(w, "\n")
return err
}
@@ -60,7 +60,7 @@ var byteUnits = []string{"B", "KB", "MB", "GB", "TB", "PB"}
// DrawTextFormatBytes is a DrawTextFormatFunc that formats the progress
// and total into human-friendly byte formats.
func DrawTextFormatBytes(progress, total int64) string {
return fmt.Sprintf("%s/%s", byteUnitStr(progress), byteUnitStr(total))
return fmt.Sprintf("%s/%s", ByteUnitStr(progress), ByteUnitStr(total))
}
// DrawTextFormatBar returns a DrawTextFormatFunc that draws a progress
@@ -84,11 +84,12 @@ func DrawTextFormatBar(width int64) DrawTextFormatFunc {
return fmt.Sprintf(
"[%s%s]",
strings.Repeat("=", int(current)),
strings.Repeat(" ", int(width - current)))
strings.Repeat(" ", int(width-current)))
}
}
func byteUnitStr(n int64) string {
// ByteUnitStr pretty prints a number of bytes.
func ByteUnitStr(n int64) string {
var unit string
size := float64(n)
for i := 1; i < len(byteUnits); i++ {
@@ -47,8 +47,8 @@ func TestDrawTextFormatBar(t *testing.T) {
}
func TestDrawTextFormatBytes(t *testing.T) {
cases := []struct{
P, T int64
cases := []struct {
P, T int64
Output string
}{
{
@@ -80,16 +80,15 @@ func (r *Reader) drawProgress() {
}
func (r *Reader) finishProgress() {
// Only output the final draw if we drawed prior
if !r.lastDraw.IsZero() {
f := r.drawFunc()
f(r.progress, r.Size)
f(-1, -1)
f := r.drawFunc()
f(r.progress, r.Size)
// Reset lastDraw so we don't finish again
var zeroDraw time.Time
r.lastDraw = zeroDraw
}
// Print a newline
f(-1, -1)
// Reset lastDraw so we don't finish again
var zeroDraw time.Time
r.lastDraw = zeroDraw
}
func (r *Reader) initProgress() {
@@ -31,7 +31,7 @@ func TestReader(t *testing.T) {
}
}
const drawReaderStr = "0/6\r2/6\r4/6\r6/6\r\n"
const drawReaderStr = "0/6\r2/6\r4/6\r6/6\r6/6\r\n"
// testReader is a test structure to help with testing the Reader by
// returning fixed slices of data.
+9 -1
View File
@@ -32,7 +32,7 @@ import (
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/aci"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/discovery"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/mitchellh/ioprogress"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/coreos/ioprogress"
"github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/crypto/openpgp"
"github.com/coreos/rkt/common/apps"
"github.com/coreos/rkt/pkg/keystore"
@@ -478,6 +478,14 @@ func (f *fetcher) downloadHTTP(url, label string, out writeSyncer) error {
barSize := int64(80 - len(prefix) - fmtBytesSize)
bar := ioprogress.DrawTextFormatBar(barSize)
fmtfunc := func(progress, total int64) string {
// Content-Length is set to -1 when unknown.
if total == -1 {
return fmt.Sprintf(
"%s: %v of an unknown total size",
prefix,
ioprogress.ByteUnitStr(progress),
)
}
return fmt.Sprintf(
"%s: %s %s",
prefix,