From 8510f09225b2733d755ea33d5b516571cda8afa5 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 13 Sep 2025 23:28:30 +0200 Subject: [PATCH] stats: convert to termstatus --- cmd/restic/cmd_stats.go | 42 +++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index 36c567fee..20b4d12eb 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -14,7 +14,9 @@ import ( "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restorer" "github.com/restic/restic/internal/ui" + "github.com/restic/restic/internal/ui/progress" "github.com/restic/restic/internal/ui/table" + "github.com/restic/restic/internal/ui/termstatus" "github.com/restic/restic/internal/walker" "github.com/spf13/cobra" @@ -62,7 +64,9 @@ Exit status is 12 if the password is incorrect. GroupID: cmdGroupDefault, DisableAutoGenTag: true, RunE: func(cmd *cobra.Command, args []string) error { - return runStats(cmd.Context(), opts, globalOptions, args) + term, cancel := setupTermstatus() + defer cancel() + return runStats(cmd.Context(), opts, globalOptions, args, term) }, } @@ -92,12 +96,14 @@ func must(err error) { } } -func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args []string) error { +func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error { err := verifyStatsInput(opts) if err != nil { return err } + printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) + ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock) if err != nil { return err @@ -108,17 +114,17 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args if err != nil { return err } - bar := newIndexProgress(gopts.Quiet, gopts.JSON) + bar := newIndexTerminalProgress(printer) if err = repo.LoadIndex(ctx, bar); err != nil { return err } if opts.countMode == countModeDebug { - return statsDebug(ctx, repo) + return statsDebug(ctx, repo, printer) } if !gopts.JSON { - Printf("scanning...\n") + printer.S("scanning...") } // create a container for the stats (and other needed state) @@ -173,26 +179,26 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args return nil } - Printf("Stats in %s mode:\n", opts.countMode) - Printf(" Snapshots processed: %d\n", stats.SnapshotsCount) + printer.S("Stats in %s mode:", opts.countMode) + printer.S(" Snapshots processed: %d", stats.SnapshotsCount) if stats.TotalBlobCount > 0 { - Printf(" Total Blob Count: %d\n", stats.TotalBlobCount) + printer.S(" Total Blob Count: %d", stats.TotalBlobCount) } if stats.TotalFileCount > 0 { - Printf(" Total File Count: %d\n", stats.TotalFileCount) + printer.S(" Total File Count: %d", stats.TotalFileCount) } if stats.TotalUncompressedSize > 0 { - Printf(" Total Uncompressed Size: %-5s\n", ui.FormatBytes(stats.TotalUncompressedSize)) + printer.S(" Total Uncompressed Size: %-5s", ui.FormatBytes(stats.TotalUncompressedSize)) } - Printf(" Total Size: %-5s\n", ui.FormatBytes(stats.TotalSize)) + printer.S(" Total Size: %-5s", ui.FormatBytes(stats.TotalSize)) if stats.CompressionProgress > 0 { - Printf(" Compression Progress: %.2f%%\n", stats.CompressionProgress) + printer.S(" Compression Progress: %.2f%%", stats.CompressionProgress) } if stats.CompressionRatio > 0 { - Printf(" Compression Ratio: %.2fx\n", stats.CompressionRatio) + printer.S(" Compression Ratio: %.2fx", stats.CompressionRatio) } if stats.CompressionSpaceSaving > 0 { - Printf("Compression Space Saving: %.2f%%\n", stats.CompressionSpaceSaving) + printer.S("Compression Space Saving: %.2f%%", stats.CompressionSpaceSaving) } return nil @@ -359,14 +365,14 @@ const ( countModeDebug = "debug" ) -func statsDebug(ctx context.Context, repo restic.Repository) error { - Warnf("Collecting size statistics\n\n") +func statsDebug(ctx context.Context, repo restic.Repository, printer progress.Printer) error { + printer.E("Collecting size statistics\n\n") for _, t := range []restic.FileType{restic.KeyFile, restic.LockFile, restic.IndexFile, restic.PackFile} { hist, err := statsDebugFileType(ctx, repo, t) if err != nil { return err } - Warnf("File Type: %v\n%v\n", t, hist) + printer.E("File Type: %v\n%v", t, hist) } hist, err := statsDebugBlobs(ctx, repo) @@ -374,7 +380,7 @@ func statsDebug(ctx context.Context, repo restic.Repository) error { return err } for _, t := range []restic.BlobType{restic.DataBlob, restic.TreeBlob} { - Warnf("Blob Type: %v\n%v\n\n", t, hist[t]) + printer.E("Blob Type: %v\n%v\n\n", t, hist[t]) } return nil