Created
May 28, 2010 13:26
-
-
Save adamgamble/417140 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Enumerable | |
def each_with_progress(&block) | |
out = STDERR | |
self.each_with_index do |thing,i| | |
out.print sprintf("%s", makeProgress(i,self.count)) + "\r" | |
block.call thing | |
end | |
nil | |
end | |
protected | |
def makeProgress(i, t) | |
percent = (Float(i) / Float(t)) * 100 | |
percent = (( percent / 1).round * 1).to_i | |
number_of_bars = percent / 5 | |
progress = "" | |
for g in 0..number_of_bars do | |
progress = progress + "=" | |
end | |
for s in 0..(19 - number_of_bars) do | |
progress = progress + " " | |
end | |
if percent < 10 && percent != 100 | |
progress = progress + " " | |
end | |
return percent.to_s + "% |" + progress + "|" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment