Skip to content

Commit

Permalink
Merge pull request #24 from zverok/lazy-enumerator-compatibility
Browse files Browse the repository at this point in the history
Lazy enumerators compatibility
  • Loading branch information
piotrmurach committed Jan 15, 2018
2 parents 5350e36 + 0b54f2c commit 4ff9be2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/tty/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def advance(progress = 1, tokens = {})
end

# Iterate over collection either yielding computation to block
# or provided Enumerator.
# or provided Enumerator. If the bar's `total` was not set,
# it would be taken from `collection.count`, otherwise previously
# set `total` would be used. This allows using the progressbar
# with infinite, lazy, or slowly-calculated enumerators.
#
# @example
# bar.iterate(30.times) { ... }
Expand All @@ -170,7 +173,7 @@ def advance(progress = 1, tokens = {})
#
# @api public
def iterate(collection, progress = 1, &block)
update(total: collection.count)
update(total: collection.count) unless total
prog = Enumerator.new do |iter|
collection.each do |elem|
advance(progress)
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/iterate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@

expect(values).to eq([0,1,2,3,4])
end

it "does not uses collection's count if total is provided" do
bar = TTY::ProgressBar.new("[:bar]", output: output, total: 5)
iterable = 5.times
expect(iterable).not_to receive(:count)
progress = bar.iterate(iterable)

values = []

progress.each { |v| values << v }

expect(values).to eq([0,1,2,3,4])
end
end

0 comments on commit 4ff9be2

Please sign in to comment.