uiprogress icon indicating copy to clipboard operation
uiprogress copied to clipboard

timeElapsed not increased on Bar.Set()?

Open matti777 opened this issue 7 years ago • 3 comments

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

matti777 avatar Feb 05 '18 05:02 matti777

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

jdell64 avatar Feb 06 '18 13:02 jdell64

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.

yifu avatar Aug 30 '21 08:08 yifu

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
}

vcschapp avatar Sep 28 '21 15:09 vcschapp