Unify progress report frequency calculation

This commit is contained in:
Michael Eischer 2020-12-29 14:56:14 +01:00
parent 969141b5e9
commit 85fe5feadb
2 changed files with 14 additions and 16 deletions

View File

@ -11,7 +11,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -545,15 +544,7 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina
}() }()
gopts.stdout, gopts.stderr = p.Stdout(), p.Stderr() gopts.stdout, gopts.stderr = p.Stdout(), p.Stderr()
if s, ok := os.LookupEnv("RESTIC_PROGRESS_FPS"); ok { p.SetMinUpdatePause(calculateProgressInterval())
fps, err := strconv.Atoi(s)
if err == nil && fps >= 1 {
if fps > 60 {
fps = 60
}
p.SetMinUpdatePause(time.Second / time.Duration(fps))
}
}
t.Go(func() error { return p.Run(t.Context(gopts.ctx)) }) t.Go(func() error { return p.Run(t.Context(gopts.ctx)) })

View File

@ -9,12 +9,10 @@ import (
"github.com/restic/restic/internal/ui/progress" "github.com/restic/restic/internal/ui/progress"
) )
// newProgressMax returns a progress.Counter that prints to stdout. // calculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS
func newProgressMax(show bool, max uint64, description string) *progress.Counter { // or if unset returns an interval for 60fps on interactive terminals and 0 (=disabled)
if !show { // for non-interactive terminals
return nil func calculateProgressInterval() time.Duration {
}
interval := time.Second / 60 interval := time.Second / 60
fps, err := strconv.ParseInt(os.Getenv("RESTIC_PROGRESS_FPS"), 10, 64) fps, err := strconv.ParseInt(os.Getenv("RESTIC_PROGRESS_FPS"), 10, 64)
if err == nil && fps >= 1 { if err == nil && fps >= 1 {
@ -25,6 +23,15 @@ func newProgressMax(show bool, max uint64, description string) *progress.Counter
} else if !stdoutIsTerminal() { } else if !stdoutIsTerminal() {
interval = 0 interval = 0
} }
return interval
}
// newProgressMax returns a progress.Counter that prints to stdout.
func newProgressMax(show bool, max uint64, description string) *progress.Counter {
if !show {
return nil
}
interval := calculateProgressInterval()
return progress.New(interval, func(v uint64, d time.Duration, final bool) { return progress.New(interval, func(v uint64, d time.Duration, final bool) {
status := fmt.Sprintf("[%s] %s %d / %d %s", status := fmt.Sprintf("[%s] %s %d / %d %s",