Created
September 14, 2011 21:55
-
-
Save bmarini/1217911 to your computer and use it in GitHub Desktop.
CLI progress bar in ruby
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
class ProgressBar | |
def initialize(units=60) | |
@units = units.to_f | |
end | |
def print(completed, total) | |
norm = 1.0 / (total / @units) | |
progress = (completed * norm).ceil | |
pending = @units - progress | |
Kernel.print "[#{'=' * progress }#{' ' * ( pending )}] #{percentage(completed, total)}%\r" | |
end | |
def percentage(completed, total) | |
( ( completed / total.to_f ) * 100 ).round | |
end | |
end | |
# Usage: | |
# | |
# bar = ProgressBar.new | |
# bar.print(50, 100) | |
# | |
# => [============================== ] 50% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment