Codebase list golang-github-vbauerster-mpb / 45f888c
refactoring: (*bState).draw(decor.Statistics) Merge draw and drawImpl into one func. Simplify (s *bState).trimSpace handling. Vladimir Bauer 2 years ago
2 changed file(s) with 49 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
4343 rmOnComplete bool
4444 noPop bool
4545 autoRefresh bool
46 aDecorators []decor.Decorator
47 pDecorators []decor.Decorator
46 buffers [3]*bytes.Buffer
47 decorators [2][]decor.Decorator
4848 averageDecorators []decor.AverageDecorator
4949 ewmaDecorators []decor.EwmaDecorator
5050 shutdownListeners []decor.ShutdownListener
51 buffers [3]*bytes.Buffer
5251 filler BarFiller
5352 extender extenderFunc
5453 renderReq chan<- time.Time
158157 iter := make(chan decor.Decorator)
159158 select {
160159 case b.operateState <- func(s *bState) {
161 for _, decorators := range [][]decor.Decorator{
162 s.pDecorators,
163 s.aDecorators,
164 } {
160 for _, decorators := range s.decorators {
165161 for _, d := range decorators {
166162 iter <- d
167163 }
483479 }
484480 }
485481
486 func (s *bState) draw(stat decor.Statistics) (io.Reader, error) {
487 r, err := s.drawImpl(stat)
488 if err != nil {
489 for _, b := range s.buffers {
490 b.Reset()
491 }
492 return nil, err
493 }
494 return io.MultiReader(r, strings.NewReader("\n")), nil
495 }
496
497 func (s *bState) drawImpl(stat decor.Statistics) (io.Reader, error) {
482 func (s *bState) draw(stat decor.Statistics) (_ io.Reader, err error) {
483 defer func() {
484 if err != nil {
485 for _, b := range s.buffers {
486 b.Reset()
487 }
488 }
489 }()
498490 decorFiller := func(buf *bytes.Buffer, decorators []decor.Decorator) (err error) {
499491 for _, d := range decorators {
500492 // need to call Decor in any case becase of width synchronization
514506 return err
515507 }
516508
517 bufP, bufB, bufA := s.buffers[0], s.buffers[1], s.buffers[2]
518
519 err := firstNonNil(decorFiller(bufP, s.pDecorators), decorFiller(bufA, s.aDecorators))
509 for i, buf := range s.buffers[:2] {
510 err = decorFiller(buf, s.decorators[i])
511 if err != nil {
512 return nil, err
513 }
514 }
515
516 spaces := []io.Reader{
517 strings.NewReader(" "),
518 strings.NewReader(" "),
519 }
520 if s.trimSpace || stat.AvailableWidth < 2 {
521 for _, r := range spaces {
522 _, _ = io.Copy(io.Discard, r)
523 }
524 } else {
525 stat.AvailableWidth -= 2
526 }
527
528 err = s.filler.Fill(s.buffers[2], stat)
520529 if err != nil {
521530 return nil, err
522531 }
523532
524 if !s.trimSpace && stat.AvailableWidth >= 2 {
525 stat.AvailableWidth -= 2
526 writeFiller := func(buf *bytes.Buffer) error {
527 return s.filler.Fill(buf, stat)
528 }
529 for _, fn := range []func(*bytes.Buffer) error{
530 writeSpace,
531 writeFiller,
532 writeSpace,
533 } {
534 if err := fn(bufB); err != nil {
535 return nil, err
536 }
537 }
538 } else {
539 err := s.filler.Fill(bufB, stat)
540 if err != nil {
541 return nil, err
542 }
543 }
544
545 return io.MultiReader(bufP, bufB, bufA), nil
533 return io.MultiReader(
534 s.buffers[0],
535 spaces[0],
536 s.buffers[2],
537 spaces[1],
538 s.buffers[1],
539 strings.NewReader("\n"),
540 ), nil
546541 }
547542
548543 func (s *bState) wSyncTable() (table syncTable) {
549544 var count int
550545 var row []chan int
551546
552 for i, decorators := range [][]decor.Decorator{
553 s.pDecorators,
554 s.aDecorators,
555 } {
547 for i, decorators := range s.decorators {
556548 for _, d := range decorators {
557549 if ch, ok := d.Sync(); ok {
558550 row = append(row, ch)
643635 func writeSpace(buf *bytes.Buffer) error {
644636 return buf.WriteByte(' ')
645637 }
646
647 func firstNonNil(errors ...error) error {
648 for _, err := range errors {
649 if err != nil {
650 return err
651 }
652 }
653 return nil
654 }
1919 return
2020 }
2121
22 // PrependDecorators let you inject decorators to the bar's left side.
23 func PrependDecorators(decorators ...decor.Decorator) BarOption {
24 decorators = inspect(decorators)
25 return func(s *bState) {
26 s.decorators[0] = decorators
27 }
28 }
29
2230 // AppendDecorators let you inject decorators to the bar's right side.
2331 func AppendDecorators(decorators ...decor.Decorator) BarOption {
2432 decorators = inspect(decorators)
2533 return func(s *bState) {
26 s.aDecorators = decorators
27 }
28 }
29
30 // PrependDecorators let you inject decorators to the bar's left side.
31 func PrependDecorators(decorators ...decor.Decorator) BarOption {
32 decorators = inspect(decorators)
33 return func(s *bState) {
34 s.pDecorators = decorators
34 s.decorators[1] = decorators
3535 }
3636 }
3737