Created
February 6, 2017 16:32
-
-
Save markprzepiora/fd3ea316d82d8c9296654f7c809a462d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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