centralize index progress bar for termstatus

This commit is contained in:
Michael Eischer
2025-09-13 23:17:15 +02:00
parent 6cdb9a75e6
commit 32b7168a9e
8 changed files with 29 additions and 12 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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")
}

View File

@@ -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...)
}