]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Shorter done percentage
authorSergey Matveev <stargrave@stargrave.org>
Sun, 27 Nov 2022 20:21:26 +0000 (23:21 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 13 Jan 2023 08:32:42 +0000 (11:32 +0300)
cmd/btrtrc/list.go

index f97f2361e5faac0e3e5991175ab206d7a598412f..dcbb8b99415610c99a85ef295a75c6872b526c4f 100644 (file)
@@ -7,6 +7,7 @@ import (
        "os"
        "path"
        "sort"
+       "strconv"
        "strings"
        "sync"
        "syscall"
@@ -103,6 +104,14 @@ func fifoList(c *torrent.Client) {
        }
 }
 
+func mustParseInt(s string) int {
+       i, err := strconv.Atoi(s)
+       if err != nil {
+               log.Fatalln(err)
+       }
+       return i
+}
+
 func fifoPeerList(t *torrent.Torrent) {
        pth := path.Join(FIFOsDir, PeersDir, t.InfoHash().HexString())
        recreateFIFO(pth)
@@ -119,18 +128,18 @@ func fifoPeerList(t *torrent.Torrent) {
                pcs := t.PeerConns()
                sort.Sort(ByPeerID(pcs))
                for _, pc := range pcs {
-                       completed := pc.CompletedString()
-                       completedColour := Red
-                       cols := strings.Split(completed, "/")
-                       if cols[0] == cols[1] {
-                               completedColour = Green
+                       cols := strings.Split(pc.CompletedString(), "/")
+                       done := (mustParseInt(cols[0]) * 100) / mustParseInt(cols[1])
+                       doneColour := Red
+                       if done == 100 {
+                               doneColour = Green
                        }
                        stats := pc.Peer.Stats()
                        fmt.Fprintf(fd,
-                               "%s%s%s %10s %s%11s%s %s%d%s/%s%d%s %s / %s | %s%s%s %q\n",
+                               "%s%s%s %10s %s%3d%%%s %s%d%s/%s%d%s %s / %s | %s%s%s %q\n",
                                Blue, hex.EncodeToString(pc.PeerID[:]), Reset,
                                pc.StatusFlags(),
-                               completedColour, pc.CompletedString(), Reset,
+                               doneColour, done, Reset,
                                Green, int(pc.DownloadRate()/1024), Reset,
                                Magenta, int(pc.UploadRate()/1024), Reset,
                                humanize.IBytes(uint64(stats.BytesReadData.Int64())),