Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Example:
#
# Content.find_each.with_progress{ |c| do_something(c) }
#
# Will do_something to each content and print out a nice progress bar as it goes
class Enumerator
def with_progress
return to_enum(__callee__) unless block_given?
bar_chars = %w( ▏ ▎ ▍ ▌ ▋ ▊ ▉ )
progress_count = 0
print "Calculating count..."
total_count = respond_to?(:count) ? count : nil
print "\r "
print_progress = -> do
out_of_string = total_count ? " / #{total_count}" : ""
print "\r #{bar_chars[progress_count % bar_chars.length]} #{progress_count}#{out_of_string} done"
end
print_progress[]
each do |x|
yield(x)
progress_count += 1
print_progress[]
end
puts "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.