Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/prompt_toolkit/input/vt100_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ def _call_handler(
self._in_bracketed_paste = True
self._paste_buffer = ""
else:
# For character-valued ANSI_SEQUENCES entries (plain characters
# that are not Keys members), use the character as insert_text
# instead of the raw escape sequence so that self-insert
# bindings work correctly.
if not isinstance(key, Keys):
insert_text = key
self.feed_key_callback(KeyPress(key, insert_text))

def feed(self, data: str) -> None:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_inputstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,26 @@ def test_cpr_response_2(processor, stream):
assert len(processor.keys) == 2
assert processor.keys[0].key == Keys.CPRResponse
assert processor.keys[1].key == Keys.ControlJ


def test_character_valued_ansi_sequence(processor, stream):
"""Character-valued ANSI_SEQUENCES entries should use the character as data.

Regression test for: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/2086
When a character-valued entry is added to ANSI_SEQUENCES (e.g., for xterm
modifyOtherKeys support), the KeyPress data should be the character, not the
raw escape sequence.
"""
from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES

SEQ = "\x1b[27;2;78~" # Shift+N via xterm modifyOtherKeys=2
ANSI_SEQUENCES[SEQ] = "N"

try:
stream.feed(SEQ)
assert len(processor.keys) == 1
assert processor.keys[0].key == "N"
# data should be the character, not the raw escape sequence
assert processor.keys[0].data == "N"
finally:
del ANSI_SEQUENCES[SEQ]
Loading