From 9fb9112d8fd952b9ad6e1324210917a709064340 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 27 Feb 2020 16:50:14 +1100 Subject: [PATCH] cmd/torrent: Ditch the progress bar I'm not happy with any implementations. The one in use was racy anyway. Helps progress toward fixing #382. --- cmd/torrent/main.go | 65 +++++++++++++++++++-------------------------- go.mod | 1 - 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 1e7feba0..28da282d 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -13,14 +13,13 @@ import ( "time" "github.com/anacrolix/missinggo" + "github.com/dustin/go-humanize" "golang.org/x/xerrors" "github.com/anacrolix/log" "github.com/anacrolix/envpprof" "github.com/anacrolix/tagflag" - humanize "github.com/dustin/go-humanize" - "github.com/gosuri/uiprogress" "golang.org/x/time/rate" "github.com/anacrolix/torrent" @@ -29,39 +28,33 @@ import ( "github.com/anacrolix/torrent/storage" ) -var progress = uiprogress.New() - func torrentBar(t *torrent.Torrent) { - bar := progress.AddBar(1) - bar.AppendCompleted() - bar.AppendFunc(func(*uiprogress.Bar) (ret string) { - select { - case <-t.GotInfo(): - default: - return "getting info" - } - if t.Seeding() { - return "seeding" - } else if t.BytesCompleted() == t.Info().TotalLength() { - return "completed" - } else { - return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength()))) - } - }) - bar.PrependFunc(func(*uiprogress.Bar) string { - return t.Name() - }) go func() { - <-t.GotInfo() - tl := int(t.Info().TotalLength()) - if tl == 0 { - bar.Set(1) - return + if t.Info() == nil { + fmt.Printf("getting info for %q\n", t.Name()) + <-t.GotInfo() } - bar.Total = tl for { - bc := t.BytesCompleted() - bar.Set(int(bc)) + var completedPieces, partialPieces int + psrs := t.PieceStateRuns() + for _, r := range psrs { + if r.Complete { + completedPieces += r.Length + } + if r.Partial { + partialPieces += r.Length + } + } + fmt.Printf( + "downloading %q: %s/%s, %d/%d pieces completed (%d partial)\n", + t.Name(), + humanize.Bytes(uint64(t.BytesCompleted())), + humanize.Bytes(uint64(t.Length())), + completedPieces, + t.NumPieces(), + partialPieces, + ) + //fmt.Println(psrs) time.Sleep(time.Second) } }() @@ -110,7 +103,9 @@ func addTorrents(client *torrent.Client) error { if err != nil { return xerrors.Errorf("adding torrent for %q: %w", arg, err) } - torrentBar(t) + if flags.Progress { + torrentBar(t) + } t.AddPeers(func() (ret []torrent.Peer) { for _, ta := range flags.TestPeer { ret = append(ret, torrent.Peer{ @@ -182,9 +177,6 @@ func main() { func mainErr() error { tagflag.Parse(&flags) defer envpprof.Stop() - if stdoutAndStderrAreSameFile() { - log.Default = log.Logger{log.StreamLogger{W: progress.Bypass(), Fmt: log.LineFormatter}} - } clientConfig := torrent.NewDefaultClientConfig() clientConfig.DisableAcceptRateLimiting = true clientConfig.NoDHT = !flags.Dht @@ -237,9 +229,6 @@ func mainErr() error { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { client.WriteStatus(w) }) - if flags.Progress { - progress.Start() - } addTorrents(client) if client.WaitAll() { log.Print("downloaded ALL the torrents") diff --git a/go.mod b/go.mod index d3befb50..b7a9a2d7 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/etcd-io/bbolt v1.3.3 github.com/fsnotify/fsnotify v1.4.7 github.com/google/btree v1.0.0 - github.com/gosuri/uiprogress v0.0.1 github.com/jessevdk/go-flags v1.4.0 github.com/mattn/go-sqlite3 v2.0.2+incompatible github.com/pkg/errors v0.9.1 -- 2.48.1