Decide what method to use for paging through text.
(
color: bool | None = None,
)
| 398 | |
| 399 | |
| 400 | def _pager_contextmanager( |
| 401 | color: bool | None = None, |
| 402 | ) -> t.ContextManager[tuple[t.BinaryIO | t.TextIO, str, bool]]: |
| 403 | """Decide what method to use for paging through text.""" |
| 404 | stdout = _default_text_stdout() |
| 405 | |
| 406 | # There are no standard streams attached to write to. For example, |
| 407 | # pythonw on Windows. |
| 408 | if stdout is None: |
| 409 | stdout = StringIO() |
| 410 | |
| 411 | if not isatty(sys.stdin) or not isatty(stdout): |
| 412 | return _nullpager(stdout, color) |
| 413 | |
| 414 | # Split using POSIX mode (the default) so that quote characters are |
| 415 | # stripped from tokens and quoted Windows paths are preserved. |
| 416 | # Non-POSIX mode retains quotes in tokens, and wrapping tokens |
| 417 | # with shlex.quote re-introduces quoting issues on Windows. |
| 418 | pager_cmd_parts = shlex.split(os.environ.get("PAGER", "")) |
| 419 | if pager_cmd_parts: |
| 420 | if WIN: |
| 421 | return _tempfilepager(pager_cmd_parts, color) |
| 422 | return _pipepager(pager_cmd_parts, color) |
| 423 | |
| 424 | if os.environ.get("TERM") in ("dumb", "emacs"): |
| 425 | return _nullpager(stdout, color) |
| 426 | if WIN or sys.platform.startswith("os2"): |
| 427 | return _tempfilepager(["more"], color) |
| 428 | return _pipepager(["less"], color) |
| 429 | |
| 430 | |
| 431 | @contextlib.contextmanager |
no test coverage detected