-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
progress.go
44 lines (34 loc) · 877 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
package v1alpha1
import (
"fmt"
"strconv"
"strings"
)
// Progress in N/M format. N is number of task complete. M is number of tasks.
type Progress string
func NewProgress(n, m int64) (Progress, bool) {
return ParseProgress(fmt.Sprintf("%v/%v", n, m))
}
func ParseProgress(s string) (Progress, bool) {
v := Progress(s)
return v, v.IsValid()
}
func (in Progress) parts() []string {
return strings.SplitN(string(in), "/", 2)
}
func (in Progress) N() int64 {
return parseInt64(in.parts()[0])
}
func (in Progress) M() int64 {
return parseInt64(in.parts()[1])
}
func (in Progress) Add(x Progress) Progress {
return Progress(fmt.Sprintf("%v/%v", in.N()+x.N(), in.M()+x.M()))
}
func (in Progress) IsValid() bool {
return in != "" && in.N() >= 0 && in.N() <= in.M() && in.M() > 0
}
func parseInt64(s string) int64 {
v, _ := strconv.ParseInt(s, 10, 64)
return v
}