-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
244 lines (204 loc) · 5.65 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Package dspc provides tools for tracking progress of concurrent operations in a terminal.
package dspc
import (
"fmt"
"io"
"iter"
"maps"
"os"
"sync/atomic"
"time"
)
// Progress tracks multiple named counters. It's similar to a concurrent map[string]int64
// but optimized for progress tracking for a small stable sets of keys (typically
// fitting on a single screen).
//
// All operations are atomic, lock-free and safe for concurrent use.
// Methods do not allocate memory in the hot path.
//
// The zero Progress is empty and ready for use
type Progress struct {
state atomic.Pointer[progressState]
// for printing w/o allocations
buf betterBuffer
entries []entry
}
type progressState struct {
counters map[string]*int64
sortedKeys []string
}
type entry struct {
key string
value int64
}
// Inc atomically adds delta to the counter associated with the given key.
// If the key doesn't exist, it's created with an initial value of 0 before adding delta.
func (p *Progress) Inc(key string, delta int64) {
c := p.getOrCreateCounter(key)
atomic.AddInt64(c, delta)
}
// Set atomically sets the counter associated with the given key to value.
// If the key doesn't exist, it's created with the specified value.
func (p *Progress) Set(key string, value int64) {
c := p.getOrCreateCounter(key)
atomic.StoreInt64(c, value)
}
// Get returns the current value of the counter associated with key.
// Returns 0 if the key doesn't exist.
func (p *Progress) Get(key string) int64 {
state := p.state.Load()
if state == nil {
return 0
}
counter := state.counters[key]
if counter == nil {
return 0
}
return atomic.LoadInt64(counter)
}
// All returns an iterator over all counters in lexicographical key order.
// The iterator yields (key, value) pairs. The values represent atomic snapshots
// of the counters at the time they are read.
func (p *Progress) All() iter.Seq2[string, int64] {
return func(yield func(string, int64) bool) {
state := p.state.Load()
if state == nil {
return
}
for _, key := range state.sortedKeys {
if !yield(key, atomic.LoadInt64(state.counters[key])) {
return
}
}
}
}
func (p *Progress) size() int {
state := p.state.Load()
if state == nil {
return 0
}
return len(state.counters)
}
func (p *Progress) getOrCreateCounter(key string) *int64 {
for {
state := p.state.Load()
// happy path: map contains the key
if state != nil {
if counter := state.counters[key]; counter != nil {
return counter
}
}
// Unhappy path: need to clone the state and add new key to it with CAS
newCounter := new(int64)
newState := &progressState{}
if state != nil {
newState.counters = make(map[string]*int64, len(state.counters)+1)
maps.Copy(newState.counters, state.counters)
newState.counters[key] = newCounter
newState.sortedKeys = cloneSortedSliceAndInsert(state.sortedKeys, key)
} else {
newState.counters = map[string]*int64{key: newCounter}
newState.sortedKeys = []string{key}
}
if p.state.CompareAndSwap(state, newState) {
return newCounter
}
}
}
func (p *Progress) prettyPrint(w io.Writer, title string, inPlace bool) error {
p.buf.Reset()
p.entries = p.entries[:0]
maxKeySize := 0
maxValueSize := 0
for key, value := range p.All() {
p.entries = append(p.entries, entry{key, value})
maxKeySize = max(maxKeySize, len(key))
maxValueSize = max(maxValueSize, digitCount(value))
}
// clear the screen after the cursor
p.buf.WriteString("\033[J")
// Start with a blank line
p.buf.WriteString("\n")
// Print the title
p.buf.WriteString(title)
p.buf.WriteString("\n")
// Print the progress
for _, ent := range p.entries {
p.buf.WriteString(" ")
p.buf.WriteString(ent.key)
p.buf.WriteByteRepeated(' ', maxKeySize-len(ent.key))
p.buf.WriteString(" ")
p.buf.WriteByteRepeated(' ', maxValueSize-digitCount(ent.value))
p.buf.WriteInt64(ent.value)
p.buf.WriteString("\n")
}
// End with a blank line
p.buf.WriteString("\n")
if inPlace {
// Move the cursor up to the start of the progress.
// Works more reliably that doing save/restore of the cursor position.
p.buf.WriteString("\033[")
p.buf.WriteInt(len(p.entries) + 3)
p.buf.WriteString("A")
}
// Flush the buffer in a single Write call
_, err := w.Write(p.buf.Bytes())
return err
}
// PrettyPrintEvery periodically prints the current state of Progress to w (typically stdout ot stderr).
// It updates the output in-place and won't damage the log output of the application
// (assuming logs are printed line by line).
// PrettyPrintEvery returns the function that stops the printing when called.
//
// Usage:
//
// stop := progress.PrettyPrintEvery(os.Stdout, time.Second, "Progress:")
// defer stop()
//
// Or better:
//
// defer progress.PrettyPrintEvery(os.Stdout, time.Second, "Progress:")()
//
// Example output:
//
// Progress:
// completed 15
// failed 3
// skipped 7
func (p *Progress) PrettyPrintEvery(w io.Writer, t time.Duration, title string) func() {
stop := make(chan struct{})
done := make(chan struct{})
printError := func(err error) {
// Should never happen, especially when writing to stdout/stderr
fmt.Fprintln(os.Stderr, "Error writing progress:", err)
}
go func() {
defer close(done)
ticker := time.NewTicker(t)
defer ticker.Stop()
if err := p.prettyPrint(w, title, true); err != nil {
printError(err)
return
}
for {
select {
case <-ticker.C:
if err := p.prettyPrint(w, title, true); err != nil {
printError(err)
return
}
case <-stop:
// w/o ansi
if err := p.prettyPrint(w, title, false); err != nil {
printError(err)
}
return
}
}
}()
stopPrinting := func() {
close(stop)
<-done
}
return stopPrinting
}