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

Progress bars get multiplied #39

Open
matti777 opened this issue Mar 7, 2019 · 7 comments
Open

Progress bars get multiplied #39

matti777 opened this issue Mar 7, 2019 · 7 comments

Comments

@matti777
Copy link

matti777 commented Mar 7, 2019

My program is creating 2 Bars like so:

        uiprogress.Start()

	bar := uiprogress.AddBar(int(file.Size())).PrependElapsed().
		AppendCompleted()
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		return paddedName
	})
	bar.Fill = '#'
	bar.Head = '#'
	bar.Empty = ' '

        // In a calback, keep calling this until done:
       bar.Set(int(count))

       uiprogress.Stop()

I would expect to see 2 lines of output, one per bar -- instead I am seeing the following:

Bar1   --- [#############################################################       ]  90%
Bar1   --- [####################################################################] 100%
Bar2  --- [#############################################################       ]  90%
Bar1   --- [####################################################################] 100%
Bar2  --- [####################################################################] 100%

I am creating the bars in arbitrary goroutines. Is this an issue? Should all the bars be created in a single goroutine?

@gocruncher
Copy link

gocruncher commented Mar 19, 2019

It does not matter either all bars will be created in a single goroutine or not, you just need to use WaitGroup for this. I can assume that your code has calling fmt.println or something like that.
This works:

func main() {
	uiprogress.Start()
	var wg sync.WaitGroup
	go newBar(&wg)
	go newBar(&wg)
	time.Sleep(time.Second)
	wg.Wait()
	uiprogress.Stop()
}

func newBar(wg *sync.WaitGroup) {
	count := 100
	bar := uiprogress.AddBar(count).PrependElapsed().AppendCompleted()
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		return "bar"
	})
	wg.Add(1)
	bar.Fill = '#'
	bar.Head = '#'
	bar.Empty = ' '
	go func() {
		defer wg.Done()
		for bar.Incr() {
			time.Sleep(time.Millisecond * time.Duration(rand.Intn(40)))
		}
	}()
}

@ghost
Copy link

ghost commented Nov 7, 2020

@gocruncher @gosuri @asahasrabuddhe

func main() {
	fmt.Println("apps: deployment started: app1, app2")
	uiprogress.Start()
	var wg sync.WaitGroup
	wg.Add(100)
	for i := 1; i <= 100; i++ {
		bar := uiprogress.AddBar(100).AppendCompleted().PrependElapsed()
		go func(appName string, progressBar *uiprogress.Bar) {
			defer wg.Done()
			deploy(appName, progressBar)
		}(fmt.Sprintf("app%d", i), bar)
	}
	wg.Wait()
	uiprogress.Stop()
	fmt.Println("apps: successfully deployed: app1, app2")
}

func deploy(app string, bar *uiprogress.Bar) {

	var lastStatus string
	status := make(chan string)
	bar.Width = 50
	// prepend the deploy step to the bar
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		statusValue := <-status
		if len(statusValue) == 0 {
			return fmt.Sprintf("%s, Deployment Status = %s completed", app, lastStatus)
		}
		lastStatus = statusValue
		return fmt.Sprintf("%s, Last Status = %#v Deployment Status = %s", app, lastStatus, statusValue)
	})

	defer func() {
		bar.Set(bar.Total)
		close(status)
	}()

	bar.Incr()
	status <- "pending"

	for retry := 1; retry <= 100; retry++ {
		bar.Incr()
		time.Sleep(time.Second * 1)
		if retry%10 == 0 {
			return
		}
		status <- fmt.Sprintf("ongoing %d", retry)
	}
}

in the above code progress bars are getting repeated....any idea how can this be resolved

can we make the output be independent of terminal size ....as the terminal is scrolled back and forth the progress bars keep on getting added

@gocruncher
Copy link

Screenshot 2020-11-10 at 16 04 0@harshit22394

@harshit22394 it is not repeating for me. I used zsh and bash. The result is the same

@ghost
Copy link

ghost commented Nov 10, 2020

@gocruncher thanks for your response....can u keep the terminal size small.and then run the code....also are u running the same.above code...? If not can u share your code snippet....the issue is seen when the terminal size is small

@ghost
Copy link

ghost commented Nov 10, 2020

@gocruncher basically if u resize the terminal during the code execution u will be able to reproduce the issue

@gocruncher
Copy link

@harshit22394 I got u but unluckily as I know this is still open issue #3

@ghost
Copy link

ghost commented Nov 10, 2020

ohhh...thanks for your time though

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

2 participants