Created
April 17, 2018 14:51
-
-
Save germsvel/f410704761501e4b86b259121cce3bca 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 | |
# Prints progress like this: | |
# Progress: (15%) [==== ] | |
def initialize(total) | |
@total = total | |
@counter = 0 | |
@bar_width = 50 | |
end | |
def inc | |
@counter += 1 | |
print_progress_bar | |
end | |
def end | |
@counter = @total | |
print_progress_bar | |
end | |
private | |
def print_progress_bar | |
printf( | |
"\rProgress: (%-.2f%%) [%-#{@bar_width}s]", | |
percent_complete, | |
"=" * bar_increments_complete | |
) | |
end | |
def bar_increments_complete | |
fraction_complete * @bar_width | |
end | |
def percent_complete | |
fraction_complete * 100 | |
end | |
def fraction_complete | |
@counter.to_f / @total.to_f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment