Skip to content
Myr

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

2 files+60 added−19 removed

modified:click/_termui_impl.py+21 added-19 removed
101 unmodified lines
102 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()107108 def render_finish(self):109 if self.is_hidden:110 return
19 unmodified lines
130 return 0.0131132 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 lines
250 self.eta_known = 0251 self.current_item = None252 self.finished = True253254 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.')262263 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 rv273268 if not PY2:269 __next__ = next270 del next274272275def 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 lines
modified:tests/test_termui.py+39 added
1import click2import time345def test_progressbar_strip_regression(runner, monkeypatch):6 label = ' padded line'
75 unmodified lines
82 with runner.isolation() as outstreams:83 click.secho(None, nl=False)84 bytes = outstreams[0].getvalue()85 assert bytes == b''868788def test_progressbar_yields_all_items(runner):89 with click.progressbar(range(3)) as progress:90 assert len(list(progress)) == 3919293def test_progressbar_update(runner, monkeypatch):94 class FakeClock(object):95 def __init__(self):96 self.now = time.time()9798 def advance_time(self, seconds=1):99 self.now += seconds100101 def time(self):102 return self.now103104 fake_clock = FakeClock()105106 @click.command()107 def cli():108 with click.progressbar(range(4)) as progress:109 for _ in progress:110 fake_clock.advance_time()111 print("")112113 monkeypatch.setattr(time, 'time', fake_clock.time)114 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)115 output = runner.invoke(cli, []).output116117 lines = [line for line in output.split('\n') if '[' in line]118119 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]