| 121 | } |
| 122 | |
| 123 | func renderOperationStatus(ctx context.Context, progressChan casclient.ProgressStatusChan, totalSize int64) { |
| 124 | pw := progress.NewWriter() |
| 125 | pw.Style().Visibility.ETA = true |
| 126 | pw.Style().Visibility.Speed = true |
| 127 | pw.SetUpdateFrequency(progress.DefaultUpdateFrequency) |
| 128 | // Render to stderr to avoid interfering with the output if the flag --output is set to "-" |
| 129 | pw.SetOutputWriter(os.Stderr) |
| 130 | |
| 131 | var tracker *progress.Tracker |
| 132 | go pw.Render() |
| 133 | defer pw.Stop() |
| 134 | |
| 135 | for { |
| 136 | select { |
| 137 | case <-ctx.Done(): |
| 138 | return |
| 139 | case status, ok := <-progressChan: |
| 140 | if !ok { |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | // Initialize tracker |
| 145 | if tracker == nil { |
| 146 | // Hack: Add 1 to the total to make sure the tracker is not marked as done before the upload is finished |
| 147 | // this way the current value will never reach the total |
| 148 | // but instead the tracker will be marked as done by the defer statement |
| 149 | tracker = &progress.Tracker{Total: totalSize + 1, Units: progress.UnitsBytes} |
| 150 | defer tracker.MarkAsDone() |
| 151 | pw.AppendTracker(tracker) |
| 152 | } |
| 153 | |
| 154 | tracker.SetValue(status.ProcessedBytes) |
| 155 | } |
| 156 | } |
| 157 | } |