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
8 changes: 7 additions & 1 deletion src/prompt_toolkit/input/vt100_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,14 @@ def _call_handler(
if key == Keys.BracketedPaste:
self._in_bracketed_paste = True
self._paste_buffer = ""
else:
elif isinstance(key, Keys):
self.feed_key_callback(KeyPress(key, insert_text))
else:
# An `ANSI_SEQUENCES` entry can map a sequence to a plain
# character instead of a `Keys`. For those, `KeyPress` data is
# the character itself (that's what `self-insert` inserts);
# passing `insert_text` would type the escape sequence.
self.feed_key_callback(KeyPress(key))

def feed(self, data: str) -> None:
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_inputstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES
from prompt_toolkit.input.vt100_parser import Vt100Parser
from prompt_toolkit.keys import Keys

Expand Down Expand Up @@ -125,6 +126,20 @@ def test_invalid(processor, stream):
assert processor.keys[2].key == "*"


def test_character_valued_sequence(processor, stream, monkeypatch):
# A sequence can be mapped to a plain character instead of a `Keys`. What
# gets inserted is the character, not the sequence that produced it.
sequence = "\x1b[27;2;78~"
monkeypatch.setitem(ANSI_SEQUENCES, sequence, "N")

stream.feed(sequence)
stream.flush()

assert len(processor.keys) == 1
assert processor.keys[0].key == "N"
assert processor.keys[0].data == "N"


def test_cpr_response(processor, stream):
stream.feed("a\x1b[40;10Rb")
assert len(processor.keys) == 3
Expand Down
Loading