Repository navigation
Expand file tree
/
Copy pathprogress.go
More file actions
124 lines (108 loc) · 3.38 KB
/
Copy pathprogress.go
File metadata and controls
124 lines (108 loc) · 3.38 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
)
// progressBarWidth is the number of cells in the rendered bar, matching the
// default width Symfony's progress bar uses.
const progressBarWidth = 28
// labelColumnWidth pads the phase label so the successive phases' bars line up
// in one column. It is the length of the widest label, "collecting".
const labelColumnWidth = 10
// maxSourceInFlight caps the bytes of source the workers hold at once. A parsed
// AST takes ~40x its source, so a few generated multi-MB vendor files parsed
// side by side would otherwise spike the heap; small files never hit the cap.
const maxSourceInFlight = 2 << 20
// stderrIsTerminal reports whether stderr is an interactive terminal. The live
// bar redraws in place with a carriage return, which only reads correctly on a
// terminal; a redirected or piped run stays plain.
var stderrIsTerminal = detectTerminal()
func detectTerminal() bool {
fileInfo, err := os.Stderr.Stat()
if err != nil {
return false
}
return fileInfo.Mode()&os.ModeCharDevice != 0
}
// renderProgressBar draws a rector-style bar keyed by phase label, e.g.
//
// collecting 1080/1659 [==============>-------------] 65%
//
// The leading carriage return rewrites the line in place on each tick.
func renderProgressBar(label string, done int, total int) string {
percent := 0
filled := 0
if total > 0 {
percent = done * 100 / total
filled = done * progressBarWidth / total
}
var bar string
if filled >= progressBarWidth {
bar = strings.Repeat("=", progressBarWidth)
} else {
bar = strings.Repeat("=", filled) + ">" + strings.Repeat("-", progressBarWidth-filled-1)
}
return fmt.Sprintf("\r%-*s %d/%d [%s] %3d%%", labelColumnWidth, label, done, total, bar, percent)
}
// progressMap reads each file and hands its source to fn on a pool of
// runtime.NumCPU() workers, drawing a live progress bar labeled label on
// stderr while it goes. Results come back in file order, so the output stays
// deterministic; on failure the error of the first failing file is returned.
// The bar is skipped on a non-terminal run so piped output stays clean.
func progressMap[T any](label string, files []string, fn func(file string, src []byte) (T, error)) ([]T, error) {
total := len(files)
results := make([]T, total)
errs := make([]error, total)
var next atomic.Int64
var mutex sync.Mutex
done := 0
// a file larger than the cap still runs, once nothing else is in flight
inFlight := 0
budget := sync.NewCond(&mutex)
var wait sync.WaitGroup
for range min(runtime.NumCPU(), total) {
wait.Go(func() {
for {
index := int(next.Add(1)) - 1
if index >= total {
return
}
src, err := os.ReadFile(files[index])
if err == nil {
mutex.Lock()
for inFlight > 0 && inFlight+len(src) > maxSourceInFlight {
budget.Wait()
}
inFlight += len(src)
mutex.Unlock()
results[index], err = fn(files[index], src)
mutex.Lock()
inFlight -= len(src)
budget.Broadcast()
mutex.Unlock()
}
errs[index] = err
if stderrIsTerminal {
mutex.Lock()
done++
fmt.Fprint(os.Stderr, renderProgressBar(label, done, total))
mutex.Unlock()
}
}
})
}
wait.Wait()
if total > 0 && stderrIsTerminal {
fmt.Fprintln(os.Stderr) // end the progress bar line
}
for _, err := range errs {
if err != nil {
return nil, err
}
}
return results, nil
}