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
|
package decor
import (
"fmt"
"time"
)
// Elapsed returns elapsed time decorator.
//
// `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
//
// `wcc` optional WC config
func Elapsed(style TimeStyle, wcc ...WC) Decorator {
var wc WC
for _, widthConf := range wcc {
wc = widthConf
}
wc.Init()
d := &elapsedDecorator{
WC: wc,
style: style,
startTime: time.Now(),
}
return d
}
type elapsedDecorator struct {
WC
style TimeStyle
startTime time.Time
msg string
completeMsg *string
}
func (d *elapsedDecorator) Decor(st *Statistics) string {
if st.Completed {
if d.completeMsg != nil {
return d.FormatMsg(*d.completeMsg)
}
return d.FormatMsg(d.msg)
}
timeElapsed := time.Since(d.startTime)
hours := int64((timeElapsed / time.Hour) % 60)
minutes := int64((timeElapsed / time.Minute) % 60)
seconds := int64((timeElapsed / time.Second) % 60)
switch d.style {
case ET_STYLE_GO:
d.msg = fmt.Sprint(time.Duration(timeElapsed.Seconds()) * time.Second)
case ET_STYLE_HHMMSS:
d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
case ET_STYLE_HHMM:
d.msg = fmt.Sprintf("%02d:%02d", hours, minutes)
case ET_STYLE_MMSS:
if hours > 0 {
d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
} else {
d.msg = fmt.Sprintf("%02d:%02d", minutes, seconds)
}
}
return d.FormatMsg(d.msg)
}
func (d *elapsedDecorator) OnCompleteMessage(msg string) {
d.completeMsg = &msg
}
|