From 85fe5feadbf7b4135bf5880af8e4148c932eb66d Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 29 Dec 2020 14:56:14 +0100 Subject: [PATCH] Unify progress report frequency calculation --- cmd/restic/cmd_backup.go | 11 +---------- cmd/restic/progress.go | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 0f4dabc84..cfec837db 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -11,7 +11,6 @@ import ( "path" "path/filepath" "runtime" - "strconv" "strings" "time" @@ -545,15 +544,7 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina }() gopts.stdout, gopts.stderr = p.Stdout(), p.Stderr() - if s, ok := os.LookupEnv("RESTIC_PROGRESS_FPS"); ok { - fps, err := strconv.Atoi(s) - if err == nil && fps >= 1 { - if fps > 60 { - fps = 60 - } - p.SetMinUpdatePause(time.Second / time.Duration(fps)) - } - } + p.SetMinUpdatePause(calculateProgressInterval()) t.Go(func() error { return p.Run(t.Context(gopts.ctx)) }) diff --git a/cmd/restic/progress.go b/cmd/restic/progress.go index b311af3b9..bb0bcd9fa 100644 --- a/cmd/restic/progress.go +++ b/cmd/restic/progress.go @@ -9,12 +9,10 @@ import ( "github.com/restic/restic/internal/ui/progress" ) -// newProgressMax returns a progress.Counter that prints to stdout. -func newProgressMax(show bool, max uint64, description string) *progress.Counter { - if !show { - return nil - } - +// calculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS +// or if unset returns an interval for 60fps on interactive terminals and 0 (=disabled) +// for non-interactive terminals +func calculateProgressInterval() time.Duration { interval := time.Second / 60 fps, err := strconv.ParseInt(os.Getenv("RESTIC_PROGRESS_FPS"), 10, 64) if err == nil && fps >= 1 { @@ -25,6 +23,15 @@ func newProgressMax(show bool, max uint64, description string) *progress.Counter } else if !stdoutIsTerminal() { 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) { status := fmt.Sprintf("[%s] %s %d / %d %s",