MCPcopy
hub / github.com/pallets/click / get_pager_file

Function get_pager_file

src/click/_termui_impl.py:432–466  ·  view source on GitHub ↗

Context manager. Yields a writable file-like object which can be used as an output pager. .. versionadded:: 8.4.0 :param color: controls if the pager supports ANSI colors or not. The default is autodetection.

(color: bool | None = None)

Source from the content-addressed store, hash-verified

430
431@contextlib.contextmanager
432def get_pager_file(color: bool | None = None) -> t.Generator[t.TextIO, None, None]:
433 """Context manager.
434
435 Yields a writable file-like object which can be used as an output pager.
436
437 .. versionadded:: 8.4.0
438
439 :param color: controls if the pager supports ANSI colors or not. The
440 default is autodetection.
441 """
442 with _pager_contextmanager(color=color) as (stream, encoding, color):
443 # Split streams by capabilities rather than the abstract TextIO /
444 # BinaryIO annotations: buffered text streams can be unwrapped to bytes,
445 # while other streams are yielded as-is.
446 wrapper: MaybeStripAnsi | None = None
447 if _has_binary_buffer(stream):
448 # Text stream backed by a binary buffer.
449 wrapper = MaybeStripAnsi(stream.buffer, color=color, encoding=encoding)
450 stream = wrapper
451 try:
452 # Narrow the BinaryIO | TextIO union that _pager_contextmanager
453 # yields; the caller writes text to the pager.
454 yield t.cast(t.TextIO, stream)
455 finally:
456 try:
457 stream.flush()
458 finally:
459 # Hand the binary buffer back to the pager that produced it
460 # rather than letting this TextIOWrapper close it on garbage
461 # collection. The pager owns the buffer's lifecycle: subprocess
462 # pipes and temp files are closed by their own helpers, while a
463 # borrowed stdout must stay open for the caller. detach() runs
464 # even if flush() raised, so the buffer is never closed here.
465 if wrapper is not None:
466 wrapper.detach()
467
468
469@contextlib.contextmanager

Callers

nothing calls this directly

Calls 4

_pager_contextmanagerFunction · 0.85
_has_binary_bufferFunction · 0.85
MaybeStripAnsiClass · 0.85
flushMethod · 0.45

Tested by

no test coverage detected