Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1483 #1647

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/prompt_toolkit/output/flush_stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
__all__ = ["flush_stdout"]


def _cut(text: str, width: int):
yield text[:width]
while True:
text = text[width:]
if not text:
break
yield text


def _getwidth():
from prompt_toolkit.application import get_app_session
return get_app_session().output.get_size().columns or None


def flush_stdout(stdout: TextIO, data: str) -> None:
# If the IO object has an `encoding` and `buffer` attribute, it means that
# we can access the underlying BinaryIO object and write into it in binary
Expand All @@ -28,6 +42,11 @@ def flush_stdout(stdout: TextIO, data: str) -> None:
# My Arch Linux installation of july 2015 reported 'ANSI_X3.4-1968'
# for sys.stdout.encoding in xterm.
out: IO[bytes]

width = _getwidth()
if width is not None:
data = "\n".join(_cut(data, width))

if has_binary_io:
stdout.buffer.write(data.encode(stdout.encoding or "utf-8", "replace"))
else:
Expand All @@ -40,15 +59,7 @@ def flush_stdout(stdout: TextIO, data: str) -> None:
# resize signal. (Just ignore. The resize handler will render
# again anyway.)
pass
elif e.args and e.args[0] == 0:
# This can happen when there is a lot of output and the user
# sends a KeyboardInterrupt by pressing Control-C. E.g. in
# a Python REPL when we execute "while True: print('test')".
# (The `ptpython` REPL uses this `Output` class instead of
# `stdout` directly -- in order to be network transparent.)
# So, just ignore.
pass
else:
elif not e.args or e.args[0] != 0:
raise


Expand Down