diff --git a/bar.go b/bar.go index ea7d93d..6d66443 100644 --- a/bar.go +++ b/bar.go @@ -25,6 +25,7 @@ cancel func() } +type syncTable [2][]chan int type extenderFunc func([]io.Reader, decor.Statistics) ([]io.Reader, error) // bState is actual bar's state. @@ -470,8 +471,8 @@ } } -func (b *Bar) wSyncTable() [][]chan int { - result := make(chan [][]chan int) +func (b *Bar) wSyncTable() syncTable { + result := make(chan syncTable) select { case b.operateState <- func(s *bState) { result <- s.wSyncTable() }: return <-result @@ -559,25 +560,27 @@ return io.MultiReader(bufP, bufB, bufA), nil } -func (s *bState) wSyncTable() [][]chan int { - columns := make([]chan int, 0, len(s.pDecorators)+len(s.aDecorators)) - var pCount int - for _, d := range s.pDecorators { - if ch, ok := d.Sync(); ok { - columns = append(columns, ch) - pCount++ - } - } - var aCount int - for _, d := range s.aDecorators { - if ch, ok := d.Sync(); ok { - columns = append(columns, ch) - aCount++ - } - } - table := make([][]chan int, 2) - table[0] = columns[0:pCount] - table[1] = columns[pCount : pCount+aCount : pCount+aCount] +func (s *bState) wSyncTable() (table syncTable) { + var count int + var row []chan int + + for i, decorators := range [][]decor.Decorator{ + s.pDecorators, + s.aDecorators, + } { + for _, d := range decorators { + if ch, ok := d.Sync(); ok { + row = append(row, ch) + count++ + } + } + switch i { + case 0: + table[i] = row[0:count] + default: + table[i] = row[len(table[i-1]):count] + } + } return table } diff --git a/progress.go b/progress.go index dbf868e..c409ad3 100644 --- a/progress.go +++ b/progress.go @@ -424,13 +424,12 @@ for i := 0; i < s.bHeap.Len(); i++ { bar := s.bHeap[i] table := bar.wSyncTable() - pRow, aRow := table[0], table[1] - - for i, ch := range pRow { + + for i, ch := range table[0] { s.pMatrix[i] = append(s.pMatrix[i], ch) } - for i, ch := range aRow { + for i, ch := range table[1] { s.aMatrix[i] = append(s.aMatrix[i], ch) } }