-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
156 lines (137 loc) · 3.4 KB
/
task.go
File metadata and controls
156 lines (137 loc) · 3.4 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package timedtask
import (
"fmt"
"io"
"strings"
"time"
)
// Task is a timed task.
type Task struct {
parent *Task
description string
depth int // The nesting depth of the task, 0 for root tasks
quiet bool
writer io.Writer
notes []string
startTime time.Time
endTime time.Time
flushed bool
}
// Duration returns the duration of the task.
//
// If the task is still running, it returns the task duration so far.
func (task *Task) Duration() time.Duration {
if task.endTime.IsZero() {
return time.Since(task.startTime)
}
return task.endTime.Sub(task.startTime)
}
// AddNote adds a note to the task that will be reported at its completion.
//
// If the note is empty it will not be added.
func (task *Task) AddNote(note string) {
if note == "" {
return
}
task.notes = append(task.notes, note)
}
// AddNoteWithLabel adds a note to the task that will be reported at its
// completion. The note will be prefixed with the given label.
//
// If the note is empty it will not be added.
func (task *Task) AddNoteWithLabel(label, note string) {
if note == "" {
return
}
if label != "" {
note = label + ": " + note
}
task.notes = append(task.notes, note)
}
// Logf prints the given format and values to stdout with an indendation
// level matching the task's nesting depth.
func (task *Task) Logf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
task.flush()
task.log(indent(task.depth + 1))
task.log(s)
}
// logf prints the given string format and values to the log.
func (task *Task) logf(depth int, format string, a ...any) {
if depth > 0 {
task.log(indent(depth))
}
task.log(fmt.Sprintf(format, a...))
}
// log writes the given string to the log.
func (task *Task) log(s string) {
if task.writer != nil {
task.writer.Write([]byte(s))
} else {
fmt.Print(s)
}
}
// start marks the task start time and prints its description if the
// task is not quiet.
func (task *Task) start() {
if !task.quiet {
if task.parent != nil {
task.parent.flush()
}
task.logf(task.depth, "%s...", task.description)
}
task.startTime = time.Now()
}
// start marks the task end time and prints its result if the
// task is not quiet or if an error was encountered.
func (task *Task) end(taskErr error) {
task.endTime = time.Now()
notes := []string{task.Duration().Round(time.Millisecond).String()}
notes = append(notes, task.notes...)
suffix := "(" + strings.Join(notes, ", ") + ")"
if taskErr != nil {
if task.flushed {
task.logf(task.depth, "%s... failed. %s\n", task.description, suffix)
return
}
if task.quiet {
if task.parent != nil {
task.parent.flush()
}
task.logf(task.depth, "%s... failed. %s\n", task.description, suffix)
return
}
task.logf(0, " failed. %s\n", suffix)
} else {
if task.flushed {
task.logf(task.depth, "%s... done. %s\n", task.description, suffix)
return
}
if !task.quiet {
task.logf(0, " done. %s\n", suffix)
}
}
}
// flush causes the task to flush any pending text to the log ahead of an
// unrelated write.
func (task *Task) flush() {
if task.flushed {
return
}
if task.parent != nil {
task.parent.flush()
}
if task.quiet {
task.logf(task.depth, "%s...\n", task.description)
} else {
task.log("\n")
}
task.flushed = true
}
// indent returns indentation for the given task depth.
func indent(depth int) string {
return strings.Repeat(" ", depth*2)
}