Support incremental output in the pager (#3534)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 65feb61, 399919f · 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
Provenance
- Commit
cd9bdd9- Tree
34eb8eb- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
65feb61- Parent 2
399919f
3 files changed+45 added−0 removed
modified:src/click/termui.py+4 added
344 unmodified lines345 346 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()353 354 355 @t.overload356 def progressbar(589 unmodified linesmodified:tests/test_termui.py+38 added
724 unmodified lines725 assert err == ""726 assert out == expected727 728 729+def 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 = []734+735+ class RecordingStream(io.StringIO):736+ def __init__(self):737+ super().__init__()738+ self.color = None739+740+ def write(self, s):741+ calls.append("write")742+ return super().write(s)743+744+ def flush(self):745+ calls.append("flush")746+747+ stream = RecordingStream()748+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)749+ monkeypatch.setattr(click._termui_impl, "_default_text_stdout", lambda: stream)750+751+ def generate():752+ yield "a\n"753+ yield "b\n"754+ yield "c\n"755+756+ click.echo_via_pager(generate())757+758+ # 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"765+766+767 def 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 linesmodified:CHANGES.rst+3 added
12 unmodified lines13 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`20 21 22 Version 8.4.123 -------------1478 unmodified lines