Clear progress bar on `ipfs cat` exit
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jul 24, 2015 at 23:04 UTC
818d3af8273028c6b1909e96d4d75c52a038567f
2 files changed
+16
-3
core/commands/add.go
+1
-2
@@ -4,7 +4,6 @@ import (
4
"fmt"
5
"io"
6
"path"
7
- "strings"
7
8
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
9
@@ -208,7 +207,7 @@ remains to be implemented.
207
if len(output.Hash) > 0 {
208
if showProgressBar {
209
// clear progress bar line before we print "added x" output
211
- fmt.Fprintf(res.Stderr(), "\r%s\r", strings.Repeat(" ", terminalWidth))
210
+ fmt.Fprintf(res.Stderr(), "\033[2K\r")
211
}
212
if quiet {
213
fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
core/commands/cat.go
+15
-1
@@ -1,6 +1,7 @@
1
package commands
2
3
import (
4
+ "fmt"
5
"io"
6
7
cmds "github.com/ipfs/go-ipfs/commands"
@@ -14,6 +15,11 @@ import (
15
16
const progressBarMinSize = 1024 * 1024 * 8 // show progress bar for outputs > 8MiB
17
18
+type clearlineReader struct {
19
+ io.Reader
20
+ out io.Writer
21
+}
22
+
23
var CatCmd = &cmds.Command{
24
Helptext: cmds.HelpText{
25
Tagline: "Show IPFS object data",
@@ -54,7 +60,7 @@ it contains.
60
bar.Start()
61
62
reader := bar.NewProxyReader(res.Output().(io.Reader))
57
- res.SetOutput(reader)
63
+ res.SetOutput(&clearlineReader{reader, res.Stderr()})
64
},
65
}
66
@@ -76,3 +82,11 @@ func cat(ctx context.Context, node *core.IpfsNode, paths []string) ([]io.Reader,
82
}
83
return readers, length, nil
84
}
85
+
86
+func (r *clearlineReader) Read(p []byte) (n int, err error) {
87
+ n, err = r.Reader.Read(p)
88
+ if err == io.EOF {
89
+ fmt.Fprintf(r.out, "\033[2K\r") // clear progress bar line on EOF
90
+ }
91
+ return
92
+}