/
progress.go
62 lines (49 loc) · 870 Bytes
/
progress.go
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
package gui
import "sync"
type Progress struct {
Title string
Progress float64
trigger chan struct{}
done chan struct{}
sync.Mutex
}
func NewProgress(title string) *Progress {
p := &Progress{
Title: title,
trigger: make(chan struct{}),
done: make(chan struct{}),
}
go p.monitor()
return p
}
func (self *Progress) Update(progress float64) {
self.Lock()
self.Progress = progress
self.Unlock()
// Try to send a trigger
select {
case self.trigger <- struct{}{}:
default:
}
}
func (self *Progress) Done() {
self.done <- struct{}{}
}
func (self *Progress) monitor() {
for {
select {
case <-self.done:
// Done :)
return
case <-self.trigger:
// Update
// Block so updates is triggerd to offen
select {
case <-time.After(time.Milisecond * 100):
case <-self.done:
// Done :)
return
}
}
}
}