summaryrefslogtreecommitdiff
path: root/vendor/github.com/vbauerster/mpb/progress_posix.go
blob: 545245a4231ad165fcf453abc4673c9cf47eda74 (plain)
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
// +build !windows

package mpb

import (
	"os"
	"os/signal"
	"syscall"
	"time"
)

func (p *Progress) serve(s *pState) {

	var ticker *time.Ticker
	var refreshCh <-chan time.Time
	var winch chan os.Signal
	var resumeTimer *time.Timer
	var resumeEvent <-chan time.Time
	winchIdleDur := s.rr * 2

	if s.manualRefreshCh == nil {
		ticker = time.NewTicker(s.rr)
		refreshCh = ticker.C
		winch = make(chan os.Signal, 2)
		signal.Notify(winch, syscall.SIGWINCH)
	} else {
		refreshCh = s.manualRefreshCh
	}

	for {
		select {
		case op := <-p.operateState:
			op(s)
		case <-refreshCh:
			if s.zeroWait {
				if s.manualRefreshCh == nil {
					signal.Stop(winch)
					ticker.Stop()
				}
				if s.shutdownNotifier != nil {
					close(s.shutdownNotifier)
				}
				close(p.done)
				return
			}
			tw, err := s.cw.GetWidth()
			if err != nil {
				tw = s.width
			}
			s.render(tw)
		case <-winch:
			tw, err := s.cw.GetWidth()
			if err != nil {
				tw = s.width
			}
			s.render(tw - tw/8)
			if resumeTimer != nil && resumeTimer.Reset(winchIdleDur) {
				break
			}
			ticker.Stop()
			resumeTimer = time.NewTimer(winchIdleDur)
			resumeEvent = resumeTimer.C
		case <-resumeEvent:
			ticker = time.NewTicker(s.rr)
			refreshCh = ticker.C
			resumeEvent = nil
			resumeTimer = nil
		}
	}
}