Skip to content

Commit

Permalink
improve cli stdout progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Aug 12, 2020
1 parent 87084f6 commit 962cace
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
23 changes: 17 additions & 6 deletions cmd/got/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"flag"
"fmt"
"log"
"os"
"time"

"github.com/apoorvam/goterminal"
"github.com/dustin/go-humanize"
"github.com/melbahja/got"
)
Expand Down Expand Up @@ -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!")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
1 change: 1 addition & 0 deletions got.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 962cace

Please sign in to comment.