Commit 121dfb10 authored by Matt Bell's avatar Matt Bell

core/commands: Fixed progress bar line clear race condition in 'add'

parent 2363cb63
package commands package commands
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
...@@ -108,9 +107,13 @@ remains to be implemented. ...@@ -108,9 +107,13 @@ remains to be implemented.
res.SetError(u.ErrCast(), cmds.ErrNormal) res.SetError(u.ErrCast(), cmds.ErrNormal)
return return
} }
res.SetOutput(nil)
wrapperChan := make(chan interface{}) quiet, _, err := res.Request().Option("quiet").Bool()
res.SetOutput((<-chan interface{})(wrapperChan)) if err != nil {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
size := int64(0) size := int64(0)
s, found := res.Request().Values()["size"] s, found := res.Request().Values()["size"]
...@@ -138,78 +141,45 @@ remains to be implemented. ...@@ -138,78 +141,45 @@ remains to be implemented.
bar.Update() bar.Update()
} }
go func() { lastFile := ""
lastFile := "" var totalProgress, prevFiles, lastBytes int64
var totalProgress, prevFiles, lastBytes int64
for out := range outChan {
output := out.(*AddedObject)
if len(output.Hash) > 0 {
if showProgressBar {
// clear progress bar line before we print "added x" output
fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth))
}
wrapperChan <- output
} else {
log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
if !showProgressBar {
continue
}
if len(lastFile) == 0 {
lastFile = output.Name
}
if output.Name != lastFile || output.Bytes < lastBytes {
prevFiles += lastBytes
lastFile = output.Name
}
lastBytes = output.Bytes
delta := prevFiles + lastBytes - totalProgress
totalProgress = bar.Add64(delta)
}
for out := range outChan {
output := out.(*AddedObject)
if len(output.Hash) > 0 {
if showProgressBar { if showProgressBar {
bar.Update() // clear progress bar line before we print "added x" output
fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth))
}
if quiet {
fmt.Printf("%s\n", output.Hash)
} else {
fmt.Printf("added %s %s\n", output.Hash, output.Name)
} }
}
close(wrapperChan)
}()
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
outChan, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}
quiet, _, err := res.Request().Option("quiet").Bool() } else {
if err != nil { log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
return nil, err
}
marshal := func(v interface{}) (io.Reader, error) { if !showProgressBar {
obj, ok := v.(*AddedObject) continue
if !ok {
return nil, u.ErrCast()
} }
var buf bytes.Buffer if len(lastFile) == 0 {
if quiet { lastFile = output.Name
buf.WriteString(fmt.Sprintf("%s\n", obj.Hash)) }
} else { if output.Name != lastFile || output.Bytes < lastBytes {
buf.WriteString(fmt.Sprintf("added %s %s\n", obj.Hash, obj.Name)) prevFiles += lastBytes
lastFile = output.Name
} }
return &buf, nil lastBytes = output.Bytes
delta := prevFiles + lastBytes - totalProgress
totalProgress = bar.Add64(delta)
} }
return &cmds.ChannelMarshaler{ if showProgressBar {
Channel: outChan, bar.Update()
Marshaler: marshal, }
}, nil }
},
}, },
Type: AddedObject{}, Type: AddedObject{},
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment