Skip to content

Commit 6013267

Browse files
committed
Refactoring
1 parent 7614768 commit 6013267

3 files changed

Lines changed: 40 additions & 51 deletions

File tree

bar.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Bar struct {
2929
trimLeftCh chan bool
3030
trimRightCh chan bool
3131
refillCh chan *refill
32-
decoratorCh chan *decorator
32+
dCommandCh chan *dCommandData
3333
flushedCh chan struct{}
3434
removeReqCh chan struct{}
3535
completeReqCh chan struct{}
@@ -89,7 +89,7 @@ func newBar(id int, total int64, width int, format string, wg *sync.WaitGroup, c
8989
trimLeftCh: make(chan bool),
9090
trimRightCh: make(chan bool),
9191
refillCh: make(chan *refill),
92-
decoratorCh: make(chan *decorator),
92+
dCommandCh: make(chan *dCommandData),
9393
flushedCh: make(chan struct{}, 1),
9494
removeReqCh: make(chan struct{}),
9595
completeReqCh: make(chan struct{}),
@@ -212,7 +212,7 @@ func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
212212
if isClosed(b.done) {
213213
return b
214214
}
215-
b.decoratorCh <- &decorator{decPrepend, f}
215+
b.dCommandCh <- &dCommandData{dPrepend, f}
216216
return b
217217
}
218218

@@ -221,15 +221,15 @@ func (b *Bar) RemoveAllPrependers() {
221221
if isClosed(b.done) {
222222
return
223223
}
224-
b.decoratorCh <- &decorator{decPrependZero, nil}
224+
b.dCommandCh <- &dCommandData{dPrependZero, nil}
225225
}
226226

227227
// AppendFunc appends DecoratorFunc
228228
func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
229229
if isClosed(b.done) {
230230
return b
231231
}
232-
b.decoratorCh <- &decorator{decAppend, f}
232+
b.dCommandCh <- &dCommandData{dAppend, f}
233233
return b
234234
}
235235

@@ -238,7 +238,7 @@ func (b *Bar) RemoveAllAppenders() {
238238
if isClosed(b.done) {
239239
return
240240
}
241-
b.decoratorCh <- &decorator{decAppendZero, nil}
241+
b.dCommandCh <- &dCommandData{dAppendZero, nil}
242242
}
243243

244244
// Completed signals to the bar, that process has been completed.
@@ -298,15 +298,15 @@ func (b *Bar) server(id int, total int64, width int, format string, wg *sync.Wai
298298
}
299299
barState.current = n
300300
prevStartTime = blockStartTime
301-
case d := <-b.decoratorCh:
302-
switch d.kind {
303-
case decAppend:
304-
barState.appendFuncs = append(barState.appendFuncs, d.f)
305-
case decAppendZero:
301+
case data := <-b.dCommandCh:
302+
switch data.action {
303+
case dAppend:
304+
barState.appendFuncs = append(barState.appendFuncs, data.f)
305+
case dAppendZero:
306306
barState.appendFuncs = nil
307-
case decPrepend:
308-
barState.prependFuncs = append(barState.prependFuncs, d.f)
309-
case decPrependZero:
307+
case dPrepend:
308+
barState.prependFuncs = append(barState.prependFuncs, data.f)
309+
case dPrependZero:
310310
barState.prependFuncs = nil
311311
}
312312
case ch := <-b.stateReqCh:

decorators.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ const (
2121
DextraSpace
2222
)
2323

24-
type decoratorOperation uint
24+
type decoratorAction uint
2525

2626
const (
27-
decAppend decoratorOperation = iota
28-
decPrepend
29-
decAppendZero
30-
decPrependZero
27+
dAppend decoratorAction = iota
28+
dPrepend
29+
dAppendZero
30+
dPrependZero
3131
)
3232

3333
// DecoratorFunc is a function that can be prepended and appended to the progress bar
3434
type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
3535

36-
type decorator struct {
37-
kind decoratorOperation
38-
f DecoratorFunc
36+
type dCommandData struct {
37+
action decoratorAction
38+
f DecoratorFunc
3939
}
4040

4141
// PrependName prepends name argument to the bar.

progress.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,23 @@ var ErrCallAfterStop = errors.New("method call on stopped Progress instance")
2020
type (
2121
// BeforeRender is a func, which gets called before render process
2222
BeforeRender func([]*Bar)
23-
barOpType uint
23+
barAction uint
2424

25-
operation struct {
26-
kind barOpType
25+
bCommandData struct {
26+
action barAction
2727
bar *Bar
2828
result chan bool
2929
}
3030

31-
indexedBarBuffer struct {
32-
index int
33-
buf []byte
34-
}
35-
36-
indexedBar struct {
37-
index int
38-
termWidth int
39-
bar *Bar
40-
}
41-
4231
widthSync struct {
4332
listen []chan int
4433
result []chan int
4534
}
4635
)
4736

4837
const (
49-
barAdd barOpType = iota
50-
barRemove
38+
bAdd barAction = iota
39+
bRemove
5140
)
5241

5342
const (
@@ -70,7 +59,7 @@ type Progress struct {
7059
width int
7160
format string
7261

73-
operationCh chan *operation
62+
bCommandCh chan *bCommandData
7463
rrChangeReqCh chan time.Duration
7564
outChangeReqCh chan io.Writer
7665
barCountReqCh chan chan int
@@ -85,7 +74,7 @@ type Progress struct {
8574
func New() *Progress {
8675
p := &Progress{
8776
width: pwidth,
88-
operationCh: make(chan *operation),
77+
bCommandCh: make(chan *bCommandData),
8978
rrChangeReqCh: make(chan time.Duration),
9079
outChangeReqCh: make(chan io.Writer),
9180
barCountReqCh: make(chan chan int),
@@ -165,7 +154,7 @@ func (p *Progress) AddBarWithID(id int, total int64) *Bar {
165154
}
166155
result := make(chan bool)
167156
bar := newBar(id, total, p.width, p.format, p.wg, p.cancel)
168-
p.operationCh <- &operation{barAdd, bar, result}
157+
p.bCommandCh <- &bCommandData{bAdd, bar, result}
169158
if <-result {
170159
p.wg.Add(1)
171160
}
@@ -179,7 +168,7 @@ func (p *Progress) RemoveBar(b *Bar) bool {
179168
panic(ErrCallAfterStop)
180169
}
181170
result := make(chan bool)
182-
p.operationCh <- &operation{barRemove, b, result}
171+
p.bCommandCh <- &bCommandData{bRemove, b, result}
183172
return <-result
184173
}
185174

@@ -213,7 +202,7 @@ func (p *Progress) Stop() {
213202
if isClosed(p.done) {
214203
return
215204
}
216-
close(p.operationCh)
205+
close(p.bCommandCh)
217206
}
218207

219208
// server monitors underlying channels and renders any progress bars
@@ -245,25 +234,25 @@ func (p *Progress) server() {
245234
case w := <-p.outChangeReqCh:
246235
cw.Flush()
247236
cw = cwriter.New(w)
248-
case op, ok := <-p.operationCh:
237+
case data, ok := <-p.bCommandCh:
249238
if !ok {
250239
return
251240
}
252-
switch op.kind {
253-
case barAdd:
254-
bars = append(bars, op.bar)
255-
op.result <- true
256-
case barRemove:
241+
switch data.action {
242+
case bAdd:
243+
bars = append(bars, data.bar)
244+
data.result <- true
245+
case bRemove:
257246
var ok bool
258247
for i, b := range bars {
259-
if b == op.bar {
248+
if b == data.bar {
260249
bars = append(bars[:i], bars[i+1:]...)
261250
ok = true
262251
b.remove()
263252
break
264253
}
265254
}
266-
op.result <- ok
255+
data.result <- ok
267256
}
268257
case respCh := <-p.barCountReqCh:
269258
respCh <- len(bars)

0 commit comments

Comments
 (0)