48
49
TODO: Is there anything that gets a better update when the window
49
50
is resized while the program is running?
53
53
return int(os.environ['COLUMNS'])
54
54
except (IndexError, KeyError, ValueError):
71
class ProgressBar(object):
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):
72
140
"""Progress bar display object.
74
142
Several options are available to control the display. These can
91
159
SPIN_CHARS = r'/-\|'
92
160
MIN_PAUSE = 0.1 # seconds
104
object.__init__(self)
105
self.to_file = to_file
106
self.suppressed = not _supports_progress(self.to_file)
163
def __init__(self, **kwargs):
164
_BaseProgressBar.__init__(self, **kwargs)
107
165
self.spin_pos = 0
111
self.last_total = None
112
self.show_pct = show_pct
113
self.show_spinner = show_spinner
114
self.show_eta = show_eta
115
self.show_bar = show_bar
116
self.show_count = show_count
118
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
126
191
def update(self, msg, current_cnt=None, total_cnt=None):
127
192
"""Update and redraw progress bar."""
131
194
# save these for the tick() function
132
195
self.last_msg = msg
133
196
self.last_cnt = current_cnt
134
197
self.last_total = total_cnt
137
if self.start_time is None:
138
self.start_time = now
140
interval = now - self.last_update
141
if interval > 0 and interval < self.MIN_PAUSE:
144
self.last_update = now
147
203
assert current_cnt <= total_cnt
265
from time import sleep
320
print 'dumb-terminal test:'
321
pb = DotsProgressBar()
323
pb.update('Leoparden', i, 99)
329
print 'smart-terminal test:'
266
330
pb = ProgressBar(show_pct=True, show_bar=True, show_spinner=False)
267
331
for i in range(100):
268
332
pb.update('Elephanten', i, 99)