Skip to content

Commit

Permalink
chore: optimize dfget progressbar (#3021)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <majinjing3@gmail.com>
  • Loading branch information
jim3ma authored Jan 15, 2024
1 parent e03f8d8 commit abcf7ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
61 changes: 25 additions & 36 deletions client/dfget/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,38 @@ func singleDownload(ctx context.Context, client dfdaemonclient.V1, cfg *config.D
downError error
)

if stream, downError = client.Download(ctx, request); downError == nil {
if cfg.ShowProgress {
pb = newProgressBar(-1)
if stream, downError = client.Download(ctx, request); downError != nil {
goto processError
}

if cfg.ShowProgress {
pb = newProgressBar(-1)
}

for {
if result, downError = stream.Recv(); downError != nil {
break
}

for {
if result, downError = stream.Recv(); downError != nil {
break
}
if result.CompletedLength > 0 && pb != nil {
_ = pb.Set64(int64(result.CompletedLength))
}

if result.CompletedLength > 0 && pb != nil {
_ = pb.Set64(int64(result.CompletedLength))
// success
if result.Done {
if pb != nil {
pb.Describe("Downloaded")
_ = pb.Close()
}

// success
if result.Done {
if pb != nil {
pb.Describe("Downloaded")
_ = pb.Close()
}

wLog.Infof("download from daemon success, length: %d bytes cost: %d ms", result.CompletedLength, time.Since(start).Milliseconds())
fmt.Printf("finish total length %d bytes\n", result.CompletedLength)
wLog.Infof("download from daemon success, length: %d bytes, cost: %d ms", result.CompletedLength, time.Since(start).Milliseconds())
fmt.Printf("finish total length %d bytes\n", result.CompletedLength)

break
}
break
}
}

processError:
if downError != nil && !cfg.KeepOriginalOffset {
wLog.Warnf("daemon downloads file error: %v", downError)
fmt.Printf("daemon downloads file error: %v\n", downError)
Expand Down Expand Up @@ -215,7 +218,7 @@ func downloadFromSource(ctx context.Context, cfg *config.DfgetConfig, hdr map[st
}
renameOK = true

wLog.Infof("download from source success, length: %d bytes cost: %d ms", written, time.Since(start).Milliseconds())
wLog.Infof("download from source success, length: %d bytes, cost: %d ms", written, time.Since(start).Milliseconds())
fmt.Printf("finish total length %d bytes\n", written)

return nil
Expand Down Expand Up @@ -281,21 +284,7 @@ func newDownRequest(cfg *config.DfgetConfig, hdr map[string]string) *dfdaemonv1.
}

func newProgressBar(max int64) *progressbar.ProgressBar {
return progressbar.NewOptions64(max,
progressbar.OptionShowBytes(true),
progressbar.OptionSetPredictTime(true),
progressbar.OptionUseANSICodes(true),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionFullWidth(),
progressbar.OptionSetDescription("[cyan]Downloading...[reset]"),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
return progressbar.DefaultBytes(-1, "Downloading")
}

func accept(u string, accept, reject string) bool {
Expand Down
4 changes: 2 additions & 2 deletions cmd/dfget/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ var rootCmd = &cobra.Command{
// do get file
err = runDfget(cmd, d.DfgetLockPath(), d.DaemonSockPath())
if err != nil {
msg := fmt.Sprintf("download success: %t cost: %d ms error: %s", false, time.Since(start).Milliseconds(), err.Error())
msg := fmt.Sprintf("download success: %t, cost: %d ms error: %s", false, time.Since(start).Milliseconds(), err.Error())
logger.With("url", dfgetConfig.URL).Info(msg)
fmt.Println(msg)
return fmt.Errorf("download url %s: %w", dfgetConfig.URL, err)
}

msg := fmt.Sprintf("download success: %t cost: %d ms", true, time.Since(start).Milliseconds())
msg := fmt.Sprintf("download success: %t, cost: %d ms", true, time.Since(start).Milliseconds())
logger.With("url", dfgetConfig.URL).Info(msg)
fmt.Println(msg)
return nil
Expand Down

0 comments on commit abcf7ea

Please sign in to comment.