-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_statistics.go
63 lines (56 loc) · 1.57 KB
/
build_statistics.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
63
package circle
import (
"bytes"
"fmt"
"os"
"strings"
"time"
"golang.org/x/crypto/ssh/terminal"
)
func roundDuration(d CircleDuration, unit time.Duration) time.Duration {
return ((time.Duration(d) + unit/2) / unit) * unit
}
const stepWidth = 45
var stepPadding = fmt.Sprintf("%%-%ds", stepWidth)
func isatty() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
// Statistics prints out statistics for the given build. If stdout is a TTY,
// failed builds will be surrounded by red ANSI escape sequences.
func (cb *CircleBuild) Statistics() string {
var b bytes.Buffer
b.WriteString(fmt.Sprintf(stepPadding, "Step"))
l := stepWidth
for i := uint8(0); i < cb.Parallel; i++ {
b.WriteString(fmt.Sprintf("%-8d", i))
l += 8
}
b.WriteString(fmt.Sprintf("\n%s\n", strings.Repeat("=", l)))
for _, step := range cb.Steps {
var stepName string
if len(step.Name) > stepWidth-2 {
stepName = fmt.Sprintf("%s… ", step.Name[:(stepWidth-2)])
} else {
stepName = fmt.Sprintf(stepPadding, step.Name)
}
b.WriteString(stepName)
for _, action := range step.Actions {
var dur time.Duration
if time.Duration(action.Runtime) > time.Minute {
dur = roundDuration(action.Runtime, time.Second)
} else {
dur = roundDuration(action.Runtime, time.Millisecond*10)
}
var durString string
if action.Failed() && isatty() {
// color the output red
durString = fmt.Sprintf("\033[38;05;160m%-8s\033[0m", dur.String())
} else {
durString = fmt.Sprintf("%-8s", dur.String())
}
b.WriteString(durString)
}
b.WriteString("\n")
}
return b.String()
}