multibar icon indicating copy to clipboard operation
multibar copied to clipboard

use case limitation

Open malikbenkirane opened this issue 5 years ago • 0 comments

Hi,

I've been playing with the package and I would like to be able to dynamically add/reset/remove progress bars. That is I don't know how many progress bars there will be on the screen before starting the program. In other words the number of bars may vary. I suspected that the package will not provide the feature because I see you call go mybars.Listen() after "making" all the bars with mybars.MakeBar calls.

I've written a short example (about the size of the example showcased in the README) to show the limitation as when 1 second has passed all the bars freeze :

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/sethgrid/multibar"
)

func main() {
	multibars, err := multibar.New()
	if err != nil {
		panic(err)
	}
	bars := []multibar.ProgressFunc{}
	stopChannels := [](chan int){}
	for i := 0; i < 5; i++ {
		bars = append(bars, multibars.MakeBar(10, fmt.Sprintf("%d", i)))
		stopChannels = append(stopChannels, make(chan int))
	}
	go multibars.Listen()
	for i := 0; i < 5; i++ {
		go func(i int) {
			t := time.NewTicker(200 * time.Millisecond)
			step := 0
			defer t.Stop()
			for {
				select {
				case <-t.C:
					step++
					if step == 10 {
						return
					}
					bars[i](step)
				case <-stopChannels[i]:
					return
				}
			}
		}(i)
	}
	t := time.NewTicker(1 * time.Second)
	<-t.C
	t.Stop()
	stopChannels[2] <- 1
	surprise := multibars.MakeBar(10, "surprise")
	go multibars.Listen()
	wg := &sync.WaitGroup{}
	wg.Add(1)
	go func() {
		t := time.NewTicker(200 * time.Millisecond)
		defer func() {
			t.Stop()
			wg.Done()
		}()
		for i := 0; i < 10; i++ {
			<-t.C
			surprise(i)
		}
	}()
	wg.Wait()
}

Has anyone already succeeded to do something like this ? Thank you in advance!

malikbenkirane avatar May 10 '20 10:05 malikbenkirane