Last active
March 31, 2016 08:12
-
-
Save ktaragorn/b0ce5106b2b8be1c9fffce0af3286e6c to your computer and use it in GitHub Desktop.
A simple progress bar class
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 max | |
@max = max | |
@current = 0.0 | |
end | |
def draw | |
progress = @current/@max | |
wipe = "\r" | |
bar_length = 20 | |
progress_bar = (progress * bar_length).to_i | |
bar = "[" + "*" * progress_bar + " " * (bar_length - progress_bar) + "]" | |
count = "[#{@current}/#{@max}]" | |
percent = "[#{(progress * 100).to_i} %]" | |
print wipe, bar, count, percent | |
end | |
def increment! by=1 | |
@current += by | |
@current = @max if @current > @max | |
draw | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment