prompt-toolkit/python-prompt-toolkit

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

Opened this issue · 5 comments

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

  • Operating System: MacOS and Ubuntu
  • Terminals: Kitty and Alacritty
  • prompt_toolkit version: prompt_toolkit==3.0.47

Additional Information

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



> hello


> hello

> hello

Related: #1675
Possibly related: #29

@KoStard did you manage to workaround this one? if yes how?

Nope, no solution for this so far

I initially could not reproduce the problem with the minimalist example until I resized the terminal to be narrower than the text.

The problem seems to be with UI elements that are as wide as the terminal. Which makes the spam go even crazier with full-width elements like a bottom toolbar. I made a video comparing bash, fish, bottom-toolbar ptk, and the included example above. Bash and fish have some strange artifacts (see echo shows up multiple times), but the artifacts fix themselves once the terminal is resized to be wide enough. python-prompt-toolkit seems to be doing something wrong.

The only workaround I found is to use Application(full_screen=True) which makes resizing work great except for the fact that users can't see the output of commands because the ui is always at the top. I'll try and figure out a way to fix that.

Here's a sample with the bottom toolbar if you want to see the spammier problem in action:

import prompt_toolkit


def demo():
    session = prompt_toolkit.PromptSession(
        multiline=True,
        bottom_toolbar=lambda: "a bottom toolbar",
        complete_while_typing=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()

I spent a few hours trying to fix renderer.erase to account for text wrapping. It works sometimes. I'm seeing that every small resize the prompt itself is moving up because the prompt line is full-width (layout.HSplit) and the empty space is text-wrapping (prompt toolkit fills the empty spaces too). I suspect my fix fails when the extra vertical space equals the height of the terminal window.

    def erase(self, leave_alternate_screen: bool = True) -> None:
        """
        Hide all output and put the cursor back at the first line. This is for
        instance used for running a system command (while hiding the CLI) and
        later resuming the same CLI.)

        :param leave_alternate_screen: When True, and when inside an alternate
            screen buffer, quit the alternate screen.
        """
        output = self.output

        # If the size of the terminal changed, we don't know the real height of
        # the drawn UI anymore. Text wrapping could have added new lines.
        # Calculate how much we need to move the cursor up to erase the old
        # content.
        new_cursor_y = self._cursor_pos.y
        if self._last_screen and self._last_size:
            size = output.get_size()
            if size.columns < self._last_size.columns:
                new_cursor_y = 0
                # Iterate from y=0 until the cursor because wrapping after the
                # cursor does not affect the new cursor y.
                for y in range(self._cursor_pos.y):
                    row = self._last_screen.data_buffer[y]
                    if row:
                        # Calculate width of this row.
                        old_row_width = 0
                        for unused_x, char in row.items():
                            old_row_width += char.width
                        # math.ceil(old_row_width / size.columns)
                        new_cursor_y += (old_row_width + size.columns - 1) // size.columns
                    else:
                        new_cursor_y += 1
                if self._cursor_pos.x > size.columns:
                    # The cursor wrapped to a new line
                    # math.ceil(self._cursor_pos.x / size.columns)
                    new_cursor_y += (self._cursor_pos.x + size.columns - 1) // size.columns

        output.cursor_backward(self._cursor_pos.x)
        output.cursor_up(new_cursor_y)
        output.erase_down()
        output.reset_attributes()
        output.enable_autowrap()

        output.flush()

        self.reset(leave_alternate_screen=leave_alternate_screen)

Maybe this architecture is only compatible with resizing a full-screen console app.