Skip to content

Commit

Permalink
fix progress bar on ipfs get
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Mar 7, 2017
1 parent 60b94a2 commit 169f789
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
16 changes: 11 additions & 5 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ func (r *clearlineReader) Read(p []byte) (n int, err error) {
}

func progressBarForReader(out io.Writer, r io.Reader, l int64) (*pb.ProgressBar, io.Reader) {
bar := makeProgressBar(out, l)
barR := bar.NewProxyReader(r)
return bar, &clearlineReader{barR, out}
}

func makeProgressBar(out io.Writer, l int64) *pb.ProgressBar {
// setup bar reader
// TODO: get total length of files
bar := pb.New64(l).SetUnits(pb.U_BYTES)
Expand All @@ -155,8 +161,7 @@ func progressBarForReader(out io.Writer, r io.Reader, l int64) (*pb.ProgressBar,
bar.Callback = nil
log.Infof("terminal width: %v\n", terminalWidth)
}
barR := bar.NewProxyReader(r)
return bar, &clearlineReader{barR, out}
return bar
}

type getWriter struct {
Expand Down Expand Up @@ -208,12 +213,13 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {

func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
bar, barR := progressBarForReader(gw.Err, r, gw.Size)
bar := makeProgressBar(gw.Err, gw.Size)
bar.Start()
defer bar.Finish()
defer bar.Set64(gw.Size)

extractor := &tar.Extractor{fpath}
return extractor.Extract(barR)
extractor := &tar.Extractor{fpath, bar.Add64}
return extractor.Extract(r)
}

func getCompressOptions(req cmds.Request) (int, error) {
Expand Down
25 changes: 23 additions & 2 deletions thirdparty/tar/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type Extractor struct {
Path string
Path string
Progress func(int64) int64
}

func (te *Extractor) Extract(reader io.Reader) error {
Expand Down Expand Up @@ -111,10 +112,30 @@ func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootEx
}
defer file.Close()

_, err = io.Copy(file, r)
err = copyWithProgress(file, r, te.Progress)
if err != nil {
return err
}

return nil
}

func copyWithProgress(to io.Writer, from io.Reader, cb func(int64) int64) error {
buf := make([]byte, 4096)
for {
n, err := from.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}

cb(int64(n))
_, err = to.Write(buf[:n])
if err != nil {
return err
}
}

}

0 comments on commit 169f789

Please sign in to comment.