Skip to content
Myr

Update progress after iteration (fix #651)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Reported "off by one" error is due to the fact that an interator is used instead of a generator. As such there is no way for the bar to know when it has iterated over the user's code block. This commit swaps the iterator for a generator, and changes _when_ the status report is updated, from before each iteration of a user's code block, to _after_. Updating the status report after the user's code has run allows the report to convey how much work has been _done_ so far. Accidental gain: The associated unit test performs a black-box test on the generated progress output. This test revealed a bug in the ETA time calculation were the behavior differed between python 2 and 3, causing the unit test to fail on python 3. The remedy employed to allow passing of the test is to use integer division for the ETA calculation, which should also address a related issue (fixes #485).

authored by Jonas Tingeborn · committed by Jonas Tingeborn · parent e6f22c6 · Browse files · 2 files+60 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+60 added−19 removed

  • tests:+39 added−0 removed
  • other:+21 added−19 removed

2 files+60 added−19 removed

modified:click/_termui_impl.py+21 added-19 removed
100 unmodified lines
101 def __iter__(self):102 if not self.entered:103 raise RuntimeError('You need to use progress bars in a with block.')104 self.render_progress()105 return self105 return self.generator()106107 def render_finish(self):108 if self.is_hidden:109 return
19 unmodified lines
129 return 0.0130131 def format_eta(self):132 if self.eta_known:133 t = self.eta + 1133 t = int(self.eta)134 seconds = t % 60135 t /= 60135 t //= 60136 minutes = t % 60137 t /= 60137 t //= 60138 hours = t % 24139 t /= 24139 t //= 24140 if t > 0:141 days = t142 return '%dd %02d:%02d:%02d' % (days, hours, minutes, seconds)143 else:
108 unmodified lines
252 self.eta_known = 0253 self.current_item = None254 self.finished = True255256 def next(self):256 def generator(self):257 """258 Returns a generator which yields the items added to the bar during259 construction, and updates the progress bar *after* the yielded block260 returns.261 """262 if not self.entered:263 raise RuntimeError('You need to use progress bars in a with block.')264265 if self.is_hidden:258 return next(self.iter)259 try:260 rv = next(self.iter)261 self.current_item = rv262 except StopIteration:266 for rv in self.iter:267 yield rv268 else:269 for rv in self.iter:270 self.current_item = rv271 yield rv272 self.update(1)273 self.finish()274 self.render_progress()265 raise StopIteration()266 else:267 self.update(1)268 return rv275270 if not PY2:271 __next__ = next272 del next276274277def pager(text, color=None):278 """Decide what method to use for paging through text."""279 stdout = _default_text_stdout()280 if not isatty(sys.stdin) or not isatty(stdout):
278 unmodified lines
modified:tests/test_termui.py+39 added
1import click2import time345def test_progressbar_strip_regression(runner, monkeypatch):6 label = ' padded line'
62 unmodified lines
69 with runner.isolation() as out:70 click.secho(None, nl=False)71 bytes = out.getvalue()72 assert bytes == b''737475def test_progressbar_yields_all_items(runner):76 with click.progressbar(range(3)) as progress:77 assert len(list(progress)) == 3787980def test_progressbar_update(runner, monkeypatch):81 class FakeClock(object):82 def __init__(self):83 self.now = time.time()8485 def advance_time(self, seconds=1):86 self.now += seconds8788 def time(self):89 return self.now9091 fake_clock = FakeClock()9293 @click.command()94 def cli():95 with click.progressbar(range(4)) as progress:96 for _ in progress:97 fake_clock.advance_time()98 print("")99100 monkeypatch.setattr(time, 'time', fake_clock.time)101 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)102 output = runner.invoke(cli, []).output103104 lines = [line for line in output.split('\n') if '[' in line]105106 assert ' 0%' in lines[0]107 assert ' 25% 00:00:03' in lines[1]108 assert ' 50% 00:00:02' in lines[2]109 assert ' 75% 00:00:01' in lines[3]110 assert '100% ' in lines[4]