From 32b7168a9ea0c87f88dcb2a909b8a4cc4c695f08 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 13 Sep 2025 23:17:15 +0200 Subject: [PATCH] centralize index progress bar for termstatus --- cmd/restic/cmd_backup.go | 3 ++- cmd/restic/cmd_check.go | 6 +++++- cmd/restic/cmd_prune.go | 2 +- cmd/restic/cmd_recover.go | 2 +- cmd/restic/cmd_repair_packs.go | 2 +- cmd/restic/cmd_restore.go | 5 ++--- cmd/restic/progress.go | 12 ++++++++---- internal/ui/progress/printer.go | 9 +++++++++ 8 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index acb5500fd..a3fd8916e 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -552,7 +552,8 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter progressPrinter.V("load index files") } - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + msg := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) + bar := newIndexTerminalProgress(msg) err = repo.LoadIndex(ctx, bar) if err != nil { return err diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 04e6fab82..fa8e864d9 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -252,7 +252,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args } printer.P("load indexes\n") - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + bar := newIndexTerminalProgress(printer) hints, errs := chkr.LoadIndex(ctx, bar) if ctx.Err() != nil { return summary, ctx.Err() @@ -528,6 +528,10 @@ func (*jsonErrorPrinter) NewCounter(_ string) *progress.Counter { return nil } +func (*jsonErrorPrinter) NewCounterTerminalOnly(_ string) *progress.Counter { + return nil +} + func (p *jsonErrorPrinter) E(msg string, args ...interface{}) { status := checkError{ MessageType: "error", diff --git a/cmd/restic/cmd_prune.go b/cmd/restic/cmd_prune.go index b3aa8431e..08175b92f 100644 --- a/cmd/restic/cmd_prune.go +++ b/cmd/restic/cmd_prune.go @@ -195,7 +195,7 @@ func runPruneWithRepo(ctx context.Context, opts PruneOptions, gopts GlobalOption printer.P("loading indexes...\n") // loading the index before the snapshots is ok, as we use an exclusive lock here - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + bar := newIndexTerminalProgress(printer) err := repo.LoadIndex(ctx, bar) if err != nil { return err diff --git a/cmd/restic/cmd_recover.go b/cmd/restic/cmd_recover.go index cdb5c2a84..7155f8618 100644 --- a/cmd/restic/cmd_recover.go +++ b/cmd/restic/cmd_recover.go @@ -69,7 +69,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term *termstatus.Termi } printer.P("load index files\n") - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + bar := newIndexTerminalProgress(printer) if err = repo.LoadIndex(ctx, bar); err != nil { return err } diff --git a/cmd/restic/cmd_repair_packs.go b/cmd/restic/cmd_repair_packs.go index 72cf52b11..babe6789c 100644 --- a/cmd/restic/cmd_repair_packs.go +++ b/cmd/restic/cmd_repair_packs.go @@ -61,7 +61,7 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term *termstatus.T printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + bar := newIndexTerminalProgress(printer) err = repo.LoadIndex(ctx, bar) if err != nil { return errors.Fatalf("%s", err) diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index f0c84c628..7a65602a6 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -11,7 +11,6 @@ import ( "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restorer" "github.com/restic/restic/internal/terminal" - "github.com/restic/restic/internal/ui" restoreui "github.com/restic/restic/internal/ui/restore" "github.com/restic/restic/internal/ui/termstatus" @@ -147,7 +146,8 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, return errors.Fatalf("failed to find snapshot: %v", err) } - bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term) + msg := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) + bar := newIndexTerminalProgress(msg) err = repo.LoadIndex(ctx, bar) if err != nil { return err @@ -158,7 +158,6 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, return err } - msg := ui.NewMessage(term, gopts.verbosity) var printer restoreui.ProgressPrinter if gopts.JSON { printer = restoreui.NewJSONProgress(term, gopts.verbosity) diff --git a/cmd/restic/progress.go b/cmd/restic/progress.go index 6c01cc0a2..3c56d6844 100644 --- a/cmd/restic/progress.go +++ b/cmd/restic/progress.go @@ -106,10 +106,6 @@ func newIndexProgress(quiet bool, json bool) *progress.Counter { return newProgressMax(!quiet && !json && terminal.StdoutIsTerminal(), 0, "index files loaded") } -func newIndexTerminalProgress(quiet bool, json bool, term *termstatus.Terminal) *progress.Counter { - return newTerminalProgressMax(!quiet && !json && terminal.StdoutIsTerminal(), 0, "index files loaded", term) -} - type terminalProgressPrinter struct { term *termstatus.Terminal ui.Message @@ -120,6 +116,10 @@ func (t *terminalProgressPrinter) NewCounter(description string) *progress.Count return newTerminalProgressMax(t.show, 0, description, t.term) } +func (t *terminalProgressPrinter) NewCounterTerminalOnly(description string) *progress.Counter { + return newTerminalProgressMax(t.show && terminal.StdoutIsTerminal(), 0, description, t.term) +} + func newTerminalProgressPrinter(json bool, verbosity uint, term *termstatus.Terminal) progress.Printer { if json { verbosity = 0 @@ -130,3 +130,7 @@ func newTerminalProgressPrinter(json bool, verbosity uint, term *termstatus.Term show: verbosity > 0, } } + +func newIndexTerminalProgress(printer progress.Printer) *progress.Counter { + return printer.NewCounterTerminalOnly("index files loaded") +} diff --git a/internal/ui/progress/printer.go b/internal/ui/progress/printer.go index c3e0d17a3..a1adc4af4 100644 --- a/internal/ui/progress/printer.go +++ b/internal/ui/progress/printer.go @@ -7,6 +7,7 @@ import "testing" // It must be safe to call its methods from concurrent goroutines. type Printer interface { NewCounter(description string) *Counter + NewCounterTerminalOnly(description string) *Counter // E prints to stderr E(msg string, args ...interface{}) @@ -29,6 +30,10 @@ func (*NoopPrinter) NewCounter(_ string) *Counter { return nil } +func (*NoopPrinter) NewCounterTerminalOnly(_ string) *Counter { + return nil +} + func (*NoopPrinter) E(_ string, _ ...interface{}) {} func (*NoopPrinter) S(_ string, _ ...interface{}) {} @@ -56,6 +61,10 @@ func (p *TestPrinter) NewCounter(_ string) *Counter { return nil } +func (p *TestPrinter) NewCounterTerminalOnly(_ string) *Counter { + return nil +} + func (p *TestPrinter) E(msg string, args ...interface{}) { p.t.Logf("error: "+msg, args...) }