progress bar shows current item
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Joel Eager · committed by David Lord · parent af584b2 · Browse files · 4 files·+29 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: David Lord <davidism@gmail.com>
Diff stats
4 files+29 added−20 removed
- tests:+9 added−12 removed
- other:+20 added−8 removed
Provenance
- Commit
2ba2fe0- Tree
01a6c37- Author
Joel Eager <git@joeleager.me>- Committer
David Lord <davidism@gmail.com>- Parent
af584b2
4 files changed+29 added−20 removed
modified:src/click/_termui_impl.py+11 added-4 removed
300 unmodified lines301 .. versionchanged:: 8.0302 Only render when the number of steps meets the303 ``update_min_steps`` threshold.304 """305+ if current_item is not None:306+ self.current_item = current_item307+308 self._completed_intervals += n_steps309 310 if self._completed_intervals >= self.update_min_steps:311 self.make_step(self._completed_intervals)309-310- if current_item is not None:311- self.current_item = current_item312-312 self.render_progress()313 self._completed_intervals = 0314 315 def finish(self):20 unmodified lines336 yield from self.iter337 else:338 for rv in self.iter:339 self.current_item = rv340+341+ # This allows show_item_func to be updated before the342+ # item is processed. Only trigger at the beginning of343+ # the update interval.344+ if self._completed_intervals == 0:345+ self.render_progress()346+347 yield rv348 self.update(1)349+350 self.finish()351 self.render_progress()352 353 342 unmodified linesmodified:src/click/termui.py+7 added-4 removed
364 unmodified lines365 default is `True` if the iterable has a length or366 `False` if not.367 :param show_pos: enables or disables the absolute position display. The368 default is `False`.369- :param item_show_func: a function called with the current item which370- can return a string to show the current item371- next to the progress bar. Note that the current372- item can be `None`!369+ :param item_show_func: A function called with the current item which370+ can return a string to show next to the progress bar. If the371+ function returns ``None`` nothing is shown. The current item can372+ be ``None``, such as when entering and exiting the bar.373 :param fill_char: the character to use to show the filled part of the374 progress bar.375 :param empty_char: the character to use to show the non-filled part of376 the progress bar.16 unmodified lines393 .. versionchanged:: 8.0394 Output is shown even if execution time is less than 0.5 seconds.395 396 .. versionchanged:: 8.0397+ ``item_show_func`` shows the current item, not the previous one.398+399+ .. versionchanged:: 8.0400 Labels are echoed if the output is not a TTY. Reverts a change401 in 7.0 that removed all output.402 403 .. versionadded:: 8.0348 unmodified linesmodified:tests/test_termui.py+9 added-12 removed
282 unmodified lines283 assert "100% " in lines[4]284 285 286 def test_progressbar_item_show_func(runner, monkeypatch):287+ """item_show_func should show the current item being yielded."""288+289 @click.command()290 def cli():289- with click.progressbar(290- range(3), item_show_func=lambda x: f"Custom {x}"291- ) as progress:292- for _ in progress:293- click.echo()291+ with click.progressbar(range(3), item_show_func=lambda x: str(x)) as progress:292+ for item in progress:293+ click.echo(f" item {item}")294 295 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)296- output = runner.invoke(cli, []).output297- lines = [line for line in output.splitlines() if "[" in line]298- assert "Custom None" in lines[0]299- assert "Custom 0" in lines[1]300- assert "Custom 1" in lines[2]301- assert "Custom 2" in lines[3]302- assert "Custom None" in lines[4]296+ lines = runner.invoke(cli).output.splitlines()297 298+ for i, line in enumerate(x for x in lines if "item" in x):299+ assert f"{i} item {i}" in line300 301+302 def test_progressbar_update_with_item_show_func(runner, monkeypatch):303 @click.command()304 def cli():305 with click.progressbar(142 unmodified linesmodified:CHANGES.rst+2 added
176 unmodified lines177 - Restore progress bar behavior of echoing only the label if the file178 is not a TTY. :issue:`1138`179 - Progress bar output is shown even if execution time is less than 0.5180 seconds. :issue:`1648`181+- Progress bar ``item_show_func`` shows the current item, not the182+ previous item. :issue:`1353`183 184 185 Version 7.1.2186 -------------635 unmodified lines