16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Simple text-mode progress indicator.
20
Simple text-mode progress indicator.
22
Everyone loves ascii art!
21
24
To display an indicator, create a ProgressBar object. Call it,
22
25
passing Progress objects indicating the current state. When done,
26
29
not to clutter log files.
32
# TODO: remove functions in favour of keeping everything in one class
29
34
# TODO: should be a global option e.g. --silent that disables progress
30
35
# indicators, preferably without needing to adjust all code that
31
36
# potentially calls them.
33
# TODO: If not on a tty perhaps just print '......' for the benefit of IDEs, etc
35
# TODO: Optionally show elapsed time instead/as well as ETA; nicer
36
# when the rate is unpredictable
38
# TODO: Perhaps don't write updates faster than a certain rate, say
49
51
TODO: Is there anything that gets a better update when the window
50
52
is resized while the program is running?
53
56
return int(os.environ['COLUMNS'])
54
57
except (IndexError, KeyError, ValueError):
58
61
def _supports_progress(f):
59
if not hasattr(f, 'isatty'):
63
if os.environ.get('TERM') == 'dumb':
64
# e.g. emacs compile window
70
def ProgressBar(to_file=sys.stderr, **kwargs):
71
"""Abstract factory"""
72
if _supports_progress(to_file):
73
return TTYProgressBar(to_file=to_file, **kwargs)
75
return DotsProgressBar(to_file=to_file, **kwargs)
78
class _BaseProgressBar(object):
87
self.to_file = to_file
91
self.last_total = None
92
self.show_pct = show_pct
93
self.show_spinner = show_spinner
94
self.show_eta = show_eta
95
self.show_bar = show_bar
96
self.show_count = show_count
100
class DummyProgress(_BaseProgressBar):
101
"""Progress-bar standin that does nothing.
103
This can be used as the default argument for methods that
104
take an optional progress indicator."""
108
def update(self, msg=None, current=None, total=None):
115
class DotsProgressBar(_BaseProgressBar):
116
def __init__(self, **kwargs):
117
_BaseProgressBar.__init__(self, **kwargs)
124
def update(self, msg=None, current_cnt=None, total_cnt=None):
125
if msg and msg != self.last_msg:
127
self.to_file.write('\n')
129
self.to_file.write(msg + ': ')
132
self.to_file.write('.')
136
self.to_file.write('\n')
139
class TTYProgressBar(_BaseProgressBar):
62
return hasattr(f, 'isatty') and f.isatty()
66
class ProgressBar(object):
140
67
"""Progress bar display object.
142
69
Several options are available to control the display. These can
159
86
SPIN_CHARS = r'/-\|'
160
87
MIN_PAUSE = 0.1 # seconds
163
def __init__(self, **kwargs):
164
_BaseProgressBar.__init__(self, **kwargs)
100
self.to_file = to_file
101
self.suppressed = not _supports_progress(self.to_file)
165
102
self.spin_pos = 0
166
self.width = _width()
167
self.start_time = None
168
self.last_update = None
172
"""Return True if the bar was updated too recently"""
174
if self.start_time is None:
175
self.start_time = self.last_update = now
178
interval = now - self.last_update
179
if interval > 0 and interval < self.MIN_PAUSE:
182
self.last_update = now
106
self.last_total = None
107
self.show_pct = show_pct
108
self.show_spinner = show_spinner
109
self.show_eta = show_eta
110
self.show_bar = show_bar
111
self.show_count = show_count
187
115
self.update(self.last_msg, self.last_cnt, self.last_total)
191
119
def update(self, msg, current_cnt=None, total_cnt=None):
192
120
"""Update and redraw progress bar."""
194
124
# save these for the tick() function
195
125
self.last_msg = msg
196
126
self.last_cnt = current_cnt
197
127
self.last_total = total_cnt
130
if self.start_time is None:
131
self.start_time = now
133
interval = now - self.last_update
134
if interval > 0 and interval < self.MIN_PAUSE:
137
self.last_update = now
203
142
assert current_cnt <= total_cnt
239
178
if self.show_bar:
240
179
# progress bar, if present, soaks up all remaining space
241
cols = self.width - 1 - len(msg) - len(spin_str) - len(pct_str) \
180
cols = width - 1 - len(msg) - len(spin_str) - len(pct_str) \
242
181
- len(eta_str) - len(count_str) - 3
260
199
m = spin_str + bar_str + msg + count_str + pct_str + eta_str
262
assert len(m) < self.width
263
self.to_file.write('\r' + m.ljust(self.width - 1))
201
assert len(m) < width
202
self.to_file.write('\r' + m.ljust(width - 1))
264
203
#self.to_file.flush()
268
self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
210
self.to_file.write('\r%s\r' % (' ' * (_width() - 1)))
269
211
#self.to_file.flush()
320
print 'dumb-terminal test:'
321
pb = DotsProgressBar()
323
pb.update('Leoparden', i, 99)
329
print 'smart-terminal test:'
260
from time import sleep
330
261
pb = ProgressBar(show_pct=True, show_bar=True, show_spinner=False)
331
262
for i in range(100):
332
263
pb.update('Elephanten', i, 99)