antirez/linenoise

Single ESC or ESC Sequence?

deadsy opened this issue · 4 comments

Use the example program.

Get yourself into completeLine().
Press escape - original buffer is restored. Good.

Get yourself into completeLine().
Press left arrow - original buffer is restored. Not good.
The user would expect a left cursor movement.

If you do press escape to restore the line, the next 2 key entries are absorbed into the escape sequence processing of linenoiseEdit(). That's not good.

If you press escape during normal line editing the next 2 key entries are absorbed into the escape sequence processing of linenoiseEdit(). That's not good.

Problem: The code (completeLine/linenoiseEdit) can't distinguish between a lone escape and an escape sequence.

Idea: fix it by checking the readability of the fd after having seen the escape.

  • If it's not readable with N ms then its a single escape.
  • If it is readable within N ms then it's (probably) an escape sequence.

See my pull request: #116
It has been there for a while and no one is interested to merge that, see if that solves your problem.

That change doesn't fix it. In completeLine() the esc from an escape sequence will revert to the original buffer and then the subsequent characters from the sequence will be treated as normal input. The user sees garbage characters added to the buffer. It doesn't fix the issue in linenoiseEdit().

As I say - I think the fix has to be time based. See an escape. If you can read a next character immediately it's an escape sequence. If you can't it's a single escape.

I ported this code to Python and implemented that fix - it works fine.
I guess I can generate a patch for the C code if the people with write permissions don't want to fix it.

I created that PR some time ago, I do not quite remember the details. At lease that change solves my problem. It seems the repo owner have not merged any PR for a long time, so feel free to fork this repo and submit your changes and refer it here.

Yes - I've fixed the problem in my own repo. I also ported the code to Python because I needed it in that language...

https://github.com/deadsy/py_linenoise