Skip to content

Commit fd202da

Browse files
authored
Merge #14
Improve CLI Stdout Progress.
2 parents 87084f6 + 962cace commit fd202da

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

cmd/got/main.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"flag"
55
"fmt"
66
"log"
7+
"os"
78
"time"
89

10+
"github.com/apoorvam/goterminal"
911
"github.com/dustin/go-humanize"
1012
"github.com/melbahja/got"
1113
)
@@ -58,24 +60,33 @@ func main() {
5860
log.Fatal(err)
5961
}
6062

63+
// Goterm writer.
64+
writer := goterminal.New(os.Stdout)
65+
6166
// Set progress func to update cli output.
6267
d.Progress.ProgressFunc = func(p *got.Progress, d *got.Download) {
6368

64-
fmt.Printf(
65-
"\r\r\bTotal: %s | Chunk: %s | Concurrency: %d | Received: %s | Time: %s | Avg: %s/s | Speed: %s/s",
66-
humanize.Bytes(p.TotalSize),
67-
humanize.Bytes(d.ChunkSize),
68-
d.Concurrency,
69+
writer.Clear()
70+
71+
fmt.Fprintf(
72+
writer,
73+
"Downloading: (%s/%s) | Time: %s | Avg: %s/s | Speed: %s/s | Chunk: %s | Concurrency: %d\n",
6974
humanize.Bytes(p.Size),
75+
humanize.Bytes(p.TotalSize),
7076
p.TotalCost().Round(time.Second),
7177
humanize.Bytes(p.AvgSpeed()),
7278
humanize.Bytes(p.Speed()),
79+
humanize.Bytes(d.ChunkSize),
80+
d.Concurrency,
7381
)
82+
83+
writer.Print()
7484
}
7585

7686
if err := d.Start(); err != nil {
7787
log.Fatal(err)
7888
}
7989

80-
fmt.Println(" | Done!")
90+
writer.Reset()
91+
fmt.Println("Done!")
8192
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/melbahja/got
33
go 1.14
44

55
require (
6+
github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5
67
github.com/dustin/go-humanize v1.0.0
8+
github.com/mattn/go-isatty v0.0.12 // indirect
79
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
810
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5 h1:VYqcjykqpcq262cDxBAkAelSdg6HETkxgwzQRTS40Aw=
2+
github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5/go.mod h1:E7x8aDc3AQzDKjEoIZCt+XYheHk2OkP+p2UgeNjecH8=
13
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
24
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
5+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
6+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
37
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
48
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
10+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

got.go

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func (d *Download) GetInfo() (Info, error) {
287287

288288
// Merge downloaded chunks.
289289
func (d *Download) merge(ctx context.Context) error {
290+
290291
file, err := os.Create(d.Dest)
291292
if err != nil {
292293
return err

0 commit comments

Comments
 (0)