Skip to content

Commit

Permalink
Remove _tempfile_pager pipe command.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBackx committed Nov 30, 2024
1 parent a9af17d commit 51389d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def pager(generator: t.Iterable[str], color: t.Optional[bool] = None) -> None:
if os.environ.get("TERM") in ("dumb", "emacs"):
return _nullpager(stdout, generator, color)
if WIN or sys.platform.startswith("os2"):
return _tempfilepager(generator, "more", color, pipe=True)
return _pipepager(generator, "more", color)
if which("less") is not None:
return _pipepager(generator, "less", color)

Expand Down Expand Up @@ -413,19 +413,25 @@ def _pipepager(generator: t.Iterable[str], cmd: str, color: t.Optional[bool]) ->
elif "r" in less_flags or "R" in less_flags:
color = True

c = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, env=env)
stdin = t.cast(t.BinaryIO, c.stdin)
encoding = get_best_encoding(stdin)
c = subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
env=env,
errors="replace",
text=True,
)
assert c.stdin is not None
try:
for text in generator:
if not color:
text = strip_ansi(text)

stdin.write(text.encode(encoding, "replace"))
c.stdin.write(text)
except (OSError, KeyboardInterrupt):
pass
else:
stdin.close()
c.stdin.close()

# Less doesn't respect ^C, but catches it for its own UI purposes (aborting
# search or other commands inside less).
Expand All @@ -448,7 +454,6 @@ def _tempfilepager(
generator: t.Iterable[str],
cmd: str,
color: t.Optional[bool],
pipe: bool = False,
) -> None:
"""Page through text by invoking a program on a temporary file.
Expand All @@ -467,11 +472,7 @@ def _tempfilepager(
with open_stream(filename, "wb")[0] as f:
f.write(text.encode(encoding))
try:
if pipe:
with open_stream(filename, "rb")[0] as f:
subprocess.call([cmd], stdin=f)
else:
subprocess.call([cmd, filename])
subprocess.call([cmd, filename])
except OSError:
# Command not found
pass
Expand Down
9 changes: 7 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def _test_gen_func():
yield "abc"


@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])
@pytest.mark.parametrize(
"test",
Expand All @@ -195,7 +194,8 @@ def _test_gen_func():
],
)
def test_echo_via_pager(monkeypatch, capfd, cat, test):
monkeypatch.setitem(os.environ, "PAGER", cat)
if not WIN:
monkeypatch.setitem(os.environ, "PAGER", cat)
monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)

expected_output = test[0]
Expand All @@ -204,6 +204,11 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
click.echo_via_pager(test_input)

out, err = capfd.readouterr()
if WIN:
# Windows character changes.
expected_output = expected_output.replace("\n", "\r\n")
# more adds this when paging
expected_output += "\r\n"
assert out == expected_output


Expand Down

0 comments on commit 51389d4

Please sign in to comment.