Skip to content

Commit

Permalink
show iterations per second
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Jul 10, 2018
1 parent 669b67c commit f3123fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type config struct {
// whether the output is expected to contain color codes
colorCodes bool
maxBytes int
// show the iterations per second
showIterationsPerSecond bool
}

// Theme defines the elements of the bar
Expand Down Expand Up @@ -109,6 +111,13 @@ func OptionSetBytes(maxBytes int) Option {
}
}

// OptionShowIts will also print the iterations/second
func OptionShowIts() Option {
return func(p *ProgressBar) {
p.config.showIterationsPerSecond = true
}
}

var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}

// NewOptions constructs a new instance of ProgressBar, with any options you specify
Expand Down Expand Up @@ -253,6 +262,11 @@ func renderProgressBar(c config, s state) (int, error) {
bytesString = fmt.Sprintf("(%2.1f kB/s)", kbPerSecond)
}

if c.showIterationsPerSecond {
// replace bytesString if used
bytesString = fmt.Sprintf("(%2.0f it/s)", float64(s.currentNum)/time.Since(s.startTime).Seconds())
}

str := fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
c.description,
s.currentPercent,
Expand Down
9 changes: 9 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func ExampleSetBytes() {
// 10% |█ | (1.0 kB/s) [1s:9s]
}

func ExampleSetIts() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█ | (10 it/s) [1s:9s]
}

func TestBar(t *testing.T) {
bar := New(0)
if err := bar.Add(1); err == nil {
Expand Down

0 comments on commit f3123fe

Please sign in to comment.