-
Notifications
You must be signed in to change notification settings - Fork 6
/
output.go
168 lines (138 loc) · 3.72 KB
/
output.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
package output
import (
"fmt"
"log"
"sync"
"sync/atomic"
"time"
"github.com/efritz/pentimento"
"github.com/sourcegraph/scip-go/internal/parallel"
)
type Verbosity int
const (
NoOutput Verbosity = iota
DefaultOutput
VerboseOutput
VeryVerboseOutput
VeryVeryVerboseOutput
)
type Options struct {
Verbosity Verbosity
ShowAnimations bool
}
var opts Options = Options{
Verbosity: DefaultOutput,
ShowAnimations: false,
}
// updateInterval is the duration between updates in withProgress.
var updateInterval = time.Second / 4
// ticker is the animated throbber used in printProgress.
var ticker = pentimento.NewAnimatedString([]string{
"⠸", "⠼",
"⠴", "⠦",
"⠧", "⠇",
"⠏", "⠋",
"⠙", "⠹",
}, updateInterval)
// var failurePrefix = "✗"
var successPrefix = "✔"
// WithProgress prints a spinner while the given function is active.
func WithProgress(name string, fn func() error) error {
ch := make(chan func() error, 1)
ch <- fn
close(ch)
wg, errCh, count := parallel.Run(ch)
WithProgressParallel(wg, name, count, 1)
// Handle any associated errors
select {
case err := <-errCh:
return err
default:
return nil
}
}
// WithProgressParallel will continuously print progress to stdout until the given wait group
// counter goes to zero. Progress is determined by the values of `c` (number of tasks completed)
// and the value `n` (total number of tasks).
func WithProgressParallel(wg *sync.WaitGroup, name string, c *uint64, n uint64) {
sync := make(chan struct{})
go func() {
wg.Wait()
close(sync)
}()
withTitle(name, func(printer *pentimento.Printer) {
for {
select {
case <-sync:
return
case <-time.After(updateInterval):
}
printProgress(printer, name, c, n)
}
})
}
// withTitle invokes withTitleAnimated withTitleStatic depending on the value of animated.
func withTitle(name string, fn func(printer *pentimento.Printer)) {
if opts.Verbosity == NoOutput {
fn(nil)
} else if !opts.ShowAnimations || opts.Verbosity >= VeryVerboseOutput {
withTitleStatic(name, opts.Verbosity, fn)
} else {
withTitleAnimated(name, opts.Verbosity, fn)
}
}
// withTitleStatic invokes the given function with non-animated output.
func withTitleStatic(name string, verbosity Verbosity, fn func(printer *pentimento.Printer)) {
start := time.Now()
fmt.Printf("%s\n", name)
fn(nil)
if verbosity > DefaultOutput {
fmt.Printf("Finished in %s.\n\n", HumanElapsed(start))
}
}
// withTitleStatic invokes the given function with animated output.
func withTitleAnimated(name string, verbosity Verbosity, fn func(printer *pentimento.Printer)) {
start := time.Now()
fmt.Printf("%s %s... ", ticker, name)
_ = pentimento.PrintProgress(func(printer *pentimento.Printer) error {
defer func() {
_ = printer.Reset()
}()
fn(printer)
return nil
})
fmt.Printf("%s %s... Done (%s)\n", successPrefix, name, HumanElapsed(start))
}
// printProgress outputs a throbber, the given name, and the given number of tasks completed to
// the given printer.
func printProgress(printer *pentimento.Printer, name string, c *uint64, n uint64) {
if printer == nil {
return
}
content := pentimento.NewContent()
if c == nil {
content.AddLine("%s %s...", ticker, name)
} else {
content.AddLine("%s %s... %d/%d\n", ticker, name, atomic.LoadUint64(c), n)
}
printer.WriteContent(content)
}
func SetOutputOptions(verb Verbosity, animation bool) {
opts.Verbosity = verb
opts.ShowAnimations = animation
}
func Println(a ...any) {
if opts.Verbosity != NoOutput {
fmt.Println(a...)
}
}
func Printf(format string, a ...any) {
if opts.Verbosity >= VerboseOutput {
log.Printf(format, a...)
}
}
func Logf(format string, a ...any) {
if opts.Verbosity >= VeryVeryVerboseOutput {
log.Printf(format, a...)
}
}