From ea4ee75376e0e9955def79a2c167562c5f4f5354 Mon Sep 17 00:00:00 2001 From: GreyElaina Date: Sat, 25 Jun 2022 20:49:53 +0800 Subject: [PATCH 1/3] fix Add auto wordwrap for StdoutProxy #1483 --- src/prompt_toolkit/output/flush_stdout.py | 27 +++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/prompt_toolkit/output/flush_stdout.py b/src/prompt_toolkit/output/flush_stdout.py index 805a81e01..d956e62cb 100644 --- a/src/prompt_toolkit/output/flush_stdout.py +++ b/src/prompt_toolkit/output/flush_stdout.py @@ -6,6 +6,18 @@ __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 @@ -28,6 +40,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 = "\r\n".join(_cut(data, width)) + if has_binary_io: stdout.buffer.write(data.encode(stdout.encoding or "utf-8", "replace")) else: @@ -40,15 +57,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 From 3c77412cd42b076fa3ebb91ca3e5d713a88c21f9 Mon Sep 17 00:00:00 2001 From: GreyElaina Date: Sat, 25 Jun 2022 20:56:35 +0800 Subject: [PATCH 2/3] switch \r\n to \n --- src/prompt_toolkit/output/flush_stdout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prompt_toolkit/output/flush_stdout.py b/src/prompt_toolkit/output/flush_stdout.py index d956e62cb..658ca0160 100644 --- a/src/prompt_toolkit/output/flush_stdout.py +++ b/src/prompt_toolkit/output/flush_stdout.py @@ -43,7 +43,7 @@ def flush_stdout(stdout: TextIO, data: str) -> None: width = _getwidth() if width is not None: - data = "\r\n".join(_cut(data, width)) + data = "\n".join(_cut(data, width)) if has_binary_io: stdout.buffer.write(data.encode(stdout.encoding or "utf-8", "replace")) From 5ab9fcab3f37b6fb48ed0a54238be7b68d7e0b9b Mon Sep 17 00:00:00 2001 From: GreyElaina Date: Sat, 25 Jun 2022 21:16:03 +0800 Subject: [PATCH 3/3] make flake8 happy --- src/prompt_toolkit/output/flush_stdout.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/prompt_toolkit/output/flush_stdout.py b/src/prompt_toolkit/output/flush_stdout.py index 658ca0160..50779b800 100644 --- a/src/prompt_toolkit/output/flush_stdout.py +++ b/src/prompt_toolkit/output/flush_stdout.py @@ -6,6 +6,7 @@ __all__ = ["flush_stdout"] + def _cut(text: str, width: int): yield text[:width] while True: @@ -14,6 +15,7 @@ def _cut(text: str, width: int): break yield text + def _getwidth(): from prompt_toolkit.application import get_app_session return get_app_session().output.get_size().columns or None