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

You can input multiline text in TextArea when multiline=False #1894

Open
eight04 opened this issue Jul 5, 2024 · 1 comment
Open

You can input multiline text in TextArea when multiline=False #1894

eight04 opened this issue Jul 5, 2024 · 1 comment

Comments

@eight04
Copy link

eight04 commented Jul 5, 2024

The app:

from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.layout.containers import Window, HSplit
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.layout import Layout

from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.widgets import TextArea

kb = KeyBindings()

@kb.add('c-q')
def exit_(event):
  event.app.exit()

buffer1 = Buffer()  # Editable buffer.

root_container = HSplit([
  TextArea(
    prompt='Type something: ',
    multiline=False,
    wrap_lines=False,
    accept_handler=lambda buff: buffer1.insert_text(f"accept_handler called: {buff.text}\n"),
  ),
  Window(height=1, char='-'),
  Window(content=BufferControl(buffer=buffer1)),
])

layout = Layout(root_container)

app = Application(key_bindings=kb, layout=layout, full_screen=True)
app.run()

When multiline=False, pressing enter should trigger accept_handler.

However, when pasting multi-line text to the console, TextArea draws a new line then scroll down. In this case, accept_handler won't be triggered.

Another way to trigger this bug is to type very fast when the computer is slow. I guess it works similarly as copy-pasting.

Windows 10
prompt_toolkit 3.0.47
cmd.exe + Windows Terminal

@eight04
Copy link
Author

eight04 commented Aug 2, 2024

It seems that the problem is: there is no "paste" event on Windows, and prompt-toolkit will try to guess a paste event when getting multiline text:

if self.recognize_paste and self._is_paste(all_keys):

However, since it is just a guess, it is not reliable. When the app is slow responding and the user presses enter multiple times, these chars will be treated as a paste event.

To avoid this behavior, add this line after creating the app:

app.input.console_input_reader.recognize_paste = False
  1. I suggest documenting this behavior.
  2. We should handle the paste event better. Since pasting multiline text into a single-line prompt doesn't really make sense, we can reject the paste event and re-feed those keys into input. On vt100, it should throw or replace \n with a space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant