Skip to content

Commit

Permalink
skip frequent progress bar renders
Browse files Browse the repository at this point in the history
  • Loading branch information
srafi1 authored and davidism committed Nov 1, 2020
1 parent a4763ef commit e2952da
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ Unreleased
type like an input value would be. :pr:`1517`
- Automatically generated short help messages will stop at the first
ending of a phrase or double linebreak. :issue:`1082`
- Skip progress bar render steps for efficiency with very fast
iterators by setting ``update_min_steps``. :issue:`676`


Version 7.1.2
Expand Down
23 changes: 18 additions & 5 deletions src/click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
label=None,
file=None,
color=None,
update_min_steps=1,
width=30,
):
self.fill_char = fill_char
Expand All @@ -77,6 +78,8 @@ def __init__(
file = _default_text_stdout()
self.file = file
self.color = color
self.update_min_steps = update_min_steps
self._completed_intervals = 0
self.width = width
self.autowidth = width == 0

Expand Down Expand Up @@ -290,13 +293,23 @@ def update(self, n_steps, current_item=None):
:param current_item: Optional item to set as ``current_item``
for the updated position.
.. versionadded:: 8.0
.. versionchanged:: 8.0
Added the ``current_item`` optional parameter.
.. versionchanged:: 8.0
Only render when the number of steps meets the
``update_min_steps`` threshold.
"""
self.make_step(n_steps)
if current_item is not None:
self.current_item = current_item
self.render_progress()
self._completed_intervals += n_steps

if self._completed_intervals >= self.update_min_steps:
self.make_step(self._completed_intervals)

if current_item is not None:
self.current_item = current_item

self.render_progress()
self._completed_intervals = 0

def finish(self):
self.eta_known = 0
Expand Down
19 changes: 13 additions & 6 deletions src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def progressbar(
width=36,
file=None,
color=None,
update_min_steps=1,
):
"""This function creates an iterable context manager that can be used
to iterate over something while showing a progress bar. It will
Expand Down Expand Up @@ -353,12 +354,6 @@ def progressbar(
archive.extract()
bar.update(archive.size, archive)
.. versionadded:: 2.0
.. versionadded:: 4.0
Added the `color` parameter. Added a `update` method to the
progressbar object.
:param iterable: an iterable to iterate over. If not provided the length
is required.
:param length: the number of items to iterate over. By default the
Expand Down Expand Up @@ -397,6 +392,17 @@ def progressbar(
default is autodetection. This is only needed if ANSI
codes are included anywhere in the progress bar output
which is not the case by default.
:param update_min_steps: Render only when this many updates have
completed. This allows tuning for very fast iterators.
.. versionadded:: 8.0
Added the ``update_min_steps`` parameter.
.. versionchanged:: 4.0
Added the ``color`` parameter. Added the ``update`` method to
the object.
.. versionadded:: 2.0
"""
from ._termui_impl import ProgressBar

Expand All @@ -416,6 +422,7 @@ def progressbar(
label=label,
width=width,
color=color,
update_min_steps=update_min_steps,
)


Expand Down
10 changes: 10 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ def cli():
assert "Custom 4" in lines[2]


def test_progress_bar_update_min_steps(runner):
bar = _create_progress(update_min_steps=5)
bar.update(3)
assert bar._completed_intervals == 3
assert bar.pos == 0
bar.update(2)
assert bar._completed_intervals == 0
assert bar.pos == 5


@pytest.mark.parametrize("key_char", ("h", "H", "é", "À", " ", "字", "àH", "àR"))
@pytest.mark.parametrize("echo", [True, False])
@pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.")
Expand Down

0 comments on commit e2952da

Please sign in to comment.