-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpbar.go
179 lines (143 loc) · 2.83 KB
/
pbar.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
// Copyright © 2022 Atonal Authors
//
package progressbar
import (
"io"
"sync"
"time"
)
func defaultBytes(mpbar MultiPB, maxBytes int64, title string, opts ...Opt) PB {
pb := &pbar{
mpbar: mpbar,
max: maxBytes,
title: title,
stepper: steppers[0].init(),
startTime: time.Now(),
// sigRedraw: make(chan struct{}),
// sigExit: make(chan struct{}),
}
for _, opt := range opts {
opt(pb)
}
go pb.run()
return pb
}
type PB interface {
io.Writer
Close()
String() string
UpdateRange(min, max int64)
LowerBound() int64
UpperBound() int64
Step(delta int64)
}
type (
Worker func(bar PB, exitCh <-chan struct{})
OnCompleted func(bar PB)
OnStart func(bar PB)
OnDataPrepared func(bar PB, data *SchemaData)
)
type pbar struct {
stopTime time.Time
startTime time.Time
stepper BarT // stepper or spinner here
mpbar MultiPB //
stepperPostInit func(bar BarT) //
worker Worker
onComp OnCompleted
onStart OnStart
onDataPrepared OnDataPrepared
title string
read int64
min int64
max int64
row int
muPainting sync.Mutex
completed bool
}
func (pb *pbar) Close() {
pb.muPainting.Lock()
defer pb.muPainting.Unlock()
// if atomic.CompareAndSwapInt32(&pb.closed, 0, 1) {
// close(pb.sigExit)
// close(pb.sigRedraw)
// }
}
func (pb *pbar) LowerBound() int64 { return pb.min }
func (pb *pbar) UpperBound() int64 { return pb.max }
func (pb *pbar) UpdateRange(min, max int64) {
pb.muPainting.Lock()
defer pb.muPainting.Unlock()
pb.min, pb.max = min, max
}
func (pb *pbar) Step(delta int64) {
pb.muPainting.Lock()
defer pb.muPainting.Unlock()
pb.read += delta
pb.invalidate()
}
func (pb *pbar) Write(data []byte) (n int, err error) {
pb.muPainting.Lock()
defer pb.muPainting.Unlock()
n = len(data)
pb.read += int64(n)
pb.invalidate()
return
}
func (pb *pbar) invalidate() {
if pb.read >= pb.max {
pb.completed = true
if pb.onComp != nil {
cb := pb.onComp
pb.onComp = nil
cb(pb)
}
}
pb.redraw()
}
func (pb *pbar) redraw() {
pb.mpbar.Redraw()
}
func (pb *pbar) run() {
if pb.onStart != nil {
pb.onStart(pb)
}
if pb.stepperPostInit != nil {
pb.stepperPostInit(pb.stepper)
}
if pb.worker != nil {
go func() {
pb.worker(pb, pb.mpbar.SignalExit())
}()
}
}
// func (pb *pbar) run() {
// if pb.worker != nil {
// go func() {
// pb.worker(pb.sigExit)
// }()
// }
//
// for {
// select {
// case <-pb.sigRedraw:
// if pb.spinner != nil {
// pb.spinner.draw(pb)
// } else {
// pb.stepper.draw(pb)
// }
// case <-pb.sigExit:
// return
// }
// }
// }
func (pb *pbar) Bytes() []byte {
return pb.stepper.Bytes(pb)
}
func (pb *pbar) String() string {
return pb.stepper.String(pb)
}
func (pb *pbar) locker() func() {
pb.muPainting.Lock()
return pb.muPainting.Unlock
}