Insert the character for character-valued ANSI_SEQUENCES entries - #2091
Open
yange0793-dot wants to merge 1 commit into
Open
Insert the character for character-valued ANSI_SEQUENCES entries#2091yange0793-dot wants to merge 1 commit into
ANSI_SEQUENCES entries#2091yange0793-dot wants to merge 1 commit into
Conversation
An `ANSI_SEQUENCES` entry can map a sequence to a plain character instead of a `Keys` member, and `KeyPress` documents that for such a key the data is the character itself. The parser always passed the matched escape sequence as `data`, so `self-insert` typed that sequence into the buffer rather than the character. Ordinary typed characters are unaffected: they already arrive with key and data equal. Closes prompt-toolkit#2086 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.
Closes #2086.
Problem
ANSI_SEQUENCESmaps an escape sequence to a key, and that key can be a plain characterrather than a
Keysmember —KeyPressexplicitly supports both (":param key: AKeysinstance or text (one character)"), and when constructed with only a key it sets
data = keyfor the character case.Vt100Parser._call_handleralways builtKeyPress(key, insert_text), whereinsert_textis the raw matched sequence.
self-insertinsertsevent.data, so a character-valuedentry typed its own escape sequence:
This is invisible for every sequence prompt_toolkit ships, because those all resolve to
Keysmembers whose bindings never readdata.Fix
Pass the escape sequence as
dataonly forKeys, and letKeyPressderive the data fora character key. Ordinary typed characters are unchanged —
_call_handler(prefix[0], prefix[0])already had key and data equal — andKeys.BracketedPastehandling isuntouched.
Test
tests/test_inputstream.py::test_character_valued_sequenceregisters such an entry withmonkeypatch.setitemand asserts bothkeyanddata. On main it fails on thedataassertion.
Verification
Ran locally against 583b341, Python 3.14.3, darwin:
pytest tests/→157 passedruff check .andruff format --check .→ cleanmypy --strict src/on--platform win32/linux/darwin→ only the pre-existingcontrib/ssh/server.py:127error (asyncssh not installed locally); no new errorsOne open question
ANSI_SEQUENCESis still annotateddict[str, Keys | tuple[Keys, ...]], which does notadmit the character values this now handles. I left the annotation alone to keep the diff
small — widening it also means guarding
_get_reverse_ansi_sequences, whose result isdict[Keys, str]. Happy to do that here or in a follow-up if you'd like the type todocument the behaviour.
AI disclosure
Written with Claude Code (Claude Opus 5), credited in the commit trailer. The output
quoted above and the check results come from running the code locally, not from the model.