1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package progress
import (
"context"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/jedib0t/go-pretty/v6/text"
"golang.org/x/term"
)
var (
// DefaultLengthTracker defines a sane value for a Tracker's length.
DefaultLengthTracker = 20
// DefaultUpdateFrequency defines a sane value for the frequency with which
// all the Tracker's get updated on the screen.
DefaultUpdateFrequency = time.Millisecond * 250
)
// Progress helps track progress for one or more tasks.
type Progress struct {
autoStop bool
lengthMessage int
lengthProgress int
lengthProgressOverall int
lengthTracker int
logsToRender []string
logsToRenderMutex sync.RWMutex
numTrackersExpected int64
outputWriter io.Writer
overallTracker *Tracker
overallTrackerMutex sync.RWMutex
pinnedMessages []string
pinnedMessageMutex sync.RWMutex
pinnedMessageNumLines int
renderContext context.Context
renderContextCancel context.CancelFunc
renderInProgress bool
renderInProgressMutex sync.RWMutex
sortBy SortBy
style *Style
terminalWidth int
terminalWidthMutex sync.RWMutex
terminalWidthOverride int
trackerPosition Position
trackersActive []*Tracker
trackersActiveMutex sync.RWMutex
trackersDone []*Tracker
trackersDoneMutex sync.RWMutex
trackersInQueue []*Tracker
trackersInQueueMutex sync.RWMutex
updateFrequency time.Duration
}
// Position defines the position of the Tracker with respect to the Tracker's
// Message.
type Position int
const (
// PositionLeft will make the Tracker be displayed first before the Message.
PositionLeft Position = iota
// PositionRight will make the Tracker be displayed after the Message.
PositionRight
)
// AppendTracker appends a single Tracker for tracking. The Tracker gets added
// to a queue, which gets picked up by the Render logic in the next rendering
// cycle.
func (p *Progress) AppendTracker(t *Tracker) {
if !t.DeferStart {
t.start()
}
p.overallTrackerMutex.Lock()
defer p.overallTrackerMutex.Unlock()
if p.overallTracker == nil {
p.overallTracker = &Tracker{Total: 1}
if p.numTrackersExpected > 0 {
p.overallTracker.Total = p.numTrackersExpected * 100
}
p.overallTracker.start()
}
// append the tracker to the "in-queue" list
p.trackersInQueueMutex.Lock()
p.trackersInQueue = append(p.trackersInQueue, t)
p.trackersInQueueMutex.Unlock()
// update the expected total progress since we are appending a new tracker
p.overallTracker.UpdateTotal(int64(p.Length()) * 100)
}
// AppendTrackers appends one or more Trackers for tracking.
func (p *Progress) AppendTrackers(trackers []*Tracker) {
for _, tracker := range trackers {
p.AppendTracker(tracker)
}
}
// IsRenderInProgress returns true if a call to Render() was made, and is still
// in progress and has not ended yet.
func (p *Progress) IsRenderInProgress() bool {
p.renderInProgressMutex.RLock()
defer p.renderInProgressMutex.RUnlock()
return p.renderInProgress
}
// Length returns the number of Trackers tracked overall.
func (p *Progress) Length() int {
p.trackersActiveMutex.RLock()
p.trackersDoneMutex.RLock()
p.trackersInQueueMutex.RLock()
out := len(p.trackersInQueue) + len(p.trackersActive) + len(p.trackersDone)
p.trackersInQueueMutex.RUnlock()
p.trackersDoneMutex.RUnlock()
p.trackersActiveMutex.RUnlock()
return out
}
// LengthActive returns the number of Trackers actively tracked (not done yet).
func (p *Progress) LengthActive() int {
p.trackersActiveMutex.RLock()
p.trackersInQueueMutex.RLock()
out := len(p.trackersInQueue) + len(p.trackersActive)
p.trackersInQueueMutex.RUnlock()
p.trackersActiveMutex.RUnlock()
return out
}
// LengthDone returns the number of Trackers that are done tracking.
func (p *Progress) LengthDone() int {
p.trackersDoneMutex.RLock()
out := len(p.trackersDone)
p.trackersDoneMutex.RUnlock()
return out
}
// LengthInQueue returns the number of Trackers in queue to be actively tracked
// (not tracking yet).
func (p *Progress) LengthInQueue() int {
p.trackersInQueueMutex.RLock()
out := len(p.trackersInQueue)
p.trackersInQueueMutex.RUnlock()
return out
}
// Log appends a log to display above the active progress bars during the next
// refresh.
func (p *Progress) Log(msg string, a ...interface{}) {
if len(a) > 0 {
msg = fmt.Sprintf(msg, a...)
}
p.logsToRenderMutex.Lock()
p.logsToRender = append(p.logsToRender, msg)
p.logsToRenderMutex.Unlock()
}
// SetAutoStop toggles the auto-stop functionality. Auto-stop set to true would
// mean that the Render() function will automatically stop once all currently
// active Trackers reach their final states. When set to false, the client code
// will have to call Progress.Stop() to stop the Render() logic. Default: false.
func (p *Progress) SetAutoStop(autoStop bool) {
p.autoStop = autoStop
}
// SetMessageLength sets the (printed) length of the tracker message. Any
// message longer the specified length will be snipped. Any message shorter than
// the specified width will be padded with spaces.
func (p *Progress) SetMessageLength(length int) {
p.lengthMessage = length
}
// SetMessageWidth sets the (printed) length of the tracker message. Any message
// longer the specified width will be snipped. Any message shorter than the
// specified width will be padded with spaces.
// Deprecated: in favor of SetMessageLength(length)
func (p *Progress) SetMessageWidth(width int) {
p.lengthMessage = width
}
// SetNumTrackersExpected sets the expected number of trackers to be tracked.
// This helps calculate the overall progress with better accuracy.
func (p *Progress) SetNumTrackersExpected(numTrackers int) {
p.numTrackersExpected = int64(numTrackers)
}
// SetOutputWriter redirects the output of Render to an io.writer object like
// os.Stdout or os.Stderr or a file. Warning: redirecting the output to a file
// may not work well as the Render() logic moves the cursor around a lot.
func (p *Progress) SetOutputWriter(writer io.Writer) {
p.outputWriter = writer
}
// SetPinnedMessages sets message(s) pinned above all the trackers of the
// progress bar. This method can be used to overwrite all the pinned messages.
// Call this function without arguments to "clear" the pinned messages.
func (p *Progress) SetPinnedMessages(messages ...string) {
p.pinnedMessageMutex.Lock()
defer p.pinnedMessageMutex.Unlock()
p.pinnedMessages = messages
}
// SetSortBy defines the sorting mechanism to use to sort the Active Trackers
// before rendering. Default: no-sorting == sort-by-insertion-order.
func (p *Progress) SetSortBy(sortBy SortBy) {
p.sortBy = sortBy
}
// SetStyle sets the Style to use for rendering.
func (p *Progress) SetStyle(style Style) {
p.style = &style
}
// SetTerminalWidth sets up a sticky terminal width and prevents the Progress
// Writer from polling for the real width during render.
func (p *Progress) SetTerminalWidth(width int) {
p.terminalWidthOverride = width
}
// SetTrackerLength sets the text-length of all the Trackers.
func (p *Progress) SetTrackerLength(length int) {
p.lengthTracker = length
}
// SetTrackerPosition sets the position of the tracker with respect to the
// Tracker message text.
func (p *Progress) SetTrackerPosition(position Position) {
p.trackerPosition = position
}
// SetUpdateFrequency sets the update frequency while rendering the trackers.
// the lower the value, the more frequently the Trackers get refreshed. A
// sane value would be 250ms.
func (p *Progress) SetUpdateFrequency(frequency time.Duration) {
p.updateFrequency = frequency
}
// ShowETA toggles showing the ETA for all individual trackers.
// Deprecated: in favor of Style().Visibility.ETA
func (p *Progress) ShowETA(show bool) {
p.Style().Visibility.ETA = show
}
// ShowPercentage toggles showing the Percent complete for each Tracker.
// Deprecated: in favor of Style().Visibility.Percentage
func (p *Progress) ShowPercentage(show bool) {
p.Style().Visibility.Percentage = show
}
// ShowOverallTracker toggles showing the Overall progress tracker with an ETA.
// Deprecated: in favor of Style().Visibility.TrackerOverall
func (p *Progress) ShowOverallTracker(show bool) {
p.Style().Visibility.TrackerOverall = show
}
// ShowTime toggles showing the Time taken by each Tracker.
// Deprecated: in favor of Style().Visibility.Time
func (p *Progress) ShowTime(show bool) {
p.Style().Visibility.Time = show
}
// ShowTracker toggles showing the Tracker (the progress bar).
// Deprecated: in favor of Style().Visibility.Tracker
func (p *Progress) ShowTracker(show bool) {
p.Style().Visibility.Tracker = show
}
// ShowValue toggles showing the actual Value of the Tracker.
// Deprecated: in favor of Style().Visibility.Value
func (p *Progress) ShowValue(show bool) {
p.Style().Visibility.Value = show
}
// Stop stops the Render() logic that is in progress.
func (p *Progress) Stop() {
if p.IsRenderInProgress() {
p.renderContextCancel()
}
}
// Style returns the current Style.
func (p *Progress) Style() *Style {
if p.style == nil {
tempStyle := StyleDefault
p.style = &tempStyle
}
return p.style
}
func (p *Progress) getTerminalWidth() int {
p.terminalWidthMutex.RLock()
defer p.terminalWidthMutex.RUnlock()
if p.terminalWidthOverride > 0 {
return p.terminalWidthOverride
}
return p.terminalWidth
}
func (p *Progress) initForRender() {
// pick a default style
p.Style()
if p.style.Options.SpeedOverallFormatter == nil {
p.style.Options.SpeedOverallFormatter = FormatNumber
}
// reset the signals
p.renderContext, p.renderContextCancel = context.WithCancel(context.Background())
// pick default lengths if no valid ones set
if p.lengthTracker <= 0 {
p.lengthTracker = DefaultLengthTracker
}
// calculate length of the actual progress bar by discounting the left/right
// border/box chars
p.lengthProgress = p.lengthTracker -
text.RuneWidthWithoutEscSequences(p.style.Chars.BoxLeft) -
text.RuneWidthWithoutEscSequences(p.style.Chars.BoxRight)
p.lengthProgressOverall = p.lengthMessage +
text.RuneWidthWithoutEscSequences(p.style.Options.Separator) +
p.lengthProgress + 1
if p.style.Visibility.Percentage {
p.lengthProgressOverall += text.RuneWidthWithoutEscSequences(
fmt.Sprintf(p.style.Options.PercentFormat, 0.0),
)
}
// if not output write has been set, output to STDOUT
if p.outputWriter == nil {
p.outputWriter = os.Stdout
}
// pick a sane update frequency if none set
if p.updateFrequency <= 0 {
p.updateFrequency = DefaultUpdateFrequency
}
// get the current terminal size for preventing roll-overs, and do this in a
// background loop until end of render
go p.watchTerminalSize() // needs p.updateFrequency
}
func (p *Progress) updateTerminalSize() {
p.terminalWidthMutex.Lock()
defer p.terminalWidthMutex.Unlock()
p.terminalWidth, _, _ = term.GetSize(int(os.Stdout.Fd()))
}
func (p *Progress) watchTerminalSize() {
// once
p.updateTerminalSize()
// until end of time
ticker := time.NewTicker(time.Second / 10)
for {
select {
case <-ticker.C:
p.updateTerminalSize()
case <-p.renderContext.Done():
return
}
}
}
// renderHint has hints for the Render*() logic
type renderHint struct {
hideTime bool // hide the time
hideValue bool // hide the value
isOverallTracker bool // is the Overall Progress tracker
}
|