Skip to content
Myr

Support incremental output in the pager

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

Closes #3242 and #2542

authored by Kevin Deldycke · committed by Kevin Deldycke · parent ffcc749 · Browse files · 3 files+45 added−0 removed
This commit

State

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

Diff stats

3 files+45 added−0 removed

  • tests:+38 added−0 removed
  • other:+7 added−0 removed

3 files+45 added−0 removed

modified:src/click/termui.py+4 added
344 unmodified lines
345346 with get_pager_file(color=color) as pager:347 for text in itertools.chain(text_generator, "\n"):348 pager.write(text)349 # Flush after each write so a slow generator streams to the pager350 # incrementally rather than staying invisible until the pipe buffer351 # fills (~8 KB).352 pager.flush()353354355@t.overload356def progressbar(
589 unmodified lines
modified:tests/test_termui.py+38 added
724 unmodified lines
725 assert err == ""726 assert out == expected727728729def test_echo_via_pager_streams_each_write(monkeypatch):730 """Each write is flushed so a slow generator streams to the pager731 incrementally instead of buffering until the end (issues #3242, #2542).732 """733 calls = []734735 class RecordingStream(io.StringIO):736 def __init__(self):737 super().__init__()738 self.color = None739740 def write(self, s):741 calls.append("write")742 return super().write(s)743744 def flush(self):745 calls.append("flush")746747 stream = RecordingStream()748 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)749 monkeypatch.setattr(click._termui_impl, "_default_text_stdout", lambda: stream)750751 def generate():752 yield "a\n"753 yield "b\n"754 yield "c\n"755756 click.echo_via_pager(generate())757758 # No two writes are adjacent: every chunk is flushed before the next one,759 # so the pager sees output as it is produced.760 assert not any(761 calls[i] == "write" and calls[i + 1] == "write" for i in range(len(calls) - 1)762 )763 assert calls.count("write") == 4 # three chunks plus the trailing newline764 assert stream.getvalue() == "a\nb\nc\n\n"765766767def test_get_pager_file_pager_missing_binary_falls_back(monkeypatch, tmp_path):768 """``PAGER`` pointing to a nonexistent binary falls back to the text stdout."""769 pager_out = tmp_path / "pager_out.txt"770
736 unmodified lines
modified:CHANGES.rst+3 added
12 unmodified lines
13 render a stray leading space before the ``(DEPRECATED)`` label. :pr:`3509`14- A :class:`Group` with ``invoke_without_command=True`` marks its subcommand as15 optional in the usage help, showing ``[COMMAND]`` instead of ``COMMAND``.16 :issue:`3059` :pr:`3507`17- ``echo_via_pager`` flushes after each write, so passing a generator streams18 output to the pager incrementally instead of staying hidden until the pipe19 buffer fills. :issue:`3242` :issue:`2542` :pr:`3534`202122Version 8.4.123-------------
1476 unmodified lines