Ruby/ProgressBar: A Text Progress Bar Library for Ruby

The ultimate text progress bar library for Ruby! It'll SMASH YOU OVER THE HEAD with a PURE RUSH of progress bar excitement!

Don't miss out on what all the kids are talking about! If you want everyone to know that your gem or app can survive in the cage then YOU WANT RUBY-PROGRESSBAR!

The Cage

Supported Rubies

  • MRI Ruby 1.8.7
  • MRI Ruby 1.9.2
  • MRI Ruby 1.9.3
  • JRuby (in 1.8 compat mode)
  • JRuby (in 1.9 compat mode)

It's Better Than The Other 186,312 Progress Bar Libraries Because...

  • Full test suite Build Status
  • Used by tons of other open source projects (which means we find out about bugs quickly)
  • It's pretty freakin' sweet
  • We have a road map of new features to make it even better
  • And most importantly... our awesome contributors

Installation

First:

gem install ruby-progressbar

Then in your script:

require 'ruby-progressbar'

or in your Gemfile

gem 'ruby-progressbar'

or from IRB

irb -r 'ruby-progressbar'

Basic Usage

Creation

It's simple to get started:

ProgressBar.create

Creates a basic progress bar beginning at 0, a total capacity of 100 and tells it to start.

Progress: |                                                                       |

Marking Progress

Every call to #increment will advance the bar by 1. Therefore:

50.times { progressbar.increment }

Would output an advancing line which would end up here:

Progress: |===================================                                    |

Advanced Usage

Options

If you would like to customize your prompt, you can pass options when you call .create.

ProgressBar.create(:title => "Items", :starting_at => 20, :total => 200)

Will output:

Items: |=======                                                                |

The following are the list of options you can use:

  • :title - (Defaults to Progress) - The title of the progress bar.
  • :total - (Defaults to 100) The total number of the items that can be completed.
  • :starting_at - (Defaults to 0) The number of items that should be considered completed when the bar first starts. This is also the default number that the bar will be set to if #reset is called.
  • :progress_mark - (Defaults to =) The mark which indicates the amount of progress that has been made.
  • :format - (Defaults to %t: |%B|) The format string which determines how the bar is displayed. See Formatting below.
  • :length - (Defaults to full width if possible, otherwise 80) The preferred width of the entire progress bar including any format options.
  • :output - (Defaults to STDOUT) All output will be sent to this object. Can be any object which responds to .print.
  • :smoothing - (Defaults to 0.1) See Smoothing Out Estimated Time Jitters below.

Changing Progress

  • #increment: Will advance the bar's progress by 1 unit. This is the main way of progressing the bar.
  • #decrement: Will retract the bar's progress by 1 unit.
  • #progress +=: Will allow you to increment by a relative amount.
  • #progress -=: Will allow you to decrement by a relative amount.
  • #progress=: Will allow you to jump the amount of progress directly to whatever value you would like. Note: This will almost always mess up your estimated time if you're using it.

Stopping

The bar can be stopped in four ways:

  • #finish: Will stop the bar by completing it immediately. The current position will be advanced to the total.
  • #stop: Will stop the bar by immediately cancelling it. The current position will remain where it is.
  • #pause: Will stop the bar similar to #stop but will allow it to be restarted where it previously left off by calling #resume. Note: Elapsed Time and Estimated Time will stop being affected while the bar is paused.
  • #reset: Will stop the bar by resetting all information. The current position of the bar will be reset to where it began when it was created. (eg if you passed :starting_at => 5 when you created the bar, it would reset to 5 and not 0)

Finishing

  • See #finish above.

Note: The bar will be finished automatically if the current value ever becomes equal to the total.

Refreshing

  • If you need to have the bar be redisplayed to give your users more of a "real-time" feel, you can call #refresh which will not affect the current position but will update the elapsed and estimated timers.

Formatting

The format of the progress bar is extremely easy to customize. When you create the progress bar and pass the :format option, that string will be used to determine what the bar looks like.

The flags you can use in the format string are as follows:

  • %t: Title
  • %a: Elapsed (absolute) time
  • %e: Estimated time (will fall back to ETA: ??:??:?? when it exceeds 99:00:00)
  • %E: Estimated time (will fall back to ETA: > 4 Days when it exceeds 99:00:00)
  • %f: Force estimated time to be displayed even if it exceeds 99:00:00
  • %p: Percentage complete represented as a whole number (eg: 82)
  • %P: Percentage complete represented as a decimal number (eg: 82.33)
  • %c: Number of items currently completed
  • %C: Total number of items to be completed
  • %B: The full progress bar including 'incomplete' space (eg: ==========)
  • %b: Progress bar only (eg: ==========)
  • %w: Bar With Integrated Percentage (eg: ==== 75 ====)
  • %i: Display the incomplete space of the bar (this string will only contain whitespace eg: )
  • %%: A literal percent sign %

All values have an absolute length with the exception of the bar flags (eg %B, %b, etc) which will occupy any leftover space. You can use as many bar flags as you'd like, but if you do weird things, weird things will happen; so be wary.

Example

If you would like a bar with the elapsed time on the left and the percentage complete followed by the title on the right, you'd do this:

ProgressBar.create(:format => '%a %B %p%% %t')

Which will output something like this:

Time: --:--:--                                                   0% Progress

Hard to see where the bar is? Just add your own end caps, whatever you'd like. Like so:

ProgressBar.create(:format => '%a <%B> %p%% %t')

Becomes:

Time: --:--:-- <                                               > 0% Progress

Want to put an end cap on your bar? Nothing special, just use the bar flag %b combined with the incomplete space flag %i like so:

ProgressBar.create(:format => '%a |%b>>%i| %p%% %t', :starting_at => 10)

Becomes:

Time: --:--:-- |====>>                                        | 10% Progress

Notice that the absolute length doesn't get any longer, the bar just shrinks to fill the remaining space.

Overriding the Length

By default, the progressbar will try to be as smart as possible about how wide it can be. Under most Unix systems, it should be as wide as the terminal will allow while still fitting on one line. If you wish to override this behavior, you can pass in the :length option when creating the bar like so:

ProgressBar.create(:length => 40)

Additionally, if you don't have access to the code calling the progressbar itself (say if you're using a gem like Fuubar), you can set the RUBY_PROGRESS_BAR_LENGTH environment variable and it will always override any other setting.

Note: If the terminal width is less than 20 characters or ruby-progressbar is being used on a non-*nix system, the bar will default to an 80 character width.

Realtime Customization

The following items can be set at any time. Changes cause an immediate bar refresh so no other action is needed:

  • #progress_mark=: Sets the string used to represent progress along the bar.
  • #title=: Sets the string used to represent the items the bar is tracking (or I guess whatever else you want it to be).
  • #format(format_string): If you need to adjust the format that the bar uses when rendering itself, just pass in a string in the same format as describe above.

In The Weeds

This is some stuff that makes ruby-progressbar extra awesome, but for the most part it "Just Works" and you won't have to even know it's there, but if you're curious like us, here it is.

Times... They Are A Changin'

Smoothing Out Estimated Time Jitters

Sometimes when you're tracking progress, you could have some items which take significantly longer than others. When this is the case, the ETA gauge can vary wildly from increment to increment.

RUBY PROGRESS BAR TO THE RESCUE!

Thanks to @L2G and 'the maths' you can pass the :smoothing option when creating a new bar and it will use an exponentially smoothed average rather than a linear one. A value of 0.0 means no smoothing and is equivalent to the classic behavior. A value of 1.0 is the maximum amount of smoothing. Any values between those two are valid. 0.1 is the default.

ProgressBar.create(:smoothing => 0.6)

Time Mocking Support

When mocking time, the concept of when now is becomes distorted. You can imagine that because ruby-progressbar tracks elapsed and estimated times, if it used the mocked version of now the results would be very undesirable. Fortunately, if you use one of our supported Ruby time mocking libraries, your elapsed and estimated times will appear correctly no matter when your 'now' is. Currently supported are:

Road Map

We're planning on adding a bunch of really nice features to this gem over the next few weeks. We want to keep the simple usage simple but allow for powerful features if they're needed. Our 1.0 release is the first step in that direction.

Issues

If you have problems, please create a Github issue.

Credits

thekompanee

ruby-progressbar is maintained by The Kompanee, Ltd.

The names and logos for The Kompanee are trademarks of The Kompanee, Ltd.

Contributors

Thanks

Thanks to @nex3 for giving us contributor access to the initial repo. Thanks to Hiroyuki Iwatsuki for giving us access to the gem on rubygems.org to allow us to push our new versions.

And a special thanks to Satoru Takabayashi who was the original author of the progressbar gem and who inspired us to do this rewrite.

License

ruby-progressbar 1.0 is Copyright © 2011-2012 The Kompanee. It is free software, and may be redistributed under the terms specified in the LICENSE file. ruby-progressbar 0.9.0 is Copyright © 2008 Satoru Takabayashi