progress: render a single download with the same table as several
block() no longer special-cases one download with a [bracketed] one-liner; it always draws the summary line, the column header, and one row per download. The non-TTY path emits the same summary for one or many. This drops lineOne and the dual rendering path it created, so a column or state change is made in one place, not two that could drift.
This commit is contained in:
@@ -114,13 +114,10 @@ func (r *Reporter) render(final bool) {
|
||||
r.redraw(r.block(stats), final)
|
||||
return
|
||||
}
|
||||
// Not a TTY: no cursor control, so emit a single line sparingly (and always
|
||||
// Not a TTY: no cursor control, so emit the summary line sparingly (and always
|
||||
// the final one) to keep redirected output and logs readable.
|
||||
var line string
|
||||
switch {
|
||||
case len(stats) == 1:
|
||||
line = r.lineOne(stats[0])
|
||||
case len(stats) > 1:
|
||||
if len(stats) > 0 {
|
||||
line = r.summary(stats)
|
||||
}
|
||||
if line != "" && (final || now.Sub(r.lastLog) >= logEvery) {
|
||||
@@ -129,52 +126,18 @@ func (r *Reporter) render(final bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reporter) lineOne(s download.Stat) string {
|
||||
dl, ul := r.rates(s)
|
||||
seeding := s.Status == download.Seeding
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "[%s ", elide(s.Name, nameW))
|
||||
// While seeding, report the share ratio in place of the size block and drop
|
||||
// the DL: field; otherwise show completed/total (or bare completed).
|
||||
if seeding {
|
||||
fmt.Fprintf(&b, "SEED(%.1f)", ratio(s.Uploaded, s.Completed))
|
||||
} else if s.Total > 0 {
|
||||
fmt.Fprintf(&b, "%s/%s(%d%%)", humanSize(s.Completed, r.human), humanSize(s.Total, r.human), percent(s.Completed, s.Total))
|
||||
} else {
|
||||
b.WriteString(humanSize(s.Completed, r.human))
|
||||
}
|
||||
// CN is always shown (including HTTP); SD is shown for any torrent.
|
||||
fmt.Fprintf(&b, " CN:%d", s.Conns)
|
||||
if s.IsBT {
|
||||
fmt.Fprintf(&b, " SD:%d", s.Seeders)
|
||||
}
|
||||
if !seeding {
|
||||
fmt.Fprintf(&b, " DL:%s", humanSize(dl, r.human))
|
||||
}
|
||||
if seeding || ul > 0 {
|
||||
fmt.Fprintf(&b, " UL:%s", humanSize(ul, r.human))
|
||||
}
|
||||
if !seeding && s.Total > 0 && dl > 0 {
|
||||
fmt.Fprintf(&b, " ETA:%s", secfmt(etaDuration(s.Total-s.Completed, dl)))
|
||||
}
|
||||
b.WriteByte(']')
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// nameW is the fixed rune width of the name column in the table, matching
|
||||
// elide's cap so a padded name lines the columns up.
|
||||
const nameW = 20
|
||||
|
||||
// block builds the lines of the live display: the single-download dashboard for
|
||||
// one download, a summary + column-header + one row each for several, or a
|
||||
// one-line summary when the table would not fit the window.
|
||||
// block builds the lines of the live display: a summary line, the column header,
|
||||
// and one row per download — or just the one-line summary when the table would
|
||||
// not fit the window. A single download renders the same way as several.
|
||||
func (r *Reporter) block(stats []download.Stat) []string {
|
||||
switch {
|
||||
case len(stats) == 0:
|
||||
if len(stats) == 0 {
|
||||
return nil
|
||||
case len(stats) == 1:
|
||||
return []string{r.lineOne(stats[0])}
|
||||
case len(stats)+3 > r.height:
|
||||
}
|
||||
if len(stats)+3 > r.height {
|
||||
// The block is repainted in place by moving the cursor up over it, which
|
||||
// breaks if printing the last row reaches the screen's bottom row and
|
||||
// scrolls the frame out from under the cursor. term height counts the row
|
||||
@@ -182,14 +145,13 @@ func (r *Reporter) block(stats []download.Stat) []string {
|
||||
// (len+2 lines) must still leave the bottom row free: fall back to the
|
||||
// single summary line once len+2 would fill to the bottom.
|
||||
return []string{r.summary(stats)}
|
||||
default:
|
||||
out := make([]string, 0, len(stats)+2)
|
||||
out = append(out, r.summary(stats), tableHeader())
|
||||
for _, s := range stats {
|
||||
out = append(out, r.tableRow(s))
|
||||
}
|
||||
return out
|
||||
}
|
||||
out := make([]string, 0, len(stats)+2)
|
||||
out = append(out, r.summary(stats), tableHeader())
|
||||
for _, s := range stats {
|
||||
out = append(out, r.tableRow(s))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// redraw repaints the block in place: move to the top of the previous frame,
|
||||
|
||||
Reference in New Issue
Block a user