cmd/torrent: Refactor and set progress interval to 3s

This commit is contained in:
Matt Joiner 2021-09-20 22:01:18 +10:00
parent ad70a761ec
commit c1744e37be
1 changed files with 6 additions and 2 deletions

View File

@ -42,7 +42,8 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
}
lastStats := t.Stats()
var lastLine string
for range time.Tick(time.Second) {
interval := 3 * time.Second
for range time.Tick(interval) {
var completedPieces, partialPieces int
psrs := t.PieceStateRuns()
for _, r := range psrs {
@ -54,6 +55,9 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
}
}
stats := t.Stats()
byteRate := int64(time.Second)
byteRate *= stats.BytesReadUsefulData.Int64() - lastStats.BytesReadUsefulData.Int64()
byteRate /= int64(interval)
line := fmt.Sprintf(
"%v: downloading %q: %s/%s, %d/%d pieces completed (%d partial): %v/s\n",
time.Since(start),
@ -63,7 +67,7 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
completedPieces,
t.NumPieces(),
partialPieces,
humanize.Bytes(uint64(stats.BytesReadUsefulData.Int64()-lastStats.BytesReadUsefulData.Int64())),
humanize.Bytes(uint64(byteRate)),
)
if line != lastLine {
lastLine = line