Skip to content

Commit

Permalink
ProgressBar.new accepts a block
Browse files Browse the repository at this point in the history
  • Loading branch information
porras committed May 24, 2012
1 parent 583d1b3 commit ad903c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ The latest version of Ruby/ProgressBar is available at
=> (ProgressBar: 0/100)
>> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
test: 67% |oooooooooooooooooooooooooo | ETA: 00:00:03

>> ProgressBar.new("test", 100) do |pbar|
>> 100.times { sleep(0.1); pbar.inc }
>> end
test: 100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10


== API

--- ProgressBar#new (title, total, out = STDERR)
--- ProgressBar#new (title, total, out = STDERR, &block)
Display the initial progress bar and return a
ProgressBar object. ((|title|)) specifies the title,
and ((|total|)) specifies the total cost of processing.
Expand All @@ -41,6 +47,9 @@ The latest version of Ruby/ProgressBar is available at
more percent is proceeded or one or more seconds are
elapsed from the previous display.

It also accepts a block in case you prefer not needing
to .finish the bar (see example above).

--- ProgressBar#inc (step = 1)
Increase the internal counter by ((|step|)) and update
the display of the progress bar. Display the estimated
Expand Down
4 changes: 4 additions & 0 deletions lib/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def initialize (title, total, out = STDERR)
@format_arguments = [:title, :percentage, :bar, :stat]
clear
show
if block_given?
yield(self)
finish
end
end

attr_reader :title
Expand Down
18 changes: 14 additions & 4 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class ProgressBarTest < Test::Unit::TestCase
SleepUnit = 0.01

def do_make_progress_bar (title, total)
ProgressBar.new(title, total)
def do_make_progress_bar(title, total, &block)
ProgressBar.new(title, total, &block)
end

def test_bytes
Expand Down Expand Up @@ -106,10 +106,20 @@ def test_custom_bar_mark
}
pbar.finish
end

def test_with_block
total = 100
do_make_progress_bar("test(block)", total) do |pbar|
total.times {
sleep(SleepUnit)
pbar.inc
}
end
end
end

class ReversedProgressBarTest < ProgressBarTest
def do_make_progress_bar (title, total)
ReversedProgressBar.new(title, total)
def do_make_progress_bar(title, total, &block)
ReversedProgressBar.new(title, total, &block)
end
end

0 comments on commit ad903c9

Please sign in to comment.