forked from ezrec/uv3dp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress.go
60 lines (48 loc) · 1.06 KB
/
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
//
// Copyright (c) 2020 Jason S. McMullan <[email protected]>
//
package uv3dp
type Progressor interface {
Show(percent float32)
Stop()
}
type nilProgress struct{}
func (np *nilProgress) Show(float32) {}
func (np *nilProgress) Stop() {}
var defaultProgress = Progressor(&nilProgress{})
func SetProgress(prog Progressor) {
if prog == Progressor(nil) {
prog = &nilProgress{}
}
defaultProgress = prog
}
type Progress struct {
Progressor
Completed chan struct{}
Done chan struct{}
}
func NewProgress(total int) (prog *Progress) {
prog = &Progress{
Progressor: defaultProgress,
Completed: make(chan struct{}, total),
Done: make(chan struct{}),
}
go func(prog *Progress) {
for completion := 0; completion < total; completion++ {
prog.Show(float32(completion) * 100.0 / float32(total))
<-prog.Completed
}
prog.Show(100.0)
prog.Stop()
close(prog.Done)
}(prog)
return
}
func (prog *Progress) Indicate() {
prog.Completed <- struct{}{}
return
}
func (prog *Progress) Close() {
<-prog.Done
close(prog.Completed)
}