t.go | 17 +++++++++++++---- torrent.go | 10 +++------- diff --git a/t.go b/t.go index fcaec919cd0b462475eacaf51e6cecef00366614..ccd300967b0264d449545a67777ec512d45711e8 100644 --- a/t.go +++ b/t.go @@ -41,10 +41,19 @@ t.addReader(&r) return &r } -// Returns the state of pieces of the torrent. They are grouped into runs of -// same state. The sum of the state run lengths is the number of pieces -// in the torrent. -func (t *Torrent) PieceStateRuns() []PieceStateRun { +type PieceStateRuns []PieceStateRun + +func (me PieceStateRuns) String() string { + ss := make([]string, 0, len(me)) + for _, psr := range me { + ss = append(ss, psr.String()) + } + return strings.Join(ss, " ") +} + +// Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of +// the state run-lengths is the number of pieces in the torrent. +func (t *Torrent) PieceStateRuns() PieceStateRuns { t.cl.rLock() defer t.cl.rUnlock() return t.pieceStateRuns() diff --git a/torrent.go b/torrent.go index cd136523f0d50c72a6163d2d93626569c0250f66..f6627b2fb8f6d285beefcd00e405d278829bd753 100644 --- a/torrent.go +++ b/torrent.go @@ -503,7 +503,7 @@ ExtendedPayload: append(p, data...), } } -func (t *Torrent) pieceStateRuns() (ret []PieceStateRun) { +func (t *Torrent) pieceStateRuns() (ret PieceStateRuns) { rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) { ret = append(ret, PieceStateRun{ PieceState: el.(PieceState), @@ -518,7 +518,7 @@ return } // Produces a small string representing a PieceStateRun. -func pieceStateRunStatusChars(psr PieceStateRun) (ret string) { +func (psr PieceStateRun) String() (ret string) { ret = fmt.Sprintf("%d", psr.Length) ret += func() string { switch psr.Priority { @@ -576,11 +576,7 @@ } }()) if t.info != nil { fmt.Fprintf(w, "Num Pieces: %d (%d completed)\n", t.numPieces(), t.numPiecesCompleted()) - fmt.Fprint(w, "Piece States:") - for _, psr := range t.pieceStateRuns() { - w.Write([]byte(" ")) - w.Write([]byte(pieceStateRunStatusChars(psr))) - } + fmt.Fprintf(w, "Piece States: %s", t.pieceStateRuns()) fmt.Fprintln(w) } fmt.Fprintf(w, "Reader Pieces:")