-
-
Save walm/8254736 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
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