github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/progress_bar.go (about)

     1  package v7action
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  
     8  	"gopkg.in/cheggaaa/pb.v1"
     9  )
    10  
    11  //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . SimpleProgressBar
    12  
    13  type SimpleProgressBar interface {
    14  	Initialize(path string) (io.Reader, int64, error)
    15  	Terminate()
    16  }
    17  
    18  type ProgressBar struct {
    19  	bar *pb.ProgressBar
    20  }
    21  
    22  func NewProgressBar() *ProgressBar {
    23  	return &ProgressBar{}
    24  }
    25  
    26  func (p *ProgressBar) Initialize(path string) (io.Reader, int64, error) {
    27  	file, err := os.Open(path)
    28  	if err != nil {
    29  		return nil, 0, err
    30  	}
    31  
    32  	fileInfo, err := file.Stat()
    33  	if err != nil {
    34  		return nil, 0, err
    35  	}
    36  
    37  	p.bar = pb.New(int(fileInfo.Size())).SetUnits(pb.U_BYTES)
    38  	p.bar.ShowTimeLeft = false
    39  	p.bar.Start()
    40  	return p.bar.NewProxyReader(file), fileInfo.Size(), nil
    41  
    42  }
    43  
    44  func (p *ProgressBar) Terminate() {
    45  	// Adding sleep to ensure UI has finished drawing
    46  	time.Sleep(time.Second)
    47  	p.bar.Finish()
    48  }