Skip to content

Commit

Permalink
cli: feat: add progress bars #25
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 15, 2024
1 parent c22bca1 commit 1d9e5b0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ Save Location`+"\t"+`: %s/
d.DownloadDirectory,
)
fmt.Println(txt)
RegisterHandlers(client)
RegisterHandlers(client, int64(d.ContentLength))
return client.Listen()
}
65 changes: 44 additions & 21 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
package cmd

import (
"fmt"
"os"
"time"

"github.com/vbauerster/mpb/v8"
cmdCommon "github.com/warpdl/warpdl/cmd/common"
"github.com/warpdl/warpdl/common"
"github.com/warpdl/warpdl/pkg/warpcli"
"github.com/warpdl/warpdl/pkg/warplib"
)

func downloadStopped(client *warpcli.Client) func(dr *common.DownloadingResponse) error {
func downloadStopped(client *warpcli.Client, sc *SpeedCounter) func(dr *common.DownloadingResponse) error {
return func(dr *common.DownloadingResponse) error {
if dr.Hash != warplib.MAIN_HASH {
return nil
}
fmt.Println("Download Stopped: ", dr.DownloadId)
sc.bar.Abort(false)
// fmt.Println("Download Stopped: ", dr.DownloadId)
client.Disconnect()
os.Exit(0)
return nil
}
}

func downloadProgress(dr *common.DownloadingResponse) error {
fmt.Println(dr.Action, dr.DownloadId, dr.Hash, dr.Value)
return nil
func downloadProgress(sc *SpeedCounter) func(dr *common.DownloadingResponse) error {
return func(dr *common.DownloadingResponse) error {
// fmt.Println(dr.Action, dr.DownloadId, dr.Hash, dr.Value)
sc.IncrBy(int(dr.Value))
return nil
}
}

func downloadComplete(client *warpcli.Client) func(dr *common.DownloadingResponse) error {
func downloadComplete(client *warpcli.Client, dbar, cbar *mpb.Bar, sc *SpeedCounter) func(dr *common.DownloadingResponse) error {
return func(dr *common.DownloadingResponse) error {
fmt.Println("Download Complete: ", dr.Hash)
if dr.Hash == warplib.MAIN_HASH {
client.Disconnect()
// fmt.Println("Download Complete: ", dr.Hash)
if dr.Hash != warplib.MAIN_HASH {
return nil
}
defer client.Disconnect()
sc.Stop()
// fill download bar
if dbar.Completed() {
return nil
}
dbar.SetCurrent(dr.Value)
// fill compile bar
if cbar.Completed() {
return nil
}
os.Exit(0)
cbar.SetCurrent(dr.Value)
return nil
}
}
Expand All @@ -42,34 +57,42 @@ func compileStart(dr *common.DownloadingResponse) error {
}

func compileComplete(dr *common.DownloadingResponse) error {
fmt.Println("Compile Complete: ", dr.Hash)
// fmt.Println("Compile Complete: ", dr.Hash)
return nil
}

func compileProgress(dr *common.DownloadingResponse) error {
return nil
func compileProgress(bar *mpb.Bar) func(dr *common.DownloadingResponse) error {
return func(dr *common.DownloadingResponse) error {
bar.IncrBy(int(dr.Value))
return nil
}
}

func RegisterHandlers(client *warpcli.Client) {
func RegisterHandlers(client *warpcli.Client, contentLength int64) {
sc := NewSpeedCounter(4350 * time.Microsecond)
p := mpb.New(mpb.WithWidth(64), mpb.WithRefreshRate(time.Millisecond*100))
dbar, cbar := cmdCommon.InitBars(p, "", contentLength)
sc.SetBar(dbar)
sc.Start()
client.AddHandler(
common.UPDATE_DOWNLOADING,
warpcli.NewDownloadingHandler(common.DownloadStopped, downloadStopped(client)),
warpcli.NewDownloadingHandler(common.DownloadStopped, downloadStopped(client, sc)),
)
client.AddHandler(
common.UPDATE_DOWNLOADING,
warpcli.NewDownloadingHandler(common.DownloadProgress, downloadProgress),
warpcli.NewDownloadingHandler(common.DownloadProgress, downloadProgress(sc)),
)
client.AddHandler(
common.UPDATE_DOWNLOADING,
warpcli.NewDownloadingHandler(common.DownloadComplete, downloadComplete(client)),
warpcli.NewDownloadingHandler(common.DownloadComplete, downloadComplete(client, dbar, cbar, sc)),
)
client.AddHandler(
common.UPDATE_DOWNLOADING,
warpcli.NewDownloadingHandler(common.CompileComplete, compileComplete),
)
client.AddHandler(
common.UPDATE_DOWNLOADING,
warpcli.NewDownloadingHandler(common.CompileProgress, compileProgress),
warpcli.NewDownloadingHandler(common.CompileProgress, compileProgress(cbar)),
)
client.AddHandler(
common.UPDATE_DOWNLOADING,
Expand Down
2 changes: 1 addition & 1 deletion cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ Max Connections`+"\t"+`: %d
txt += fmt.Sprintf("Max Segments\t: %d\n", d.MaxSegments)
}
fmt.Println(txt)
RegisterHandlers(client)
RegisterHandlers(client, int64(d.ContentLength))
return client.Listen()
}
2 changes: 1 addition & 1 deletion cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ Max Connections`+"\t"+`: %d
txt += fmt.Sprintf("Max Segments\t: %d\n", r.MaxSegments)
}
fmt.Println(txt)
RegisterHandlers(client)
RegisterHandlers(client, int64(r.ContentLength))
return client.Listen()
}
2 changes: 0 additions & 2 deletions pkg/warpcli/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func (d *Dispatcher) process(buf []byte) error {
if !res.Ok {
return errors.New(res.Error)
}
fmt.Println(res.Update.Type)
fmt.Println(string(res.Update.Message))
d.mu.RLock()
handlers, ok := d.Handlers[res.Update.Type]
d.mu.RUnlock()
Expand Down

0 comments on commit 1d9e5b0

Please sign in to comment.