• Stars
    star
    623
  • Rank 72,088 (Top 2 %)
  • Language
    Ruby
  • License
    Do What The F*ck ...
  • Created over 13 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Ruby terminal progress_bar

ProgressBar

Build StatusGem Version

ProgressBar is a simple Ruby library for displaying progress of long-running tasks on the console. It is intended to be as simple to use as possible.

NOTE: This project isn't dead! It's just feature complete, and I don't want to keep adding things to it. If you find bugs, please open an Issue, or even better, a Pull Request, and I'll take a look. We at ProgressBar know you have lots of progress bar alternatives, and we thank you for using ProgressBar!

Installation

gem install progress_bar

Examples

The Easy Way

require 'progress_bar'

bar = ProgressBar.new

100.times do
  sleep 0.1
  bar.increment!
end

Produces output like:

[#######################################                           ] [ 59.00%] [00:06]

Note: It may not be exactly like this. I might have changed the default meters between now and when I wrote this readme, and forgotten to update it.

Setting the Max

Usually, the defaults should be fine, the only thing you'll need to tweak is the max.

bar = ProgressBar.new(1000)

Larger Steps

If you want to process several things, and update less often, you can pass a number to #increment!

    bar.increment! 42

Printing additional output

Sometimes you want to print some additional messages in the output, but since the ProgressBar uses terminal control characters to replace the text on the same line on every update, the output looks funny:

[#######################################                           ] [ 59.00%] [00:06]
Hello!
[#########################################                         ] [ 60.00%] [00:05]

To prevent this, you can use ProgressBar#puts so ProgressBar knows you want to print something, and it'll clear the bar before printing, then resume printing on the next line:

100.times do |i|
  sleep 0.1
  bar.puts "Halfway there!" if i == 50
  bar.increment!
end

Produces output like:

Halfway there!
[##################################] [100/100] [100%] [00:10] [00:00] [  9.98/s]

Try it out in examples/printing_messages.rb to see how it looks.

Picking the meters

By default, ProgressBar will use all available meters (this will probably change). To select which meters you want, and in which order, pass them to the constructor:

bar = ProgressBar.new(100, :bar, :rate, :eta)

Available Meters

  • :bar -- The bar itself, fills empty space with "#"s. Ex: [### ].
  • :counter -- Number of items complete, over the max. Ex: [ 20/100]
  • :percentage -- Percentage of items in the maximum. Ex: [ 42%]
  • :elapsed -- Time elapsed (since the ProgressBar was initialized. Ex: [00:42]
  • :eta -- Estimated Time remaining. Given the rate that items are completed, a guess at how long the rest will take. Ex: [01:30]
  • :rate -- The rate at which items are being completed. Ex: [ 42.42/s]

Run the tests to see examples of all the formats, with different values and maximums.

gem install --development progress_bar
rspec spec/*_spec.rb

Using ProgressBar on Enumerable-alikes.

If you do a lot of progresses, you can shorten your way with this:

class Array
  include ProgressBar::WithProgress
end

[1,2,3].each_with_progress{do_something}

# or any other Enumerable's methods:

(1..1000).to_a.with_progress.select{|i| (i % 2).zero?}

You can include ProgressBar::WithProgress in any class, having methods #count and #each, like some DB datasets and so on.

If you are using progress_bar regularly on plain arrays, you may want to do:

require 'progress_bar/core_ext/enumerable_with_progress'

# it adds each_with_progress/with_progress to Array/Hash/Range

(1..400).with_progress.select{|i| (i % 2).zero?}

If you want to display only specific meters you can do it like so:

(1..400).with_progress(:bar, :elapsed).select{|i| (i % 2).zero?}

More Repositories

1

cloudwatch_scheduler

Use Cloudwatch Events to kick off recurring SQS ActiveJob jobs
Ruby
23
star
2

rspec-resembles_json_matchers

Ruby
20
star
3

letsencrypt-route53

A set of tools to perform a Lets Encrypt DNS01 update on Route53
Ruby
19
star
4

dm-tokyotyrant-adapter

An adapter for the TokyoTyrant network interface on the TokyoCabinet database engine
Ruby
15
star
5

datapathy

A stupid-simple ORM that provides the minimum neccessary api, and stays out of your way
Ruby
14
star
6

dry-transaction-extra

Extra steps and functionality for Dry::Transaction
Ruby
10
star
7

faraday-cache

Ruby
8
star
8

uri_template

Implements the URITemplate draft v7
Ruby
7
star
9

dotfiles

Shell
7
star
10

mwrc2010

JavaScript
6
star
11

prissy

Ruby
6
star
12

dm-s3-adapter

An adapter for using Amazon's S3 with DataMapper
Ruby
6
star
13

mold

A less annoying form builder
Ruby
5
star
14

dm-lite

A simplified DataMapper for me to try out ideas
Ruby
4
star
15

spinal

TODO: one-line summary of your gem
Ruby
4
star
16

dm-echo-adapter

A DataMapper Adapter that wraps another adapter, and prints the args and return values to STDOUT
Ruby
3
star
17

mtnwest2009

My Presentation for MountainWest 2009
JavaScript
3
star
18

blog.theamazingrando.com

Posts for my blog
SCSS
3
star
19

jrb_handler

A Ruby Template library for rendering JSON web services
Ruby
3
star
20

dm-adapters

Some experimental http-ish adapters for datamapper
Ruby
3
star
21

benchmarks

Ruby
3
star
22

rails-plugins

Various useful plugins for rails.
Ruby
2
star
23

giki

A Wiki...on Git
Ruby
2
star
24

rack-acceptable

Adds an #acceptable_media_types to Rack::Request for conneg
Ruby
2
star
25

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
2
star
26

resume

My Resume, using a custom Rake task to render mkd -> html
Ruby
2
star
27

dm-abstract-rest-adapter

A DataMapper Adapter that handles the HTTP part of a Restful adapter
2
star
28

restblog

A blog the way I want it
Ruby
2
star
29

blog

My Blog
Ruby
2
star
30

paul.github.com

My Blog
2
star
31

doctaur

Ruby
1
star
32

rackcloudrb

TODO
Ruby
1
star
33

resourceful-redis-cache-manager

TODO
Ruby
1
star
34

quickstart

An evolving set of best-practice html, css, and js settings, so that I don't have to look them up every time.
1
star
35

sterno

1
star
36

vimfiles

Vim Script
1
star
37

mongomon

Ruby
1
star
38

awesome

C
1
star
39

heracles

Ruby
1
star
40

r2d2

rrdcache client library
Ruby
1
star
41

sqlmatic

1
star
42

env_config

Ruby
1
star
43

datapathy-ssbe-adapter

TODO
Ruby
1
star
44

rack-accept

TODO: one-line summary of your gem
1
star
45

readme_maker

Tool for generating sample outputs from code in a readme
Ruby
1
star
46

hydra_shield

A Rails Engine to generate and consume Hydra APIs
Ruby
1
star
47

rackcloud

TODO
1
star
48

europa

Language experiments
Ruby
1
star
49

datapathy-rfj-adapter

TODO: one-line summary of your gem
Ruby
1
star
50

browser-launcher

A Qt based tool to launch my myriad of browsers with the right options
C++
1
star
51

rest-json-template

TODO: one-line summary of your gem
Ruby
1
star