make rows once
Vladimir Bauer
3 years ago
| 38 | 38 | heapUpdated bool |
| 39 | 39 | pMatrix map[int][]chan int |
| 40 | 40 | aMatrix map[int][]chan int |
| 41 | rows []io.Reader | |
| 41 | 42 | |
| 42 | 43 | // following are provided/overrided by user |
| 43 | 44 | refreshRate time.Duration |
| 67 | 68 | // method has been called. |
| 68 | 69 | func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress { |
| 69 | 70 | s := &pState{ |
| 71 | rows: make([]io.Reader, 32), | |
| 70 | 72 | refreshRate: defaultRefreshRate, |
| 71 | 73 | popPriority: math.MinInt32, |
| 72 | 74 | manualRefresh: make(chan interface{}), |
| 326 | 328 | |
| 327 | 329 | func (s *pState) flush(wg *sync.WaitGroup, cw *cwriter.Writer, height int) error { |
| 328 | 330 | var popCount int |
| 329 | rows := make([]io.Reader, 0, height) | |
| 330 | 331 | pool := make([]*Bar, 0, s.bHeap.Len()) |
| 332 | s.rows = s.rows[:0] | |
| 331 | 333 | |
| 332 | 334 | for s.bHeap.Len() > 0 { |
| 333 | 335 | b := heap.Pop(&s.bHeap).(*Bar) |
| 338 | 340 | } |
| 339 | 341 | var usedRows int |
| 340 | 342 | for i := len(frame.rows) - 1; i >= 0; i-- { |
| 341 | if row := frame.rows[i]; len(rows) < height { | |
| 342 | rows = append(rows, row) | |
| 343 | if row := frame.rows[i]; len(s.rows) < height { | |
| 344 | s.rows = append(s.rows, row) | |
| 343 | 345 | usedRows++ |
| 344 | 346 | } else { |
| 345 | 347 | wg.Add(1) |
| 388 | 390 | }() |
| 389 | 391 | } |
| 390 | 392 | |
| 391 | for i := len(rows) - 1; i >= 0; i-- { | |
| 392 | _, err := cw.ReadFrom(rows[i]) | |
| 393 | for i := len(s.rows) - 1; i >= 0; i-- { | |
| 394 | _, err := cw.ReadFrom(s.rows[i]) | |
| 393 | 395 | if err != nil { |
| 394 | 396 | return err |
| 395 | 397 | } |
| 396 | 398 | } |
| 397 | 399 | |
| 398 | err := cw.Flush(len(rows) - popCount) | |
| 400 | err := cw.Flush(len(s.rows) - popCount) | |
| 399 | 401 | return err |
| 400 | 402 | } |
| 401 | 403 | |