-
Notifications
You must be signed in to change notification settings - Fork 5
/
printer.go
143 lines (125 loc) · 2.84 KB
/
printer.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
package knit
import (
"fmt"
"io"
"path/filepath"
"sync"
pb "github.com/schollz/progressbar/v3"
)
type BasicPrinter struct {
w io.Writer
lock sync.Mutex
}
func (p *BasicPrinter) SetSteps(int) {}
func (p *BasicPrinter) Update() {}
func (p *BasicPrinter) Clear() {}
func (p *BasicPrinter) Done(string) {}
func (p *BasicPrinter) NeedsUpdate() bool { return false }
func (p *BasicPrinter) Print(cmd, dir string, name string, step int) {
p.lock.Lock()
defer p.lock.Unlock()
if dir != "." {
fmt.Fprintf(p.w, "[%s] ", dir)
}
fmt.Fprintln(p.w, cmd)
}
type StepPrinter struct {
w io.Writer
lock sync.Mutex
steps int
last int
}
func (p *StepPrinter) SetSteps(steps int) {
p.steps = steps
}
func (p *StepPrinter) Update() {}
func (p *StepPrinter) Clear() {}
func (p *StepPrinter) Done(string) {}
func (p *StepPrinter) NeedsUpdate() bool { return false }
func (p *StepPrinter) Print(cmd, dir string, name string, step int) {
p.lock.Lock()
defer p.lock.Unlock()
if step <= p.last {
return
}
fmt.Fprintf(p.w, "[%d/%d] ", step, p.steps)
if dir != "." {
fmt.Fprintf(p.w, "[%s] ", dir)
}
fmt.Fprintln(p.w, name)
p.last = step
}
type ProgressPrinter struct {
w io.Writer
lock sync.Mutex
bar *pb.ProgressBar
tasks map[string]string
showCommands bool
}
func (p *ProgressPrinter) SetSteps(steps int) {
p.bar = pb.NewOptions64(int64(steps),
pb.OptionSetWriter(p.w),
pb.OptionSetWidth(10),
pb.OptionShowCount(),
pb.OptionSpinnerType(14),
pb.OptionFullWidth(),
pb.OptionSetPredictTime(false),
pb.OptionSetDescription("Building"),
pb.OptionOnCompletion(func() {
fmt.Fprint(p.w, "\n")
}),
pb.OptionSetTheme(pb.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
}
func (p *ProgressPrinter) NeedsUpdate() bool { return true }
func (p *ProgressPrinter) Update() {
p.lock.Lock()
defer p.lock.Unlock()
p.bar.RenderBlank()
fmt.Fprint(p.w, "\r")
}
func (p *ProgressPrinter) Clear() {
p.lock.Lock()
defer p.lock.Unlock()
p.bar.Clear()
fmt.Fprint(p.w, "\r")
}
func (p *ProgressPrinter) Print(cmd, dir string, name string, step int) {
p.lock.Lock()
defer p.lock.Unlock()
p.tasks[name] = cmd
if p.showCommands {
p.bar.Clear()
fmt.Fprint(p.w, "\r")
if dir != "." {
fmt.Fprintf(p.w, "[%s] ", dir)
}
fmt.Fprintln(p.w, cmd)
}
p.bar.Describe(p.desc())
p.bar.RenderBlank()
}
func (p *ProgressPrinter) desc() string {
desc := "Building"
for k := range p.tasks {
desc += " " + fmt.Sprintf("%-40s", filepath.Base(k))
break
}
return desc
}
func (p *ProgressPrinter) Done(name string) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.tasks, name)
if len(p.tasks) == 0 {
p.bar.Describe("Built " + name)
} else {
p.bar.Describe(p.desc())
}
p.bar.Add(1)
}