This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Done with examples. Made bar public.
Made self.bar public instead of "private". People who wish to have color progress bars (or change any of the bar's characters) may be more common. No point in leaving it private.
- Loading branch information
Showing
3 changed files
with
184 additions
and
9 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |