Skip to content

Instantly share code, notes, and snippets.

@markprzepiora
Created February 6, 2017 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markprzepiora/fd3ea316d82d8c9296654f7c809a462d to your computer and use it in GitHub Desktop.
Save markprzepiora/fd3ea316d82d8c9296654f7c809a462d to your computer and use it in GitHub Desktop.
# 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