ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.49k stars 392 forks source link

Bugged character input indentification while mouse event #675

Open Ruebled opened 1 year ago

Ruebled commented 1 year ago

Like can be seen in key-press example, while holding down ESC "character", and draging the mouse around some random character will spawn instead of the right ones.

ArthurSonzogni commented 1 year ago

Thanks!

The problem is that the sequence for the ESC Key is prefix of many other sequences.

Key sequence
ESC 27
MOUSE_NONE_PRESSED_66_11 27 91 60 51 53 59 54 53 59 49 50 70

When we see 27, we don't know yet if this was an escape key or if it will be a MOUSE_NONE_PRESSED_64_11.

This is a big issue. The terminal protocol is badly designed. It is not even a PrefixCode. There are "multiple" way to parse it. We can't decide.

What FTXUI does to overcome the problem: we wait for more input in case we "might" be able to eat a full MOUSE_NONE_PRESSED_64_11. Normally the terminal should be able to send the whole sequence in a single chunk within this interval and not cause any issues.

We wait for 50ms for new input. If there is nothing, we submit the ESC sequence. See code: https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/terminal_input_parser.cpp#L52-L62

I think what happen your case, we reach the 50ms timeout after receiving only 27 91 91. So this is an ESC key followed by the beginning of a MOUSE_NONE_PRESSED_64_11. As is, this is an incorrect symbol and we throw it away after the timeout.

The current implementation has been working flawlessly for many years. This is the first time someone submit a bug.

I don't have any good suggestion to improve over it. Would you have any ideas?

foodwaterwifi commented 1 year ago

@ArthurSonzogni This is a bug with how FTXUI processes the escape codes.

When you press ESC followed by moving the mouse, it can send for example "ESC ESC [ < 3 5 ; 1 1 4 ; 1 2 M".

However, FTXUI incorrectly interprets "ESC ESC [" as a single escape sequence, and "< 3 5 ; 1 1 4 ; 1 2 M" is interpreted as character input.

As far as I can tell, "ESC ESC [" is a nonsensical sequence. In fact, it doesn't make sense for there to be two ESCs heading an escape sequence.

FTXUI should instead interpret the first ESC as a keypress if it is followed immediately by another ESC.

In other words, ESC will be interpreted as an ESC keypress when:

  1. The timeout elapses.
  2. Another ESC follows immediately after it.