From 47c4562e1bef396a75b079d3cc0dd2f257162a57 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 27 Nov 2022 23:21:26 +0300 Subject: [PATCH] Shorter done percentage --- cmd/btrtrc/list.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/btrtrc/list.go b/cmd/btrtrc/list.go index f97f2361..dcbb8b99 100644 --- a/cmd/btrtrc/list.go +++ b/cmd/btrtrc/list.go @@ -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())), -- 2.44.0