skip frequent progress bar renders (#1698)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shakil Rafi · committed by GitHub · parent a4763ef · Browse files · 4 files·+43 added−11 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+43 added−11 removed
- tests:+10 added−0 removed
- other:+33 added−11 removed
Provenance
- Commit
acc91bc- Tree
c6d451c- Author
Shakil Rafi <shakilrafi0@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a4763ef
4 files changed+43 added−11 removed
modified:src/click/_termui_impl.py+18 added-5 removed
60 unmodified lines61 item_show_func=None,62 label=None,63 file=None,64 color=None,65+ update_min_steps=1,66 width=30,67 ):68 self.fill_char = fill_char69 self.empty_char = empty_char7 unmodified lines77 if file is None:78 file = _default_text_stdout()79 self.file = file80 self.color = color81+ self.update_min_steps = update_min_steps82+ self._completed_intervals = 083 self.width = width84 self.autowidth = width == 085 86 if length is None:205 unmodified lines292 :param n_steps: Number of steps to advance.293 :param current_item: Optional item to set as ``current_item``294 for the updated position.295 293- .. versionadded:: 8.0296+ .. versionchanged:: 8.0297 Added the ``current_item`` optional parameter.298+299+ .. versionchanged:: 8.0300+ Only render when the number of steps meets the301+ ``update_min_steps`` threshold.302 """296- self.make_step(n_steps)297- if current_item is not None:298- self.current_item = current_item299- self.render_progress()303+ self._completed_intervals += n_steps304 305+ if self._completed_intervals >= self.update_min_steps:306+ self.make_step(self._completed_intervals)307+308+ if current_item is not None:309+ self.current_item = current_item310+311+ self.render_progress()312+ self._completed_intervals = 0313+314 def finish(self):315 self.eta_known = 0316 self.current_item = None317 self.finished = True369 unmodified linesmodified:src/click/termui.py+13 added-6 removed
297 unmodified lines298 info_sep=" ",299 width=36,300 file=None,301 color=None,302+ update_min_steps=1,303 ):304 """This function creates an iterable context manager that can be used305 to iterate over something while showing a progress bar. It will306 either iterate over the `iterable` or `length` items (that are counted46 unmodified lines353 for archive in zip_file:354 archive.extract()355 bar.update(archive.size, archive)356 356- .. versionadded:: 2.0357-358- .. versionadded:: 4.0359- Added the `color` parameter. Added a `update` method to the360- progressbar object.361-357 :param iterable: an iterable to iterate over. If not provided the length358 is required.359 :param length: the number of items to iterate over. By default the360 progressbar will attempt to ask the iterator about its30 unmodified lines391 :param color: controls if the terminal supports ANSI colors or not. The392 default is autodetection. This is only needed if ANSI393 codes are included anywhere in the progress bar output394 which is not the case by default.395+ :param update_min_steps: Render only when this many updates have396+ completed. This allows tuning for very fast iterators.397+398+ .. versionadded:: 8.0399+ Added the ``update_min_steps`` parameter.400+401+ .. versionchanged:: 4.0402+ Added the ``color`` parameter. Added the ``update`` method to403+ the object.404+405+ .. versionadded:: 2.0406 """407 from ._termui_impl import ProgressBar408 409 color = resolve_color_default(color)11 unmodified lines421 file=file,422 label=label,423 width=width,424 color=color,425+ update_min_steps=update_min_steps,426 )427 428 429 def clear():312 unmodified linesmodified:tests/test_termui.py+10 added
342 unmodified lines343 assert "Custom 2" in lines[1]344 assert "Custom 4" in lines[2]345 346 347+def test_progress_bar_update_min_steps(runner):348+ bar = _create_progress(update_min_steps=5)349+ bar.update(3)350+ assert bar._completed_intervals == 3351+ assert bar.pos == 0352+ bar.update(2)353+ assert bar._completed_intervals == 0354+ assert bar.pos == 5355+356+357 @pytest.mark.parametrize("key_char", ("h", "H", "é", "À", " ", "字", "àH", "àR"))358 @pytest.mark.parametrize("echo", [True, False])359 @pytest.mark.skipif(not WIN, reason="Tests user-input using the msvcrt module.")360 def test_getchar_windows(runner, monkeypatch, key_char, echo):88 unmodified linesmodified:CHANGES.rst+2 added
147 unmodified lines148 - The default value passed to ``prompt`` will be cast to the correct149 type like an input value would be. :pr:`1517`150 - Automatically generated short help messages will stop at the first151 ending of a phrase or double linebreak. :issue:`1082`152+- Skip progress bar render steps for efficiency with very fast153+ iterators by setting ``update_min_steps``. :issue:`676`154 155 156 Version 7.1.2157 -------------635 unmodified lines