Codebase list golang-github-vbauerster-mpb / ee6c2fd progress.go
ee6c2fd

Tree @ee6c2fd (Download .tar.gz)

progress.go @ee6c2fdraw · history · blame

  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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package mpb

import (
	"errors"
	"fmt"
	"io"
	"os"
	"runtime"
	"sync"
	"time"
	"unicode/utf8"

	"github.com/vbauerster/mpb/cwriter"
)

// ErrCallAfterStop thrown by panic, if Progress methods like (*Progress).AddBar()
// are called after (*Progress).Stop() has been called
var ErrCallAfterStop = errors.New("method call on stopped Progress instance")

// default RefreshRate
var rr = 100 * time.Millisecond

type (
	// BeforeRender is a func, which gets called before render process
	BeforeRender func([]*Bar)
	barAction    uint

	bCommandData struct {
		action barAction
		bar    *Bar
		result chan bool
	}

	widthSync struct {
		listen []chan int
		result []chan int
	}

	// config changeable by user
	userConf struct {
		width        int
		format       string
		beforeRender BeforeRender
		cw           *cwriter.Writer
		ticker       *time.Ticker

		shutdownNotifier chan struct{}
		cancel           <-chan struct{}
	}
)

const (
	bAdd barAction = iota
	bRemove
)

const (
	// default width
	pwidth = 80
	// default format
	pformat = "[=>-]"
	// number of format runes for bar
	numFmtRunes = 5
)

// Progress represents the container that renders Progress bars
type Progress struct {
	// WaitGroup for internal rendering sync
	wg *sync.WaitGroup

	done          chan struct{}
	userConf      chan userConf
	bCommandCh    chan *bCommandData
	barCountReqCh chan chan int
	beforeStop    chan struct{}
}

// New creates new Progress instance, which will orchestrate bars rendering
// process. It acceepts context.Context, for cancellation.
// If you don't plan to cancel, it is safe to feed with nil
func New() *Progress {
	p := &Progress{
		wg:            new(sync.WaitGroup),
		done:          make(chan struct{}),
		userConf:      make(chan userConf),
		bCommandCh:    make(chan *bCommandData),
		barCountReqCh: make(chan chan int),
		beforeStop:    make(chan struct{}),
	}
	go p.server(userConf{
		width:  pwidth,
		format: pformat,
		cw:     cwriter.New(os.Stdout),
		ticker: time.NewTicker(rr),
	})
	return p
}

// WithCancel cancellation via channel.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
// or nil channel passed
func (p *Progress) WithCancel(ch <-chan struct{}) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	if ch == nil {
		panic("nil cancel channel")
	}
	conf := <-p.userConf
	conf.cancel = ch
	p.userConf <- conf
	return p
}

// SetWidth overrides default (70) width of bar(s).
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) SetWidth(width int) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	if width < 0 {
		return p
	}
	conf := <-p.userConf
	conf.width = width
	p.userConf <- conf
	return p
}

// SetOut sets underlying writer of progress. Default one is os.Stdout.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) SetOut(w io.Writer) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	if w == nil {
		return p
	}
	conf := <-p.userConf
	conf.cw.Flush()
	conf.cw = cwriter.New(w)
	p.userConf <- conf
	return p
}

// RefreshRate overrides default (100ms) refresh rate value
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) RefreshRate(d time.Duration) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	conf := <-p.userConf
	conf.ticker.Stop()
	rr = d
	conf.ticker = time.NewTicker(rr)
	p.userConf <- conf
	return p
}

// BeforeRenderFunc accepts a func, which gets called before render process.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	conf := <-p.userConf
	conf.beforeRender = f
	p.userConf <- conf
	return p
}

// AddBar creates a new progress bar and adds to the container.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) AddBar(total int64) *Bar {
	return p.AddBarWithID(0, total)
}

// AddBarWithID creates a new progress bar and adds to the container.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) AddBarWithID(id int, total int64) *Bar {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	conf := <-p.userConf
	result := make(chan bool)
	bar := newBar(id, total, p.wg, &conf)
	p.bCommandCh <- &bCommandData{bAdd, bar, result}
	if <-result {
		p.wg.Add(1)
	}
	return bar
}

// RemoveBar removes bar at any time.
// Pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) RemoveBar(b *Bar) bool {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	result := make(chan bool)
	p.bCommandCh <- &bCommandData{bRemove, b, result}
	return <-result
}

// BarCount returns bars count in the container.
// Pancis if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) BarCount() int {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	respCh := make(chan int, 1)
	p.barCountReqCh <- respCh
	return <-respCh
}

// ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	conf := <-p.userConf
	conf.shutdownNotifier = ch
	p.userConf <- conf
	return p
}

// Format sets custom format for underlying bar(s), default one is "[=>-]".
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (p *Progress) Format(format string) *Progress {
	if isClosed(p.done) {
		panic(ErrCallAfterStop)
	}
	if utf8.RuneCountInString(format) != numFmtRunes {
		return p
	}
	conf := <-p.userConf
	conf.format = format
	p.userConf <- conf
	return p
}

// Stop shutdowns Progress' goroutine.
// Should be called only after each bar's work done, i.e. bar has reached its
// 100 %. It is NOT for cancelation. Use WithContext or WithCancel for
// cancelation purposes.
func (p *Progress) Stop() {
	if isClosed(p.done) {
		return
	}
	p.beforeStop <- struct{}{}
	p.wg.Wait()
	close(p.bCommandCh)
}

// server monitors underlying channels and renders any progress bars
func (p *Progress) server(conf userConf) {

	defer func() {
		conf.ticker.Stop()
		close(p.done)
		if conf.shutdownNotifier != nil {
			close(conf.shutdownNotifier)
		}
	}()

	recoverFn := func(ch chan []byte) {
		if p := recover(); p != nil {
			var buf [4096]byte
			n := runtime.Stack(buf[:], false)
			os.Stderr.Write(buf[:n])
			ch <- []byte(fmt.Sprintln(p))
		}
		close(ch)
	}

	bars := make([]*Bar, 0, 3)

	for {
		select {
		case p.userConf <- conf:
		case conf = <-p.userConf:
		case data, ok := <-p.bCommandCh:
			if !ok {
				return
			}
			switch data.action {
			case bAdd:
				bars = append(bars, data.bar)
				data.result <- true
			case bRemove:
				var ok bool
				for i, b := range bars {
					if b == data.bar {
						bars = append(bars[:i], bars[i+1:]...)
						ok = true
						b.remove()
						break
					}
				}
				data.result <- ok
			}
		case respCh := <-p.barCountReqCh:
			respCh <- len(bars)
		case <-conf.ticker.C:
			numBars := len(bars)

			if numBars == 0 {
				break
			}

			if conf.beforeRender != nil {
				conf.beforeRender(bars)
			}

			quitWidthSyncCh := make(chan struct{})
			time.AfterFunc(rr, func() {
				close(quitWidthSyncCh)
			})

			b0 := bars[0]
			prependWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfPrependers())
			appendWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfAppenders())

			width, _, _ := cwriter.GetTermSize()

			sequence := make([]<-chan []byte, numBars)
			for i, b := range bars {
				sequence[i] = b.render(recoverFn, width, prependWs, appendWs)
			}

			ch := fanIn(sequence...)

			for buf := range ch {
				conf.cw.Write(buf)
			}

			conf.cw.Flush()

			for _, b := range bars {
				b.flushed()
			}
		case <-p.beforeStop:
			for _, b := range bars {
				if b.GetStatistics().Total <= 0 {
					b.Completed()
				}
			}
		case <-conf.cancel:
			return
		}
	}
}

func newWidthSync(quit <-chan struct{}, numBars, numColumn int) *widthSync {
	ws := &widthSync{
		listen: make([]chan int, numColumn),
		result: make([]chan int, numColumn),
	}
	for i := 0; i < numColumn; i++ {
		ws.listen[i] = make(chan int, numBars)
		ws.result[i] = make(chan int, numBars)
	}
	for i := 0; i < numColumn; i++ {
		go func(listenCh <-chan int, resultCh chan<- int) {
			defer close(resultCh)
			widths := make([]int, 0, numBars)
		loop:
			for {
				select {
				case w := <-listenCh:
					widths = append(widths, w)
					if len(widths) == numBars {
						break loop
					}
				case <-quit:
					if len(widths) == 0 {
						return
					}
					break loop
				}
			}
			result := max(widths)
			for i := 0; i < len(widths); i++ {
				resultCh <- result
			}
		}(ws.listen[i], ws.result[i])
	}
	return ws
}

func fanIn(inputs ...<-chan []byte) <-chan []byte {
	ch := make(chan []byte)

	go func() {
		defer close(ch)
		for _, input := range inputs {
			ch <- <-input
		}
	}()

	return ch
}

// isClosed check if ch closed
// caution see: http://www.tapirgames.com/blog/golang-channel-closing
func isClosed(ch <-chan struct{}) bool {
	select {
	case <-ch:
		return true
	default:
		return false
	}
}

func max(slice []int) int {
	max := slice[0]

	for i := 1; i < len(slice); i++ {
		if slice[i] > max {
			max = slice[i]
		}
	}

	return max
}