prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.39k stars 716 forks source link

Terminal resize causes prompt_toolkit to print the prompt and the current input again and again #1933

Open KoStard opened 1 month ago

KoStard commented 1 month ago

I encountered this issue both on MacOS and Ubuntu. I used different terminals, verified with Kitty and Alacritty.

Minimal Reproducible Example

import prompt_toolkit

def demo():
    session = prompt_toolkit.PromptSession(multiline=True)
    print("Enter your text (type 'exit' to quit):")

    while True:
        try:
            user_input = session.prompt('> ')
            if user_input.strip().lower() == 'exit':
                print("Exiting...")
                break
            print(f"You entered:\n{user_input}")
        except (EOFError, KeyboardInterrupt):
            print("Exiting...")
            break

if __name__ == "__main__":
    demo()

Description

I am developing HermesCLI, which is a chat app, and I use prompt_toolkit to handle multiline input. This issue frustrates the users as their history now gets lots of noise. When the terminal is resized, the prompt and the current input are printed repeatedly, cluttering the interface.

Steps to Reproduce

  1. Run the provided minimal example.
  2. Resize the terminal window.
  3. Observe the repeated printing of the prompt and current input.

Expected Behavior

The prompt and current input should not be printed again when the terminal is resized.

Environment

Additional Information

❯ python demo.py 
Enter your text (type 'exit' to quit):
> hello

> hello

> hello

> hello
KoStard commented 1 month ago

Related: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1675 Possibly related: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/29