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
/
Copy pathexample_colors.py
executable file
·64 lines (51 loc) · 1.92 KB
/
example_colors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""Example implementation of ProgressBar with color text.
Requires colorclass (pip install colorclass).
Usage:
example_colors.py run [-f] [--light-bg]
example_colors.py run -h | --help
Options:
-f --fast Quickly run example (for testing).
-h --help Show this screen.
--light-bg Autocolors adapt to white/light backgrounds.
"""
from __future__ import print_function
import locale
import os
import signal
import sys
import time
from colorclass import Color, set_dark_background, set_light_background, Windows
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():
Windows.enable() # Does nothing if not on Windows.
# Prepare.
if os.name == 'nt':
locale.setlocale(locale.LC_ALL, 'english-us')
else:
locale.resetlocale()
progress_bar = ProgressBar(5 if OPTIONS['--fast'] else 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(6 if OPTIONS['--fast'] else 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()