From: Matt Joiner Date: Mon, 20 Sep 2021 12:01:18 +0000 (+1000) Subject: cmd/torrent: Refactor and set progress interval to 3s X-Git-Tag: v1.32.0~24 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c1744e37bec48844e8d07160ac1d3a12f1fb2118;p=btrtrc.git cmd/torrent: Refactor and set progress interval to 3s --- 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