From c1744e37bec48844e8d07160ac1d3a12f1fb2118 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 20 Sep 2021 22:01:18 +1000 Subject: [PATCH] cmd/torrent: Refactor and set progress interval to 3s --- cmd/torrent/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 385d732c..f27a8538 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -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 -- 2.48.1