| 38 | 38 |
pMatrix map[int][]chan int
|
| 39 | 39 |
aMatrix map[int][]chan int
|
| 40 | 40 |
barShutdownQueue []*Bar
|
|
41 |
barPopQueue []*Bar
|
| 41 | 42 |
|
| 42 | 43 |
// following are provided/overrided by user
|
| 43 | 44 |
idCount int
|
| 44 | 45 |
width int
|
|
46 |
popCompleted bool
|
| 45 | 47 |
rr time.Duration
|
| 46 | 48 |
uwg *sync.WaitGroup
|
| 47 | 49 |
manualRefreshCh <-chan time.Time
|
|
| 131 | 133 |
}
|
| 132 | 134 |
bar := newBar(p, bs)
|
| 133 | 135 |
if bs.runningBar != nil {
|
| 134 | |
if bar.priority == ps.idCount {
|
| 135 | |
bar.priority = bs.runningBar.priority
|
| 136 | |
}
|
| 137 | 136 |
ps.parkedBars[bs.runningBar] = bar
|
| 138 | 137 |
} else {
|
| 139 | 138 |
heap.Push(&ps.bHeap, bar)
|
|
| 162 | 161 |
}
|
| 163 | 162 |
}
|
| 164 | 163 |
|
| 165 | |
// UpdateBarPriority is deprecated. Please use *Bar.SetOrder.
|
|
164 |
func (p *Progress) setBarPriority(b *Bar, priority int) {
|
|
165 |
select {
|
|
166 |
case p.operateState <- func(s *pState) {
|
|
167 |
if b.index < 0 {
|
|
168 |
return
|
|
169 |
}
|
|
170 |
b.priority = priority
|
|
171 |
heap.Fix(&s.bHeap, b.index)
|
|
172 |
}:
|
|
173 |
case <-p.done:
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
// UpdateBarPriority same as *Bar.SetPriority.
|
| 166 | 178 |
func (p *Progress) UpdateBarPriority(b *Bar, priority int) {
|
| 167 | 179 |
p.setBarPriority(b, priority)
|
| 168 | |
}
|
| 169 | |
|
| 170 | |
func (p *Progress) setBarPriority(b *Bar, priority int) {
|
| 171 | |
select {
|
| 172 | |
case p.operateState <- func(s *pState) { s.bHeap.update(b, priority) }:
|
| 173 | |
case <-p.done:
|
| 174 | |
}
|
| 175 | 180 |
}
|
| 176 | 181 |
|
| 177 | 182 |
// BarCount returns bars count
|
|
| 254 | 259 |
}
|
| 255 | 260 |
|
| 256 | 261 |
func (s *pState) flush(cw *cwriter.Writer) error {
|
| 257 | |
var rpop bool
|
| 258 | 262 |
var lineCount int
|
| 259 | 263 |
hlen := s.bHeap.Len()
|
| 260 | |
tmp := make([]*Bar, hlen)
|
|
264 |
bb := make([]*Bar, hlen)
|
| 261 | 265 |
for i := 0; i < hlen; i++ {
|
| 262 | |
bar := heap.Pop(&s.bHeap).(*Bar)
|
|
266 |
b := heap.Pop(&s.bHeap).(*Bar)
|
| 263 | 267 |
defer func() {
|
| 264 | |
if bar.toShutdown {
|
|
268 |
if b.toShutdown {
|
| 265 | 269 |
// shutdown at next flush, in other words decrement underlying WaitGroup
|
| 266 | 270 |
// only after the bar with completed state has been flushed. this
|
| 267 | 271 |
// ensures no bar ends up with less than 100% rendered.
|
| 268 | |
s.barShutdownQueue = append(s.barShutdownQueue, bar)
|
| 269 | |
// if parkedBar := s.parkedBars[bar]; parkedBar != nil {
|
| 270 | |
// heap.Push(&s.bHeap, parkedBar)
|
| 271 | |
// s.heapUpdated = true
|
| 272 | |
// delete(s.parkedBars, bar)
|
| 273 | |
// }
|
| 274 | |
// if bar.toDrop {
|
| 275 | |
// s.heapUpdated = true
|
| 276 | |
// return
|
| 277 | |
// }
|
| 278 | |
// bar.priority = 0
|
|
272 |
s.barShutdownQueue = append(s.barShutdownQueue, b)
|
|
273 |
if s.popCompleted && s.parkedBars[b] == nil {
|
|
274 |
b.priority = -1
|
|
275 |
}
|
| 279 | 276 |
}
|
| 280 | 277 |
}()
|
| 281 | |
cw.ReadFrom(<-bar.frameCh)
|
| 282 | |
lineCount += bar.extendedLines + 1
|
| 283 | |
tmp[i] = bar
|
| 284 | |
}
|
| 285 | |
|
| 286 | |
for _, b := range tmp {
|
|
278 |
cw.ReadFrom(<-b.frameCh)
|
|
279 |
lineCount += b.extendedLines + 1
|
|
280 |
bb[i] = b
|
|
281 |
}
|
|
282 |
|
|
283 |
for _, b := range bb {
|
| 287 | 284 |
heap.Push(&s.bHeap, b)
|
| 288 | 285 |
}
|
| 289 | 286 |
|
| 290 | 287 |
for _, b := range s.barShutdownQueue {
|
| 291 | 288 |
if parkedBar := s.parkedBars[b]; parkedBar != nil {
|
| 292 | |
heap.Remove(&s.bHeap, b.index)
|
|
289 |
parkedBar.priority = b.priority
|
| 293 | 290 |
heap.Push(&s.bHeap, parkedBar)
|
| 294 | 291 |
delete(s.parkedBars, b)
|
| 295 | |
s.heapUpdated = true
|
| 296 | |
} else if b.toDrop {
|
|
292 |
b.toDrop = true
|
|
293 |
}
|
|
294 |
if b.toDrop {
|
| 297 | 295 |
heap.Remove(&s.bHeap, b.index)
|
| 298 | 296 |
s.heapUpdated = true
|
|
297 |
} else if s.popCompleted {
|
|
298 |
defer func() {
|
|
299 |
s.barPopQueue = append(s.barPopQueue, b)
|
|
300 |
}()
|
| 299 | 301 |
}
|
| 300 | 302 |
b.cancel()
|
| 301 | 303 |
}
|
| 302 | 304 |
s.barShutdownQueue = s.barShutdownQueue[0:0]
|
|
305 |
|
|
306 |
for _, b := range s.barPopQueue {
|
|
307 |
heap.Remove(&s.bHeap, b.index)
|
|
308 |
s.heapUpdated = true
|
|
309 |
lineCount -= b.extendedLines + 1
|
|
310 |
}
|
|
311 |
s.barPopQueue = s.barPopQueue[0:0]
|
| 303 | 312 |
|
| 304 | 313 |
return cw.Flush(lineCount)
|
| 305 | 314 |
}
|