diff --git a/etaprogress/progress.py b/etaprogress/progress.py index a957cb7..3f5858b 100644 --- a/etaprogress/progress.py +++ b/etaprogress/progress.py @@ -30,10 +30,10 @@ def __init__(self, denominator, max_width=None): super(ProgressBar, self).__init__(denominator, max_width=max_width) if self.undefined: self.template = '{numerator} {bar} eta --:-- {spinner}' - self._bar = BarUndefinedAnimated() + self.bar = BarUndefinedAnimated() else: self.template = '{percent:3d}% ({fraction}) {bar} eta {eta} {spinner}' - self._bar = Bar() + self.bar = Bar() def __str__(self): """Returns the fully-built progress bar and other data.""" @@ -51,7 +51,7 @@ def __str__(self): # Determine bar width and finish. width = get_remaining_width(template.format(bar=''), self.max_width or None) - bar = self._bar.bar(width, percent=self.percent) + bar = self.bar.bar(width, percent=self.percent) return template.format(bar=bar) @staticmethod @@ -165,12 +165,12 @@ def __init__(self, denominator, max_width=None, eta_every=1): if self.undefined: self.template = ' {bar} {numerator:<11s} {rate:>9s} {eta:<12s}' BarUndefinedAnimated.CHAR_ANIMATED = '<=>' - self._bar = BarUndefinedAnimated() + self.bar = BarUndefinedAnimated() else: self.template = '{percent:^4s}{bar} {numerator:<11s} {rate:>9s} {eta:<12s}' Bar.CHAR_FULL = '=' Bar.CHAR_LEADING = '>' - self._bar = Bar() + self.bar = Bar() def __str__(self): """Returns the fully-built progress bar and other data.""" @@ -187,7 +187,7 @@ def __str__(self): # Determine bar width and finish. width = get_remaining_width(template.format(bar=''), self.max_width or None) - bar = self._bar.bar(width, percent=self.percent) + bar = self.bar.bar(width, percent=self.percent) return template.format(bar=bar) @staticmethod @@ -244,9 +244,9 @@ def __init__(self, denominator, filename, max_width=None): self.template = '{filename} {percent:>4s} {bar} {rate:>9s} | {numerator:>7s} {eta:<12s}' self.template_completed = '{filename} | {numerator:>7s} {eta:<12s}' if self.undefined: - self._bar = BarUndefinedEmpty() + self.bar = BarUndefinedEmpty() else: - self._bar = BarDoubled() + self.bar = BarDoubled() def __str__(self): """Returns the fully-built progress bar and other data.""" @@ -271,7 +271,7 @@ def __str__(self): else: width_filename = int(width * 0.4) filename = self.filename[:width_filename].ljust(width_filename) if width_filename > 0 else '' - bar = self._bar.bar(width - width_filename, percent=self.percent) + bar = self.bar.bar(width - width_filename, percent=self.percent) return template.format(bar=bar, filename=filename) @staticmethod diff --git a/example.py b/example.py new file mode 100755 index 0000000..d1a8929 --- /dev/null +++ b/example.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +"""Example implementation of all progress bars. + +Usage: + example_colors.py progress_bar + example_colors.py progress_bar_bits + example_colors.py progress_bar_bytes + example_colors.py progress_bar_wget + example_colors.py progress_bar_yum + example_colors.py -h | --help + +Options: + -h --help Show this screen. + --light-bg Autocolors adapt to white/light backgrounds. +""" + +from __future__ import print_function +import locale +from math import ceil +import signal +import sys +import time + +from docopt import docopt +from etaprogress.progress import ProgressBar, ProgressBarBits, ProgressBarBytes, ProgressBarWget, ProgressBarYum + +OPTIONS = docopt(__doc__) if __name__ == '__main__' else dict() + + +def error(message, code=1): + """Prints an error message to stderr and exits with a status of 1 by default.""" + if message: + print('ERROR: {0}'.format(message), file=sys.stderr) + else: + print(file=sys.stderr) + sys.exit(code) + + +def progress_bar(): + bar = ProgressBar(100) + for i in range(101): + bar.numerator = i + print(bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + print(bar) # Always print one last time. + + +def progress_bar_bits(): + bar = ProgressBarBits(100000000) + for i in range(0, 100000001, 1234567): + bar.numerator = i + print(bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + bar.numerator = 100000000 + print(bar) + + +def progress_bar_bytes(): + bar = ProgressBarBytes(100000000) + for i in range(0, 100000001, 1234567): + bar.numerator = i + print(bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + bar.numerator = 100000000 + print(bar) + + +def progress_bar_wget(): + bar = ProgressBarWget(100000000) + for i in range(0, 100000001, 1234567): + bar.numerator = i + print(bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + bar.numerator = 100000000 + print(bar) + + +def progress_bar_yum(): + files = { + 'CentOS-7.0-1406-x86_64-DVD.iso': 4148166656, + 'CentOS-7.0-1406-x86_64-Everything.iso': 7062159360, + 'md5sum.txt': 486, + } + for file_name, file_size in files.items(): + bar = ProgressBarYum(file_size, file_name) + for i in range(0, file_size + 1, int(file_size / 100.0)): + bar.numerator = i + print(bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + bar.numerator = file_size + print(bar) + + +def main(): + locale.resetlocale() + if OPTIONS['progress_bar']: + progress_bar() + elif OPTIONS['progress_bar_bits']: + progress_bar_bits() + elif OPTIONS['progress_bar_bytes']: + progress_bar_bytes() + elif OPTIONS['progress_bar_wget']: + progress_bar_wget() + elif OPTIONS['progress_bar_yum']: + progress_bar_yum() + else: + raise RuntimeError + + +if __name__ == '__main__': + signal.signal(signal.SIGINT, lambda *_: error('', 0)) # Properly handle Control+C + main() diff --git a/example_colors.py b/example_colors.py new file mode 100755 index 0000000..08367bc --- /dev/null +++ b/example_colors.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +"""Example implementation of ProgressBar with color text. + +Requires colorclass (pip install colorclass). + +Usage: + example_colors.py run [--light-bg] + example_colors.py run -h | --help + +Options: + -h --help Show this screen. + --light-bg Autocolors adapt to white/light backgrounds. +""" + +from __future__ import print_function +import locale +import signal +import sys +import time + +from colorclass import Color, set_dark_background, set_light_background +from docopt import docopt +from etaprogress.progress import ProgressBar + +OPTIONS = docopt(__doc__) if __name__ == '__main__' else dict() + + +def error(message, code=1): + """Prints an error message to stderr and exits with a status of 1 by default.""" + if message: + print('ERROR: {0}'.format(message), file=sys.stderr) + else: + print(file=sys.stderr) + sys.exit(code) + + +def main(): + # Prepare. + locale.resetlocale() + progress_bar = ProgressBar(100) + progress_bar.bar.CHAR_FULL = Color('{autoyellow}#{/autoyellow}') + progress_bar.bar.CHAR_LEADING = Color('{autoyellow}#{/autoyellow}') + progress_bar.bar.CHAR_LEFT_BORDER = Color('{autoblue}[{/autoblue}') + progress_bar.bar.CHAR_RIGHT_BORDER = Color('{autoblue}]{/autoblue}') + + # Run. + for i in range(101): + progress_bar.numerator = i + print(progress_bar, end='\r') + sys.stdout.flush() + time.sleep(0.25) + print(progress_bar) # Always print one last time. + + +if __name__ == '__main__': + signal.signal(signal.SIGINT, lambda *_: error('', 0)) # Properly handle Control+C + set_light_background() if OPTIONS['--light-bg'] else set_dark_background() + main()