Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeElapsed not increased on Bar.Set()? #34

Open
matti777 opened this issue Feb 5, 2018 · 3 comments
Open

timeElapsed not increased on Bar.Set()? #34

matti777 opened this issue Feb 5, 2018 · 3 comments

Comments

@matti777
Copy link

matti777 commented Feb 5, 2018

Apparently I get "---" for the time elapsed prepend when using Set(). Is this by design?

@jdell64
Copy link

jdell64 commented Feb 6, 2018

Same here. I tried setting bar.TimeStarted but that didn't work.

@yifu
Copy link

yifu commented Aug 30, 2021

Same here. Reproduced with this code:

package main

import (
	"github.com/gosuri/uiprogress"
	"time"
)

func main() {
	uiprogress.Start()            // start rendering
	defer uiprogress.Stop()
	bar := uiprogress.AddBar(100) // Add a new bar

	// optionally, append and prepend completion and elapsed time
	bar.AppendCompleted()
	bar.PrependElapsed()

	for {
		//bar.Set(bar.Current() + 1)
		bar.Incr()
		if bar.Current() == 100 {
			break
		}
		time.Sleep(time.Millisecond * 20)
	}
}

If you comment line 19 (bar.Incr()) and uncomment line 18 (//bar.Set(bar.Current() + 1)) then the elapsed time does not show up.

@vcschapp
Copy link

I noticed this as well, using the latest release (v0.0.1).

It's caused by a discrepancy between how Bar.Incr() and Bar.Set() work. It would be more consistent if Bar.Incr() was implemented in terms of Bar.Set().

Here's the current code:

// Set the current count of the bar. It returns ErrMaxCurrentReached when trying n exceeds the total value. This is atomic operation and concurrency safe.
func (b *Bar) Set(n int) error {
	b.mtx.Lock()
	defer b.mtx.Unlock()

	if n > b.Total {
		return ErrMaxCurrentReached
	}
	b.current = n
	return nil
}

// Incr increments the current value by 1, time elapsed to current time and returns true. It returns false if the cursor has reached or exceeds total value.
func (b *Bar) Incr() bool {
	b.mtx.Lock()
	defer b.mtx.Unlock()

	n := b.current + 1
	if n > b.Total {
		return false
	}
	var t time.Time
	if b.TimeStarted == t {
		b.TimeStarted = time.Now()
	}
	b.timeElapsed = time.Since(b.TimeStarted)
	b.current = n
	return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants