fix 248 with a minimal impact#255
Open
paolopas wants to merge 3 commits intoantirez:masterfrom
Open
Conversation
paolopas
added a commit
to paolopas/b2
that referenced
this pull request
Apr 12, 2026
+ backport of linenoise bug fixing, see antirez/linenoise#255
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I believe it is impossible to implement a completion loop without running into the problem described in #248.
But perhaps no one has noticed another problem related to the management of the ESC key which in my opinion is strictly linked to the #248 (well described in #129 about a decade ago.) That is, after you press the ESC key, linenoise throws away the next two characters you press.
These problems arises because linenoise cannot distinguish (and does not even try to distinguish) the ESC key press from other keys that produce an escape sequence.
I saw the solution proposed in #230 but I don't like the fact that any character other than a TAB overwrites the buffer, as if the only way to escape a completion loop was to accept the current completion.
I think it's fair to be able to abort the loop and return to the previous buffer by simply pressing ESC. But I'm sure we all hit the arrow keys while iterating with TAB.
So in my opinion the simplest solution is through:
completeLinedo its job and exit the loop as soon as it sees an ESC, but leave the responsibility of handling it to its caller, which is exactly how it was intended, andlinenoiseEditFeed, don't make too many assumptions that an escape key necessarily means an escape sequence, and so be careful not to throw away keys that aren't part of an escape sequence.It doesn't cover 100% of cases, but it has the minimum impact on the current implementation (only 11 lines of code: 1 deleted + 10 added are actually needed), also thanks to a nice goto, which I put in to avoid making dangerous jumps from inside a switch (they've already invented coroutines.)
Of course the goto is a provocation, it could be done with
do ... while, but the implementation with goto is certainly more readable, as well as being shorter.I didn't even consider using
ungetc, especially since to really handle all cases (instead of just onepccharacter) you should use a queue and put in it all the extracted characters that need to be reconsidered.Note
For those who want to try, here is a version of the file with the changes (to replace the original one)
linenoise_2026-04-12.zip