From 962cacec2bdc5f72d0732f4228deb6508a993f6a Mon Sep 17 00:00:00 2001 From: Mohamed Elbahja Date: Wed, 12 Aug 2020 21:47:49 +0100 Subject: [PATCH] improve cli stdout progress. --- cmd/got/main.go | 23 +++++++++++++++++------ go.mod | 2 ++ go.sum | 6 ++++++ got.go | 1 + 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cmd/got/main.go b/cmd/got/main.go index 5c5fa83..fadf5fa 100644 --- a/cmd/got/main.go +++ b/cmd/got/main.go @@ -4,8 +4,10 @@ import ( "flag" "fmt" "log" + "os" "time" + "github.com/apoorvam/goterminal" "github.com/dustin/go-humanize" "github.com/melbahja/got" ) @@ -58,24 +60,33 @@ func main() { log.Fatal(err) } + // Goterm writer. + writer := goterminal.New(os.Stdout) + // Set progress func to update cli output. d.Progress.ProgressFunc = func(p *got.Progress, d *got.Download) { - fmt.Printf( - "\r\r\bTotal: %s | Chunk: %s | Concurrency: %d | Received: %s | Time: %s | Avg: %s/s | Speed: %s/s", - humanize.Bytes(p.TotalSize), - humanize.Bytes(d.ChunkSize), - d.Concurrency, + writer.Clear() + + fmt.Fprintf( + writer, + "Downloading: (%s/%s) | Time: %s | Avg: %s/s | Speed: %s/s | Chunk: %s | Concurrency: %d\n", humanize.Bytes(p.Size), + humanize.Bytes(p.TotalSize), p.TotalCost().Round(time.Second), humanize.Bytes(p.AvgSpeed()), humanize.Bytes(p.Speed()), + humanize.Bytes(d.ChunkSize), + d.Concurrency, ) + + writer.Print() } if err := d.Start(); err != nil { log.Fatal(err) } - fmt.Println(" | Done!") + writer.Reset() + fmt.Println("Done!") } diff --git a/go.mod b/go.mod index 3efb4d8..0570c4a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module github.com/melbahja/got go 1.14 require ( + github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5 github.com/dustin/go-humanize v1.0.0 + github.com/mattn/go-isatty v0.0.12 // indirect golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 ) diff --git a/go.sum b/go.sum index be423ba..6d98f68 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,10 @@ +github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5 h1:VYqcjykqpcq262cDxBAkAelSdg6HETkxgwzQRTS40Aw= +github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5/go.mod h1:E7x8aDc3AQzDKjEoIZCt+XYheHk2OkP+p2UgeNjecH8= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/got.go b/got.go index e8abfcb..7ce6a81 100644 --- a/got.go +++ b/got.go @@ -287,6 +287,7 @@ func (d *Download) GetInfo() (Info, error) { // Merge downloaded chunks. func (d *Download) merge(ctx context.Context) error { + file, err := os.Create(d.Dest) if err != nil { return err