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
Provenance
- Commit
fcd8503- Tree
45aed5d- Author
bhuffman-usgs <40209416+bhuffman-usgs@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c326df9
5 files changed+36 added−12 removed
modified:src/click/_termui_impl.py+10 added-7 removed
45 unmodified lines46 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 lines61 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_func36 unmodified lines106 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 = None112 113 def __enter__(self) -> ProgressBar[V]:114 self.entered = True22 unmodified lines137 # twice works and does "what you want".138 return next(iter(self))139 140 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 lines233 234 def render_progress(self) -> None:235 import shutil236 235- 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+ return239+240+ 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)241-245 return246 247 buf = []248 # Update width in case the terminal has been resized95 unmodified lines344 # order for that interface to work.345 if not self.entered:346 raise RuntimeError("You need to use progress bars in a with block.")347 345- 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 = rv392 unmodified linesmodified:src/click/termui.py+11 added-4 removed
286 unmodified lines287 def 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 lines363 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. The24 unmodified lines398 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.401 402+ .. versionadded:: 8.2403+ The ``hidden`` argument.404+405 .. versionchanged:: 8.0406 Output is shown even if execution time is less than 0.5 seconds.407 408 .. versionchanged:: 8.03 unmodified lines412 Labels are echoed if the output is not a TTY. Reverts a change413 in 7.0 that removed all output.414 415 .. versionadded:: 8.0409- Added the ``update_min_steps`` parameter.416+ The ``update_min_steps`` parameter.417 411- .. versionchanged:: 4.0412- Added the ``color`` parameter. Added the ``update`` method to413- the object.418+ .. versionadded:: 4.0419+ The ``color`` parameter and ``update`` method.420 421 .. versionadded:: 2.0422 """423 from ._termui_impl import ProgressBar1 unmodified line425 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 linesmodified:tests/test_termui.py+12 added-1 removed
69 unmodified lines70 result = runner.invoke(cli, [])71 assert result.exception is None72 73 74-def test_progressbar_hidden(runner, monkeypatch):74+def 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 lines81 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)82 assert runner.invoke(cli, []).output == "working\n"83 84 85+def 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+ pass91+92+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)93+ assert runner.invoke(cli, []).output == ""94+95+96 @pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])97 def test_progressbar_time_per_iteration(runner, avg, expected):98 with _create_progress(2, avg=avg) as progress:99 assert progress.time_per_iteration == expected373 unmodified linesmodified:.gitignore+2 added
1 .idea/2 .vscode/3 .venv*/4 venv*/5+.env*/6+env*/7 __pycache__/8 dist/9 .coverage*10 htmlcov/2 unmodified linesmodified:CHANGES.rst+1 added
46 unmodified lines47 :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`52 53 54 Version 8.1.855 -------------1116 unmodified lines