github.com/vipernet-xyz/tm@v0.34.24/libs/progressbar/progressbar.go (about)

     1  package progressbar
     2  
     3  import "fmt"
     4  
     5  // the progressbar indicates the current status and progress would be desired.
     6  // ref: https://www.pixelstech.net/article/1596946473-A-simple-example-on-implementing-progress-bar-in-GoLang
     7  
     8  type Bar struct {
     9  	percent int64  // progress percentage
    10  	cur     int64  // current progress
    11  	start   int64  // the init starting value for progress
    12  	total   int64  // total value for progress
    13  	rate    string // the actual progress bar to be printed
    14  	graph   string // the fill value for progress bar
    15  }
    16  
    17  func (bar *Bar) NewOption(start, total int64) {
    18  	bar.cur = start
    19  	bar.start = start
    20  	bar.total = total
    21  	bar.graph = "█"
    22  	bar.percent = bar.getPercent()
    23  }
    24  
    25  func (bar *Bar) getPercent() int64 {
    26  	return int64(float32(bar.cur-bar.start) / float32(bar.total-bar.start) * 100)
    27  }
    28  
    29  func (bar *Bar) Play(cur int64) {
    30  	bar.cur = cur
    31  	last := bar.percent
    32  	bar.percent = bar.getPercent()
    33  	if bar.percent != last && bar.percent%2 == 0 {
    34  		bar.rate += bar.graph
    35  	}
    36  	fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent, bar.cur, bar.total)
    37  }
    38  
    39  func (bar *Bar) Finish() {
    40  	fmt.Println()
    41  }