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
Provenance
- Commit
716a5be- Tree
b4cdf14- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a438322- Parent 2
04a17ae
4 files changed+77 added−1 removed
modified:click/_termui_impl.py+14 added-1 removed
258 unmodified lines259 self.avg = self.avg[-6:] + [step]260 261 self.eta_known = self.length_known262 263- 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.267+268+ :param n_steps: Number of steps to advance.269+ :param current_item: Optional item to set as ``current_item``270+ for the updated position.271+272+ .. 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()279 280 def finish(self):281 self.eta_known = 0359 unmodified linesmodified:click/termui.py+14 added
300 unmodified lines301 for chunk in chunks:302 process_chunk(chunk)303 bar.update(chunks.bytes)304 305+ 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::309+310+ 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)318+319 .. versionadded:: 2.0320 321 .. versionadded:: 4.0322 Added the `color` parameter. Added a `update` method to the304 unmodified linesmodified:tests/test_termui.py+44 added
237 unmodified lines238 assert ' 75% 00:00:01' in lines[2]239 assert '100% ' in lines[3]240 241 242+def test_progressbar_item_show_func(runner, monkeypatch):243+ fake_clock = FakeClock()244+245+ @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("")251+252+ monkeypatch.setattr(time, 'time', fake_clock.time)253+ monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)254+ output = runner.invoke(cli, []).output255+256+ lines = [line for line in output.split('\n') if '[' in line]257+258+ 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]262+263+264+def test_progressbar_update_with_item_show_func(runner, monkeypatch):265+ fake_clock = FakeClock()266+267+ @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("")274+275+ monkeypatch.setattr(time, 'time', fake_clock.time)276+ monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)277+ output = runner.invoke(cli, []).output278+279+ lines = [line for line in output.split('\n') if '[' in line]280+281+ assert 'Custom 0' in lines[0]282+ assert 'Custom 2' in lines[1]283+ assert 'Custom 4' in lines[2]284+285+286 @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 linesmodified:CHANGES.rst+5 added
6 unmodified lines7 Unreleased8 9 - 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`_)13 14 .. _#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/133220 21+22 Version 7.123 -----------24 25 Unreleased644 unmodified lines