@cryptotaxi247 / kubo / commits / 7caba7e46

Use common progressbar function for cat and get

License: MIT Signed-off-by: rht <rhtbot@gmail.com>

rht committed Sep 26, 2015 at 22:17 UTC 7caba7e46a06453fdd9b32b3c9f6a7faee8b0d13
2 files changed +29 -24
core/commands/cat.go
+2 -19
@@ -1,24 +1,17 @@
1 package commands
2
3 import (
4 - "fmt"
4 "io"
5
6 cmds "github.com/ipfs/go-ipfs/commands"
7 core "github.com/ipfs/go-ipfs/core"
8 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
9
11 - "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11 )
12
13 const progressBarMinSize = 1024 * 1024 * 8 // show progress bar for outputs > 8MiB
14
17 -type clearlineReader struct {
18 - io.Reader
19 - out io.Writer
20 -}
21 -
15 var CatCmd = &cmds.Command{
16 Helptext: cmds.HelpText{
17 Tagline: "Show IPFS object data",
@@ -54,12 +47,10 @@ it contains.
47 return
48 }
49
57 - bar := pb.New(int(res.Length())).SetUnits(pb.U_BYTES)
58 - bar.Output = res.Stderr()
50 + bar, reader := progressBarForReader(res.Stderr(), res.Output().(io.Reader), int64(res.Length()))
51 bar.Start()
52
61 - reader := bar.NewProxyReader(res.Output().(io.Reader))
62 - res.SetOutput(&clearlineReader{reader, res.Stderr()})
53 + res.SetOutput(reader)
54 },
55 }
56
@@ -76,11 +67,3 @@ func cat(ctx context.Context, node *core.IpfsNode, paths []string) ([]io.Reader,
67 }
68 return readers, length, nil
69 }
79 -
80 -func (r *clearlineReader) Read(p []byte) (n int, err error) {
81 - n, err = r.Reader.Read(p)
82 - if err == io.EOF {
83 - fmt.Fprintf(r.out, "\033[2K\r") // clear progress bar line on EOF
84 - }
85 - return
86 -}
core/commands/get.go
+27 -5
@@ -112,13 +112,35 @@ may also specify the level of compression by specifying '-l=<1-9>'.
112 },
113 }
114
115 -func progressBarForReader(out io.Writer, r io.Reader) (*pb.ProgressBar, *pb.Reader) {
115 +type clearlineReader struct {
116 + io.Reader
117 + out io.Writer
118 +}
119 +
120 +func (r *clearlineReader) Read(p []byte) (n int, err error) {
121 + n, err = r.Reader.Read(p)
122 + if err == io.EOF {
123 + // callback
124 + fmt.Fprintf(r.out, "\033[2K\r") // clear progress bar line on EOF
125 + }
126 + return
127 +}
128 +
129 +func progressBarForReader(out io.Writer, r io.Reader, l int64) (*pb.ProgressBar, io.Reader) {
130 // setup bar reader
131 // TODO: get total length of files
118 - bar := pb.New(0).SetUnits(pb.U_BYTES)
132 + bar := pb.New64(l).SetUnits(pb.U_BYTES)
133 bar.Output = out
134 +
135 + // the progress bar lib doesn't give us a way to get the width of the output,
136 + // so as a hack we just use a callback to measure the output, then git rid of it
137 + bar.Callback = func(line string) {
138 + terminalWidth := len(line)
139 + bar.Callback = nil
140 + log.Infof("terminal width: %v\n", terminalWidth)
141 + }
142 barR := bar.NewProxyReader(r)
121 - return bar, barR
143 + return bar, &clearlineReader{barR, out}
144 }
145
146 type getWriter struct {
@@ -159,7 +181,7 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
181 defer file.Close()
182
183 fmt.Fprintf(gw.Out, "Saving archive to %s\n", fpath)
162 - bar, barR := progressBarForReader(gw.Err, r)
184 + bar, barR := progressBarForReader(gw.Err, r, 0)
185 bar.Start()
186 defer bar.Finish()
187
@@ -169,7 +191,7 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
191
192 func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
193 fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
172 - bar, barR := progressBarForReader(gw.Err, r)
194 + bar, barR := progressBarForReader(gw.Err, r, 0)
195 bar.Start()
196 defer bar.Finish()
197