]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Write piece status as counts of contiguous identical states
authorMatt Joiner <anacrolix@gmail.com>
Sun, 7 Dec 2014 03:16:02 +0000 (21:16 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 7 Dec 2014 03:16:02 +0000 (21:16 -0600)
torrent.go

index 91e550d9e8b85b9e20364a759b954953b07b72be..884d23dae04203fe3a08f57f2e5748d5e71ff9ac 100644 (file)
@@ -336,14 +336,33 @@ func (t *torrent) WriteStatus(w io.Writer) {
                        return "?"
                }
        }())
-       fmt.Fprint(w, "Pieces:")
-       for index := range t.Pieces {
-               if index%100 == 0 {
-                       fmt.Fprintln(w)
+       if t.haveInfo() {
+               fmt.Fprint(w, "Pieces: ")
+               var (
+                       char  byte
+                       count int
+               )
+               writeSequence := func() {
+                       fmt.Fprintf(w, "%d%c ", count, char)
+               }
+               if len(t.Pieces) != 0 {
+                       char = t.pieceStatusChar(0)
+               }
+               for index := range t.Pieces {
+                       char1 := t.pieceStatusChar(index)
+                       if char1 == char {
+                               count++
+                       } else {
+                               writeSequence()
+                               char = char1
+                               count = 1
+                       }
+               }
+               if count != 0 {
+                       writeSequence()
                }
-               fmt.Fprintf(w, "%c", t.pieceStatusChar(index))
+               fmt.Fprintln(w)
        }
-       fmt.Fprintln(w)
        fmt.Fprintf(w, "Trackers: ")
        for _, tier := range t.Trackers {
                for _, tr := range tier {