From 818d3af8273028c6b1909e96d4d75c52a038567f Mon Sep 17 00:00:00 2001 From: rht Date: Fri, 24 Jul 2015 23:04:57 +0700 Subject: [PATCH] Clear progress bar on `ipfs cat` exit License: MIT Signed-off-by: rht --- core/commands/add.go | 3 +-- core/commands/cat.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index a3e438e3238..6b96d8f9035 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "path" - "strings" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb" @@ -208,7 +207,7 @@ remains to be implemented. if len(output.Hash) > 0 { if showProgressBar { // clear progress bar line before we print "added x" output - fmt.Fprintf(res.Stderr(), "\r%s\r", strings.Repeat(" ", terminalWidth)) + fmt.Fprintf(res.Stderr(), "\033[2K\r") } if quiet { fmt.Fprintf(res.Stdout(), "%s\n", output.Hash) diff --git a/core/commands/cat.go b/core/commands/cat.go index 8da37c576a1..bea51067c37 100644 --- a/core/commands/cat.go +++ b/core/commands/cat.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "io" cmds "github.com/ipfs/go-ipfs/commands" @@ -14,6 +15,11 @@ import ( const progressBarMinSize = 1024 * 1024 * 8 // show progress bar for outputs > 8MiB +type clearlineReader struct { + io.Reader + out io.Writer +} + var CatCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Show IPFS object data", @@ -54,7 +60,7 @@ it contains. bar.Start() reader := bar.NewProxyReader(res.Output().(io.Reader)) - res.SetOutput(reader) + res.SetOutput(&clearlineReader{reader, res.Stderr()}) }, } @@ -76,3 +82,11 @@ func cat(ctx context.Context, node *core.IpfsNode, paths []string) ([]io.Reader, } return readers, length, nil } + +func (r *clearlineReader) Read(p []byte) (n int, err error) { + n, err = r.Reader.Read(p) + if err == io.EOF { + fmt.Fprintf(r.out, "\033[2K\r") // clear progress bar line on EOF + } + return +}