piotrmurach / tty-progressbar

Display a single or multiple progress bars in the terminal.
https://ttytoolkit.org
MIT License
424 stars 24 forks source link

A way to decorate enumerable with progressbar #20

Closed prometh07 closed 7 years ago

prometh07 commented 7 years ago

I'm not sure whether it aligns with the gem's purpose, but I think it would be quite nice to have a built-in function wrapping an enumerable with a progressbar, so instead of manually advancing a progressbar with every iteration:

bar = TTY::ProgressBar.new("downloading [:bar]", total: 30)
30.times do
  sleep(0.1)
  bar.advance(1)
end

one could just call something like: with_bar(enumerable).map { |v| computation(v) }

An example of a simple with_bar implementation:

def with_bar(ary)
  Enumerator.new do |y|
    bar = TTY::ProgressBar.new('[:bar]', total: ary.size)
    ary.each do |e|
      bar.advance(1)
      y << e
    end
  end
end

Perhaps with_bar should take another argument, so one could customize a progressbar.

What do you think?

piotrmurach commented 7 years ago

I'm sorry but I would be inclined to say no to this feature. I want to keep tty-progressbar as is and give a full control so as to wrapping the progression logic. Having said, I think we could probably add automatic progression to this bar based on some conditions - see tty-spinner#auto_spin.

Unfortunately, I'm currently taking a leave from OSS contributions until next month so I won't be able to work on this library. But please submit PRs and I will review as soon as I can.

piotrmurach commented 7 years ago

Initially I was reluctant to consider this as a good addition. However, after thinking about it I came round to actually liking this idea quite a bit. I think this simplifies a lot of use cases. I have added #iterate message which you can read about in docs. I've also added example to demonstrate usage. I hope you will enjoy it!