Skip to content
Myr

Merge pull request #1803 from pallets/progress-show

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

don't hide fast progress bars

authored by David Lord · committed by GitHub · parents 3018f57, 3cc31b1 · Browse files · 4 files+24 added−44 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+24 added−44 removed

  • tests:+17 added−38 removed
  • other:+7 added−6 removed

4 files+24 added−44 removed

modified:src/click/_termui_impl.py+2 added-6 removed
100 unmodified lines
101 self.entered = False102 self.current_item = None103 self.is_hidden = not isatty(self.file)104 self._last_line = None105 self.short_limit = 0.5105106 def __enter__(self):107 self.entered = True108 self.render_progress()
15 unmodified lines
124 # so it is re-entry safe. Calling `next(self.generator())`125 # twice works and does "what you want".126 return next(iter(self))127129 def is_fast(self):130 return time.time() - self.start <= self.short_limit131128 def render_finish(self):133 if self.is_hidden or self.is_fast():129 if self.is_hidden:130 return131 self.file.write(AFTER_BAR)132 self.file.flush()133
124 unmodified lines
258 buf.append(" " * (clear_width - line_len))259 line = "".join(buf)260 # Render the line only if it changed.261266 if line != self._last_line and not self.is_fast():262 if line != self._last_line:263 self._last_line = line264 echo(line, file=self.file, color=self.color, nl=False)265 self.file.flush()266
422 unmodified lines
modified:src/click/termui.py+3 added
389 unmodified lines
390 :param update_min_steps: Render only when this many updates have391 completed. This allows tuning for very fast iterators.392393 .. versionchanged:: 8.0394 Output is shown even if execution time is less than 0.5 seconds.395396 .. versionchanged:: 8.0397 Labels are echoed if the output is not a TTY. Reverts a change398 in 7.0 that removed all output.399400 .. versionadded:: 8.0
348 unmodified lines
modified:tests/test_termui.py+17 added-38 removed
25 unmodified lines
26 return progress272829def test_progressbar_strip_regression(runner, monkeypatch):30 fake_clock = FakeClock()30 label = " padded line"3132 @click.command()33 def cli():34 with _create_progress(label=label) as progress:35 for _ in progress:37 fake_clock.advance_time()36 pass3739 monkeypatch.setattr(time, "time", fake_clock.time)38 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)39 assert label in runner.invoke(cli, []).output4041
15 unmodified lines
57 raise StopIteration5859 next = __next__6063 fake_clock = FakeClock()6461 @click.command()62 def cli():63 with click.progressbar(Hinted(10), label="test") as progress:64 for _ in progress:69 fake_clock.advance_time()65 pass6671 monkeypatch.setattr(time, "time", fake_clock.time)67 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)68 result = runner.invoke(cli, [])69 assert result.exception is None707172def test_progressbar_hidden(runner, monkeypatch):78 fake_clock = FakeClock()7973 @click.command()74 def cli():75 with _create_progress(label="working") as progress:76 for _ in progress:84 fake_clock.advance_time()77 pass7886 monkeypatch.setattr(time, "time", fake_clock.time)79 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)80 assert runner.invoke(cli, []).output == "working\n"8182
101 unmodified lines
184 iter(progress)185186187def test_progressbar_is_iterator(runner, monkeypatch):196 fake_clock = FakeClock()197188 @click.command()189 def cli():190 with click.progressbar(range(10), label="test") as progress:191 while True:192 try:193 next(progress)204 fake_clock.advance_time()194 except StopIteration:195 break196208 monkeypatch.setattr(time, "time", fake_clock.time)197 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)210198 result = runner.invoke(cli, [])199 assert result.exception is None200201
73 unmodified lines
275 output = runner.invoke(cli, []).output276277 lines = [line for line in output.split("\n") if "[" in line]278292 assert " 25% 00:00:03" in lines[0]293 assert " 50% 00:00:02" in lines[1]294 assert " 75% 00:00:01" in lines[2]295 assert "100% " in lines[3]279 assert " 0%" in lines[0]280 assert " 25% 00:00:03" in lines[1]281 assert " 50% 00:00:02" in lines[2]282 assert " 75% 00:00:01" in lines[3]283 assert "100% " in lines[4]284285286def test_progressbar_item_show_func(runner, monkeypatch):299 fake_clock = FakeClock()300287 @click.command()288 def cli():289 with click.progressbar(304 range(4), item_show_func=lambda x: f"Custom {x}"290 range(3), item_show_func=lambda x: f"Custom {x}"291 ) as progress:292 for _ in progress:307 fake_clock.advance_time()308 print("")293 click.echo()294310 monkeypatch.setattr(time, "time", fake_clock.time)295 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)296 output = runner.invoke(cli, []).output297 lines = [line for line in output.splitlines() if "[" in line]298 assert "Custom None" in lines[0]299 assert "Custom 0" in lines[1]300 assert "Custom 1" in lines[2]301 assert "Custom 2" in lines[3]302 assert "Custom None" in lines[4]303314 lines = [line for line in output.split("\n") if "[" in line]304316 assert "Custom 0" in lines[0]317 assert "Custom 1" in lines[1]318 assert "Custom 2" in lines[2]319 assert "Custom None" in lines[3]320321305def test_progressbar_update_with_item_show_func(runner, monkeypatch):323 fake_clock = FakeClock()324306 @click.command()307 def cli():308 with click.progressbar(309 length=6, item_show_func=lambda x: f"Custom {x}"310 ) as progress:311 while not progress.finished:331 fake_clock.advance_time()312 progress.update(2, progress.pos)333 print("")313 click.echo()314335 monkeypatch.setattr(time, "time", fake_clock.time)315 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)316 output = runner.invoke(cli, []).output317318 lines = [line for line in output.split("\n") if "[" in line]
132 unmodified lines
modified:CHANGES.rst+2 added
174 unmodified lines
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`179- Progress bar output is shown even if execution time is less than 0.5180 seconds. :issue:`1648`181182183Version 7.1.2184-------------
635 unmodified lines