github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/progressbar/progress_bar.go (about)

     1  package progressbar
     2  
     3  import (
     4  	"io"
     5  	"time"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  	pb "gopkg.in/cheggaaa/pb.v1"
     9  )
    10  
    11  type ProgressBar struct {
    12  	ready chan bool
    13  	bar   *pb.ProgressBar
    14  }
    15  
    16  func NewProgressBar() *ProgressBar {
    17  	return &ProgressBar{
    18  		ready: make(chan bool),
    19  	}
    20  }
    21  
    22  func (p *ProgressBar) Complete() {
    23  	// Adding sleep to ensure UI has finished drawing
    24  	time.Sleep(time.Second)
    25  	p.bar.Finish()
    26  }
    27  
    28  func (p *ProgressBar) NewProgressBarWrapper(reader io.Reader, sizeOfFile int64) io.Reader {
    29  	log.WithField("file_size", sizeOfFile).Debug("new progress bar")
    30  
    31  	ready, ok := <-p.ready
    32  	if !ready || !ok {
    33  		return nil
    34  	}
    35  
    36  	log.Debug("progress bar ready")
    37  	p.bar = pb.New(int(sizeOfFile)).SetUnits(pb.U_BYTES)
    38  	p.bar.ShowTimeLeft = false
    39  	p.bar.Start()
    40  	return p.bar.NewProxyReader(reader)
    41  }
    42  
    43  func (p *ProgressBar) Ready() {
    44  	p.ready <- true
    45  }