Skip to content
Myr

Merge pull request #1332 from thecav/update-current-item

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

Add an optional current_item parameter to ProgressBar.update()

authored by David Lord · committed by GitHub · parents a438322, 04a17ae · Browse files · 4 files+77 added−1 removed
This commit

State

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

Diff stats

4 files+77 added−1 removed

  • tests:+44 added−0 removed
  • other:+33 added−1 removed

4 files+77 added−1 removed

modified:click/_termui_impl.py+14 added-1 removed
258 unmodified lines
259 self.avg = self.avg[-6:] + [step]260261 self.eta_known = self.length_known262263 def update(self, n_steps):263 def update(self, n_steps, current_item=None):264 """Update the progress bar by advancing a specified number of265 steps, and optionally set the ``current_item`` for this new266 position.267268 :param n_steps: Number of steps to advance.269 :param current_item: Optional item to set as ``current_item``270 for the updated position.271272 .. versionadded:: 8.0273 Added the ``current_item`` optional parameter.274 """275 self.make_step(n_steps)276 if current_item is not None:277 self.current_item = current_item278 self.render_progress()279280 def finish(self):281 self.eta_known = 0
359 unmodified lines
modified:click/termui.py+14 added
300 unmodified lines
301 for chunk in chunks:302 process_chunk(chunk)303 bar.update(chunks.bytes)304305 The ``update()`` method also takes an optional value specifying the306 ``current_item`` at the new position. This is useful when used307 together with ``item_show_func`` to customize the output for each308 manual step::309310 with click.progressbar(311 length=total_size,312 label='Unzipping archive',313 item_show_func=lambda a: a.filename314 ) as bar:315 for archive in zip_file:316 archive.extract()317 bar.update(archive.size, archive)318319 .. versionadded:: 2.0320321 .. versionadded:: 4.0322 Added the `color` parameter. Added a `update` method to the
304 unmodified lines
modified:tests/test_termui.py+44 added
237 unmodified lines
238 assert ' 75% 00:00:01' in lines[2]239 assert '100% ' in lines[3]240241242def test_progressbar_item_show_func(runner, monkeypatch):243 fake_clock = FakeClock()244245 @click.command()246 def cli():247 with click.progressbar(range(4), item_show_func=lambda x: "Custom {}".format(x)) as progress:248 for _ in progress:249 fake_clock.advance_time()250 print("")251252 monkeypatch.setattr(time, 'time', fake_clock.time)253 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)254 output = runner.invoke(cli, []).output255256 lines = [line for line in output.split('\n') if '[' in line]257258 assert 'Custom 0' in lines[0]259 assert 'Custom 1' in lines[1]260 assert 'Custom 2' in lines[2]261 assert 'Custom None' in lines[3]262263264def test_progressbar_update_with_item_show_func(runner, monkeypatch):265 fake_clock = FakeClock()266267 @click.command()268 def cli():269 with click.progressbar(length=6, item_show_func=lambda x: "Custom {}".format(x)) as progress:270 while not progress.finished:271 fake_clock.advance_time()272 progress.update(2, progress.pos)273 print("")274275 monkeypatch.setattr(time, 'time', fake_clock.time)276 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)277 output = runner.invoke(cli, []).output278279 lines = [line for line in output.split('\n') if '[' in line]280281 assert 'Custom 0' in lines[0]282 assert 'Custom 2' in lines[1]283 assert 'Custom 4' in lines[2]284285286@pytest.mark.parametrize(287 'key_char', (u'h', u'H', u'é', u'À', u' ', u'字', u'àH', u'àR')288)289@pytest.mark.parametrize('echo', [True, False])
30 unmodified lines
modified:CHANGES.rst+5 added
6 unmodified lines
7Unreleased89- Adds a repr to Command, showing the command name for friendlier debugging. (`#1267`_, `#1295`_)10- Add support for distinguishing the source of a command line parameter. (`#1264`_, `#1329`_)11- Add an optional parameter to ``ProgressBar.update`` to set the12 ``current_item``. (`#1226`_, `#1332`_)1314.. _#1267: https://github.com/pallets/click/issues/126715.. _#1295: https://github.com/pallets/click/pull/129516.. _#1264: https://github.com/pallets/click/issues/126417.. _#1329: https://github.com/pallets/click/pull/132918.. _#1226: https://github.com/pallets/click/issues/122619.. _#1332: https://github.com/pallets/click/pull/1332202122Version 7.123-----------2425Unreleased
644 unmodified lines