Skip to content

Commit 9f298a0

Browse files
Tanq16Tanq16
andauthored
add speed to progress bar (#25)
Co-authored-by: Tanq16 <[email protected]>
1 parent 4b92695 commit 9f298a0

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

internal/downloader.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
119119

120120
if err == utils.ErrRangeRequestsNotSupported {
121121
outputMgr.SetStatus(entryFunctionId, "warning")
122-
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("Downloading %s with 1 connection (range requests unsupported)", entry.OutputPath))
122+
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("[range unsupported] Downloading %s with 1-thread (%s)", entry.OutputPath, utils.FormatBytes(uint64(fileSize))))
123123
} else if err != nil {
124124
outputMgr.ReportError(entryFunctionId, fmt.Errorf("error getting file size for %s: %v", entry.OutputPath, err))
125125
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("Error getting file size for %s", entry.OutputPath))
@@ -136,8 +136,8 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
136136
var totalDownloaded int64
137137
for bytesDownloaded := range progCh {
138138
totalDownloaded += bytesDownloaded
139-
progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(totalFileSize)))
140-
outputMgr.AddProgressBarToStream(funcId, totalDownloaded, totalFileSize, progressString)
139+
// progressString := , utils.FormatBytes(uint64(totalFileSize)))
140+
outputMgr.AddProgressBarToStream(funcId, totalDownloaded, totalFileSize, utils.FormatBytes(uint64(totalDownloaded)))
141141
}
142142
}(entryFunctionId, fileSize, progressCh)
143143

@@ -268,8 +268,8 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
268268
var totalDownloaded int64
269269
for bytesDownloaded := range progCh {
270270
totalDownloaded += bytesDownloaded
271-
progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(totalFileSize)))
272-
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, totalFileSize, progressString)
271+
// progressString := fmt.Sprintf("%s / %s", , utils.FormatBytes(uint64(totalFileSize)))
272+
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, totalFileSize, utils.FormatBytes(uint64(totalDownloaded)))
273273
}
274274
}(entry.OutputPath, size, progressCh)
275275

@@ -393,6 +393,7 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
393393
totoalJobSize += s3Job.Size
394394
}
395395
close(s3JobsCh)
396+
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("Downloading S3 key %s (%s)", entry.URL, utils.FormatBytes(uint64(totoalJobSize))))
396397

397398
// Internal goroutine to forward progress updates to the manager
398399
progressWg.Add(1)
@@ -401,8 +402,8 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
401402
var totalDownloaded int64
402403
for bytesDownloaded := range progCh {
403404
totalDownloaded += bytesDownloaded
404-
progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(totalFileSize)))
405-
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, totalFileSize, progressString)
405+
// progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(totalFileSize)))
406+
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, totalFileSize, utils.FormatBytes(uint64(totalDownloaded)))
406407
}
407408
}(entry.OutputPath, totoalJobSize, progressCh)
408409

@@ -488,9 +489,9 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
488489
}
489490
entry.OutputPath = config.OutputPath
490491
}
491-
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("Downloading GDrive file %s", entry.OutputPath))
492492
fileSize := metadata["size"].(string)
493493
fileSizeInt, _ := strconv.ParseInt(fileSize, 10, 64)
494+
outputMgr.SetMessage(entryFunctionId, fmt.Sprintf("Downloading GDrive file %s (%s)", entry.OutputPath, utils.FormatBytes(uint64(fileSizeInt))))
494495

495496
var progressWg sync.WaitGroup
496497
progressWg.Add(1)
@@ -499,8 +500,8 @@ func BatchDownload(entries []utils.DownloadEntry, numLinks, connectionsPerLink i
499500
var totalDownloaded int64
500501
for bytesDownloaded := range progCh {
501502
totalDownloaded += bytesDownloaded
502-
progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(filesize)))
503-
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, filesize, progressString)
503+
// progressString := fmt.Sprintf("%s / %s", utils.FormatBytes(uint64(totalDownloaded)), utils.FormatBytes(uint64(filesize)))
504+
outputMgr.AddProgressBarToStream(entryFunctionId, totalDownloaded, filesize, utils.FormatBytes(uint64(totalDownloaded)))
504505
}
505506
}(entry.OutputPath, fileSizeInt, progressCh)
506507

utils/functions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ func FormatBytes(bytes uint64) string {
217217
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
218218
}
219219

220+
func FormatSpeed(bytes int64, elapsed float64) string {
221+
if elapsed == 0 {
222+
return "0 B/s"
223+
}
224+
bps := float64(bytes) / elapsed
225+
formatted := FormatBytes(uint64(bps))
226+
return formatted[:len(formatted)-1] + "B/s" // Slice off "B" and add "B/s"
227+
}
228+
220229
func Clean(outputPath string) error {
221230
tempDir := filepath.Join(filepath.Dir(outputPath), ".danzo-temp")
222231
if err := os.RemoveAll(tempDir); err != nil {

utils/output-manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ func (m *Manager) AddProgressBarToStream(name string, outof, final int64, text s
413413
defer m.mutex.Unlock()
414414
if info, exists := m.outputs[name]; exists {
415415
progressBar := PrintProgressBar(max(0, outof), final, 30)
416-
display := progressBar + debugStyle.Render(text)
416+
elapsed := time.Since(info.StartTime).Round(time.Second).Seconds()
417+
display := fmt.Sprintf("%s%s %s %s", progressBar, debugStyle.Render(text), StyleSymbols["bullet"], debugStyle.Render(FormatSpeed(outof, elapsed)))
417418
info.StreamLines = []string{display} // Set as only stream so nothing else is displayed
418419
info.LastUpdated = time.Now()
419420
}

0 commit comments

Comments
 (0)