progressbar icon indicating copy to clipboard operation
progressbar copied to clipboard

Throttle prevents initial RenderBlank from working

Open ViRb3 opened this issue 3 years ago • 2 comments

If you create a new default bar and immediately try to RenderBlank(), nothing will render. To work around this:

func NewBar(max int, description ...string) *progressbar.ProgressBar {
	bar := progressbar.Default(int64(max), description...)
	// wait for throttle duration so initial render doesn't skip
	time.Sleep(65 * time.Millisecond)
	bar.RenderBlank()
	return bar
}

Haven't tested, but I think the problem is the p.state.lastShown is set to time.Now() along with p.config.throttleDuration in:

func getBasicState() state {
	now := time.Now()
	return state{
		startTime:   now,
		lastShown:   now,
		counterTime: now,
	}
}

Maybe set lastShown to 0?

ViRb3 avatar Oct 14 '20 01:10 ViRb3

i just stumbled across the same issue.

The initial handshake takes a bit ( ~3 seconds) and the bar will not display the blank bar until after the second write of the io.MultiWriter.

Changing getBasicState lastShown to a empty Time{} made it work.

func getBasicState() state {
	now := time.Now()
	return state{
		startTime:   now,
		lastShown:   time.Time{}, // empty time struct.
		counterTime: now,
	}
}

kirides avatar Oct 26 '20 09:10 kirides

I ran into this problem too. The solution above from @kirides almost works however it causes the OptionSetRenderBlankState(true) option to be ignored. I think a better approach is to simply have RenderBlank ignore throttling and always trigger a render. I can't imagine anyone would be abusing this API in a tight loop.

NathanBaulch avatar Jul 25 '22 02:07 NathanBaulch