Merge pull request #706 from jojje/issue_651
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Update progress after iteration (fix #651)
authored by Dan Sully · committed by GitHub · parents 961a441, 8780a76 · 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
Provenance
- Commit
21d5125- Tree
46e1656- Author
Dan Sully <daniel-github@electricrain.com>- Committer
GitHub <noreply@github.com>- Parent 1
961a441- Parent 2
8780a76
2 files changed+60 added−19 removed
modified:click/_termui_impl.py+21 added-19 removed
101 unmodified lines102 def __iter__(self):103 if not self.entered:104 raise RuntimeError('You need to use progress bars in a with block.')105 self.render_progress()106- return self106+ return self.generator()107 108 def render_finish(self):109 if self.is_hidden:110 return19 unmodified lines130 return 0.0131 132 def format_eta(self):133 if self.eta_known:134- t = self.eta + 1134+ t = int(self.eta)135 seconds = t % 60136- t /= 60136+ t //= 60137 minutes = t % 60138- t /= 60138+ t //= 60139 hours = t % 24140- t /= 24140+ t //= 24141 if t > 0:142 days = t143 return '%dd %02d:%02d:%02d' % (days, hours, minutes, seconds)144 else:105 unmodified lines250 self.eta_known = 0251 self.current_item = None252 self.finished = True253 254- def next(self):254+ def generator(self):255+ """256+ Returns a generator which yields the items added to the bar during257+ construction, and updates the progress bar *after* the yielded block258+ returns.259+ """260+ if not self.entered:261+ raise RuntimeError('You need to use progress bars in a with block.')262+263 if self.is_hidden:256- return next(self.iter)257- try:258- rv = next(self.iter)259- self.current_item = rv260- except StopIteration:264+ for rv in self.iter:265+ yield rv266+ else:267+ for rv in self.iter:268+ self.current_item = rv269+ yield rv270+ self.update(1)271 self.finish()272 self.render_progress()263- raise StopIteration()264- else:265- self.update(1)266- return rv273 268- if not PY2:269- __next__ = next270- del next274 272-275 def pager(generator, color=None):276 """Decide what method to use for paging through text."""277 stdout = _default_text_stdout()278 if not isatty(sys.stdin) or not isatty(stdout):283 unmodified linesmodified:tests/test_termui.py+39 added
1 import click2+import time3 4 5 def test_progressbar_strip_regression(runner, monkeypatch):6 label = ' padded line'75 unmodified lines82 with runner.isolation() as outstreams:83 click.secho(None, nl=False)84 bytes = outstreams[0].getvalue()85 assert bytes == b''86+87+88+def test_progressbar_yields_all_items(runner):89+ with click.progressbar(range(3)) as progress:90+ assert len(list(progress)) == 391+92+93+def test_progressbar_update(runner, monkeypatch):94+ class FakeClock(object):95+ def __init__(self):96+ self.now = time.time()97+98+ def advance_time(self, seconds=1):99+ self.now += seconds100+101+ def time(self):102+ return self.now103+104+ fake_clock = FakeClock()105+106+ @click.command()107+ def cli():108+ with click.progressbar(range(4)) as progress:109+ for _ in progress:110+ fake_clock.advance_time()111+ print("")112+113+ monkeypatch.setattr(time, 'time', fake_clock.time)114+ monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)115+ output = runner.invoke(cli, []).output116+117+ lines = [line for line in output.split('\n') if '[' in line]118+119+ assert ' 0%' in lines[0]120+ assert ' 25% 00:00:03' in lines[1]121+ assert ' 50% 00:00:02' in lines[2]122+ assert ' 75% 00:00:01' in lines[3]123+ assert '100% ' in lines[4]