Skip to content
Myr

Add functionality to hide the progress bar #2609 (#2727)

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

authored by bhuffman-usgs · committed by GitHub · parent c326df9 · Browse files · 5 files+36 added−12 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: Andreas Backx <andreas@backx.org>

Diff stats

5 files+36 added−12 removed

  • tests:+12 added−1 removed
  • other:+24 added−11 removed

5 files+36 added−12 removed

modified:src/click/_termui_impl.py+10 added-7 removed
45 unmodified lines
46 fill_char: str = "#",47 empty_char: str = " ",48 bar_template: str = "%(bar)s",49 info_sep: str = " ",50 hidden: bool = False,51 show_eta: bool = True,52 show_percent: bool | None = None,53 show_pos: bool = False,54 item_show_func: t.Callable[[V | None], str | None] | None = None,
6 unmodified lines
61 self.fill_char = fill_char62 self.empty_char = empty_char63 self.bar_template = bar_template64 self.info_sep = info_sep65 self.hidden = hidden66 self.show_eta = show_eta67 self.show_percent = show_percent68 self.show_pos = show_pos69 self.item_show_func = item_show_func
36 unmodified lines
106 self.finished: bool = False107 self.max_width: int | None = None108 self.entered: bool = False109 self.current_item: V | None = None108 self.is_hidden: bool = not isatty(self.file)110 self._is_atty = isatty(self.file)111 self._last_line: str | None = None112113 def __enter__(self) -> ProgressBar[V]:114 self.entered = True
22 unmodified lines
137 # twice works and does "what you want".138 return next(iter(self))139140 def render_finish(self) -> None:139 if self.is_hidden:141 if self.hidden or not self._is_atty:142 return143 self.file.write(AFTER_BAR)144 self.file.flush()145
87 unmodified lines
233234 def render_progress(self) -> None:235 import shutil236235 if self.is_hidden:236 # Only output the label as it changes if the output is not a237 # TTY. Use file=stderr if you expect to be piping stdout.237 if self.hidden:238 return239240 if not self._is_atty:241 # Only output the label once if the output is not a TTY.242 if self._last_line != self.label:243 self._last_line = self.label244 echo(self.label, file=self.file, color=self.color)241245 return246247 buf = []248 # Update width in case the terminal has been resized
95 unmodified lines
344 # order for that interface to work.345 if not self.entered:346 raise RuntimeError("You need to use progress bars in a with block.")347345 if self.is_hidden:348 if not self._is_atty:349 yield from self.iter350 else:351 for rv in self.iter:352 self.current_item = rv
392 unmodified lines
modified:src/click/termui.py+11 added-4 removed
286 unmodified lines
287def progressbar(288 iterable: cabc.Iterable[V] | None = None,289 length: int | None = None,290 label: str | None = None,291 hidden: bool = False,292 show_eta: bool = True,293 show_percent: bool | None = None,294 show_pos: bool = False,295 item_show_func: t.Callable[[V | None], str | None] | None = None,
67 unmodified lines
363 also provided this parameter can be used to override the364 length. If an iterable is not provided the progress bar365 will iterate over a range of that length.366 :param label: the label to show next to the progress bar.367 :param hidden: hide the progressbar. Defaults to ``False``. When no tty is368 detected, it will only print the progressbar label. Setting this to369 ``False`` also disables that.370 :param show_eta: enables or disables the estimated time display. This is371 automatically disabled if the length cannot be372 determined.373 :param show_percent: enables or disables the percentage display. The
24 unmodified lines
398 which is not the case by default.399 :param update_min_steps: Render only when this many updates have400 completed. This allows tuning for very fast iterators.401402 .. versionadded:: 8.2403 The ``hidden`` argument.404405 .. versionchanged:: 8.0406 Output is shown even if execution time is less than 0.5 seconds.407408 .. versionchanged:: 8.0
3 unmodified lines
412 Labels are echoed if the output is not a TTY. Reverts a change413 in 7.0 that removed all output.414415 .. versionadded:: 8.0409 Added the ``update_min_steps`` parameter.416 The ``update_min_steps`` parameter.417411 .. versionchanged:: 4.0412 Added the ``color`` parameter. Added the ``update`` method to413 the object.418 .. versionadded:: 4.0419 The ``color`` parameter and ``update`` method.420421 .. versionadded:: 2.0422 """423 from ._termui_impl import ProgressBar
1 unmodified line
425 color = resolve_color_default(color)426 return ProgressBar(427 iterable=iterable,428 length=length,429 hidden=hidden,430 show_eta=show_eta,431 show_percent=show_percent,432 show_pos=show_pos,433 item_show_func=item_show_func,
360 unmodified lines
modified:tests/test_termui.py+12 added-1 removed
69 unmodified lines
70 result = runner.invoke(cli, [])71 assert result.exception is None727374def test_progressbar_hidden(runner, monkeypatch):74def test_progressbar_no_tty(runner, monkeypatch):75 @click.command()76 def cli():77 with _create_progress(label="working") as progress:78 for _ in progress:
2 unmodified lines
81 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)82 assert runner.invoke(cli, []).output == "working\n"838485def test_progressbar_hidden_manual(runner, monkeypatch):86 @click.command()87 def cli():88 with _create_progress(label="see nothing", hidden=True) as progress:89 for _ in progress:90 pass9192 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)93 assert runner.invoke(cli, []).output == ""949596@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])97def test_progressbar_time_per_iteration(runner, avg, expected):98 with _create_progress(2, avg=avg) as progress:99 assert progress.time_per_iteration == expected
373 unmodified lines
modified:.gitignore+2 added
1.idea/2.vscode/3.venv*/4venv*/5.env*/6env*/7__pycache__/8dist/9.coverage*10htmlcov/
2 unmodified lines
modified:CHANGES.rst+1 added
46 unmodified lines
47 :issue:`2695` :pr:`2696`48- ``Context.close`` will be called on exit. This results in all49 ``Context.call_on_close`` callbacks and context managers added via50 ``Context.with_resource`` to be closed on exit as well. :pr:`2680`51- Add ``ProgressBar(hidden: bool)`` to allow hiding the progressbar. :issue:`2609`525354Version 8.1.855-------------
1116 unmodified lines