startProgress starts the progress bar printing It returns a func which should be called to stop the stats.
()
| 25 | // |
| 26 | // It returns a func which should be called to stop the stats. |
| 27 | func startProgress() func() { |
| 28 | stopStats := make(chan struct{}) |
| 29 | oldSyncPrint := operations.SyncPrintf |
| 30 | |
| 31 | if !log.Redirected() { |
| 32 | // Intercept the log calls if not logging to file or syslog |
| 33 | log.Handler.SetOutput(func(level slog.Level, text string) { |
| 34 | printProgress(text) |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // Intercept output from functions such as HashLister to stdout |
| 39 | operations.SyncPrintf = func(format string, a ...any) { |
| 40 | printProgress(fmt.Sprintf(format, a...)) |
| 41 | } |
| 42 | |
| 43 | var wg sync.WaitGroup |
| 44 | wg.Go(func() { |
| 45 | progressInterval := defaultProgressInterval |
| 46 | if ShowStats() && *statsInterval > 0 { |
| 47 | progressInterval = *statsInterval |
| 48 | } |
| 49 | ticker := time.NewTicker(progressInterval) |
| 50 | for { |
| 51 | select { |
| 52 | case <-ticker.C: |
| 53 | printProgress("") |
| 54 | case <-stopStats: |
| 55 | ticker.Stop() |
| 56 | printProgress("") |
| 57 | if !log.Redirected() { |
| 58 | // Reset intercept of the log calls |
| 59 | log.Handler.ResetOutput() |
| 60 | } |
| 61 | operations.SyncPrintf = oldSyncPrint |
| 62 | fmt.Println("") |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | }) |
| 67 | return func() { |
| 68 | close(stopStats) |
| 69 | wg.Wait() |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // state for the progress printing |
| 74 | var ( |
no test coverage detected
searching dependent graphs…