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

Function echo_via_pager

src/click/termui.py:320–352  ·  view source on GitHub ↗

This function takes a text and shows it via an environment specific pager on stdout. .. versionchanged:: 3.0 Added the `color` flag. :param text_or_generator: the text to page, or alternatively, a generator emitting the text to page. :param colo

(
    text_or_generator: cabc.Iterable[str] | t.Callable[[], cabc.Iterable[str]] | str,
    color: bool | None = None,
)

Source from the content-addressed store, hash-verified

318
319
320def echo_via_pager(
321 text_or_generator: cabc.Iterable[str] | t.Callable[[], cabc.Iterable[str]] | str,
322 color: bool | None = None,
323) -> None:
324 """This function takes a text and shows it via an environment specific
325 pager on stdout.
326
327 .. versionchanged:: 3.0
328 Added the `color` flag.
329
330 :param text_or_generator: the text to page, or alternatively, a
331 generator emitting the text to page.
332 :param color: controls if the pager supports ANSI colors or not. The
333 default is autodetection.
334 """
335
336 if inspect.isgeneratorfunction(text_or_generator):
337 i = t.cast("t.Callable[[], cabc.Iterable[str]]", text_or_generator)()
338 elif isinstance(text_or_generator, str):
339 i = [text_or_generator]
340 else:
341 i = iter(t.cast("cabc.Iterable[str]", text_or_generator))
342
343 # convert every element of i to a text type if necessary
344 text_generator = (el if isinstance(el, str) else str(el) for el in i)
345
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 pager
350 # incrementally rather than staying invisible until the pipe buffer
351 # fills (~8 KB).
352 pager.flush()
353
354
355@t.overload

Callers

nothing calls this directly

Calls 3

get_pager_fileFunction · 0.70
writeMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected