Don't panic when bars have not equal decorators
Vladimir Bauer
8 years ago
| 37 | 37 |
bar.priority = priority
|
| 38 | 38 |
heap.Fix(pq, bar.index)
|
| 39 | 39 |
}
|
|
40 |
|
|
41 |
func (pq priorityQueue) maxNumP() int {
|
|
42 |
if pq.Len() == 0 {
|
|
43 |
return 0
|
|
44 |
}
|
|
45 |
|
|
46 |
max := pq[0].NumOfPrependers()
|
|
47 |
for i := 1; i < pq.Len(); i++ {
|
|
48 |
n := pq[i].NumOfPrependers()
|
|
49 |
if n > max {
|
|
50 |
max = n
|
|
51 |
}
|
|
52 |
}
|
|
53 |
return max
|
|
54 |
}
|
|
55 |
|
|
56 |
func (pq priorityQueue) maxNumA() int {
|
|
57 |
if pq.Len() == 0 {
|
|
58 |
return 0
|
|
59 |
}
|
|
60 |
|
|
61 |
max := pq[0].NumOfAppenders()
|
|
62 |
for i := 1; i < pq.Len(); i++ {
|
|
63 |
n := pq[i].NumOfAppenders()
|
|
64 |
if n > max {
|
|
65 |
max = n
|
|
66 |
}
|
|
67 |
}
|
|
68 |
return max
|
|
69 |
}
|
| 36 | 36 |
// progress state, which may contain several bars
|
| 37 | 37 |
pState struct {
|
| 38 | 38 |
bHeap *priorityQueue
|
|
39 |
heapUpdated bool
|
| 39 | 40 |
idCounter int
|
| 40 | 41 |
width int
|
| 41 | 42 |
format string
|
|
| 97 | 98 |
options = append(options, barWidth(s.width), barFormat(s.format))
|
| 98 | 99 |
b := newBar(s.idCounter, total, p.wg, s.cancel, options...)
|
| 99 | 100 |
heap.Push(s.bHeap, b)
|
|
101 |
s.heapUpdated = true
|
| 100 | 102 |
s.idCounter++
|
| 101 | 103 |
result <- b
|
| 102 | 104 |
}:
|
|
| 111 | 113 |
result := make(chan bool, 1)
|
| 112 | 114 |
select {
|
| 113 | 115 |
case p.operateState <- func(s *pState) {
|
| 114 | |
b.Complete()
|
| 115 | |
result <- heap.Remove(s.bHeap, b.index) != nil
|
|
116 |
if heap.Remove(s.bHeap, b.index) != nil {
|
|
117 |
s.heapUpdated = true
|
|
118 |
b.Complete()
|
|
119 |
result <- true
|
|
120 |
} else {
|
|
121 |
result <- false
|
|
122 |
}
|
| 116 | 123 |
}:
|
| 117 | 124 |
return <-result
|
| 118 | 125 |
case <-p.quit:
|
|
| 211 | 218 |
}
|
| 212 | 219 |
|
| 213 | 220 |
func (s *pState) writeAndFlush(tw, numP, numA int) (err error) {
|
| 214 | |
if numP < 0 && numA < 0 {
|
| 215 | |
return
|
| 216 | |
}
|
| 217 | |
|
| 218 | 221 |
wSyncTimeout := make(chan struct{})
|
| 219 | 222 |
time.AfterFunc(s.rr, func() {
|
| 220 | 223 |
close(wSyncTimeout)
|
| 24 | 24 |
close(p.done)
|
| 25 | 25 |
}()
|
| 26 | 26 |
|
| 27 | |
numP, numA := -1, -1
|
| 28 | |
|
|
27 |
var numP, numA int
|
| 29 | 28 |
var timer *time.Timer
|
| 30 | 29 |
var resumeTicker <-chan time.Time
|
| 31 | |
resumeDelay := 300 * time.Millisecond
|
|
30 |
resumeDelay := 320 * time.Millisecond
|
| 32 | 31 |
|
| 33 | 32 |
for {
|
| 34 | 33 |
select {
|
|
| 39 | 38 |
runtime.Gosched()
|
| 40 | 39 |
break
|
| 41 | 40 |
}
|
| 42 | |
b0 := (*s.bHeap)[0]
|
| 43 | |
if numP == -1 {
|
| 44 | |
numP = b0.NumOfPrependers()
|
| 45 | |
}
|
| 46 | |
if numA == -1 {
|
| 47 | |
numA = b0.NumOfAppenders()
|
|
41 |
if s.heapUpdated {
|
|
42 |
numP = s.bHeap.maxNumP()
|
|
43 |
numA = s.bHeap.maxNumA()
|
|
44 |
s.heapUpdated = false
|
| 48 | 45 |
}
|
| 49 | 46 |
tw, _, _ := cwriter.TermSize()
|
| 50 | 47 |
err := s.writeAndFlush(tw, numP, numA)
|
| 17 | 17 |
close(p.done)
|
| 18 | 18 |
}()
|
| 19 | 19 |
|
| 20 | |
numP, numA := -1, -1
|
|
20 |
var numP, numA int
|
| 21 | 21 |
|
| 22 | 22 |
for {
|
| 23 | 23 |
select {
|
|
| 28 | 28 |
runtime.Gosched()
|
| 29 | 29 |
break
|
| 30 | 30 |
}
|
| 31 | |
b0 := (*s.bHeap)[0]
|
| 32 | |
if numP == -1 {
|
| 33 | |
numP = b0.NumOfPrependers()
|
| 34 | |
}
|
| 35 | |
if numA == -1 {
|
| 36 | |
numA = b0.NumOfAppenders()
|
|
31 |
if s.heapUpdated {
|
|
32 |
numP = s.bHeap.maxNumP()
|
|
33 |
numA = s.bHeap.maxNumA()
|
|
34 |
s.heapUpdated = false
|
| 37 | 35 |
}
|
| 38 | 36 |
tw, _, _ := cwriter.TermSize()
|
| 39 | 37 |
err := s.writeAndFlush(tw, numP, numA)
|