Skip to content
Myr

progressbar: fix regression with hidden bars

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

The documentation of the progress bar states that the progress bar will output only the label of the bar if it is outputting to a file that is not a tty. However, this was broken a few versions ago, without the documentation being adjusted, and even a test added. Restore this behaviour so we follow the documentation again, and adjust the test to match. Fixes #1138.

authored by Nicolas F · committed by David Lord · parent 69a25da · Browse files · 4 files+16 added−5 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+16 added−5 removed

  • tests:+2 added−3 removed
  • other:+14 added−2 removed

4 files+16 added−5 removed

modified:src/click/_termui_impl.py+6 added
225 unmodified lines
226 def render_progress(self):227 import shutil228229 if self.is_hidden:230 # Only output the label as it changes if the output is not a231 # TTY. Use file=stderr if you expect to be piping stdout.232 if self._last_line != self.label:233 self._last_line = self.label234 echo(self.label, file=self.file, color=self.color)235236 return237238 buf = []239 # Update width in case the terminal has been resized
453 unmodified lines
modified:src/click/termui.py+6 added-2 removed
379 unmodified lines
380 info section.381 :param info_sep: the separator between multiple info items (eta etc.)382 :param width: the width of the progress bar in characters, 0 means full383 terminal width384 :param file: the file to write to. If this is not a terminal then385 only the label is printed.384 :param file: The file to write to. If this is not a terminal then385 only the label is printed.386 :param color: controls if the terminal supports ANSI colors or not. The387 default is autodetection. This is only needed if ANSI388 codes are included anywhere in the progress bar output389 which is not the case by default.390 :param update_min_steps: Render only when this many updates have391 completed. This allows tuning for very fast iterators.392393 .. versionchanged:: 8.0394 Labels are echoed if the output is not a TTY. Reverts a change395 in 7.0 that removed all output.396397 .. versionadded:: 8.0398 Added the ``update_min_steps`` parameter.399400 .. versionchanged:: 4.0
345 unmodified lines
modified:tests/test_termui.py+2 added-3 removed
74 unmodified lines
757677def test_progressbar_hidden(runner, monkeypatch):78 fake_clock = FakeClock()79 label = "whatever"7980 @click.command()81 def cli():83 with _create_progress(label=label) as progress:82 with _create_progress(label="working") as progress:83 for _ in progress:84 fake_clock.advance_time()8586 monkeypatch.setattr(time, "time", fake_clock.time)87 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)89 assert runner.invoke(cli, []).output == ""88 assert runner.invoke(cli, []).output == "working\n"899091@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])92def test_progressbar_time_per_iteration(runner, avg, expected):
379 unmodified lines
modified:CHANGES.rst+2 added
172 unmodified lines
173- ``confirmation_prompt`` can be set to a custom string. :issue:`723`174- Allow styled output in Jupyter on Windows. :issue:`1271`175- ``style()`` supports the ``strikethrough`` style. :issue:`805`176- Multiline marker is removed from short help text. :issue:`1597`177- Restore progress bar behavior of echoing only the label if the file178 is not a TTY. :issue:`1138`179180181Version 7.1.2182-------------
635 unmodified lines