From cc4ccbef3128723bad4781ed1c3bb50e56125b61 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:14:28 -0600 Subject: [PATCH 1/2] input(fix[chord]): Parse a modified plus why: Splitting a chord on every plus sign loses the literal key. what: - Treat a trailing plus as a literal key after named modifiers. - Keep invalid and extra separators rejected. - Check parsed events and their encoded bytes. Fixes #126 --- docs/topics/protocols-and-input.md | 2 ++ src/libvt/input.py | 9 +++++++-- tests/test_input.py | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/docs/topics/protocols-and-input.md b/docs/topics/protocols-and-input.md index 31b497d..4a1d36c 100644 --- a/docs/topics/protocols-and-input.md +++ b/docs/topics/protocols-and-input.md @@ -115,6 +115,8 @@ b'\x1b[27;5;59~' >>> term.feed(b"\x1b[>5u") >>> term.encode(KeyEvent.parse("ctrl+shift+1")) b'\x1b[49:33;6u' +>>> term.encode(KeyEvent.parse("ctrl++")) +b'\x1b[43;5u' ``` ## Paste encoding diff --git a/src/libvt/input.py b/src/libvt/input.py index ecb16f6..f0a6a42 100644 --- a/src/libvt/input.py +++ b/src/libvt/input.py @@ -200,7 +200,8 @@ def parse(cls, chord: str, *, action: KeyAction = KeyAction.PRESS) -> KeyEvent: ---------- chord : str ``+``-separated modifier and key specification (e.g. ``"ctrl+c"``, - ``"ctrl+shift+1"``, ``"up"``). + ``"ctrl+shift+1"``, ``"up"``). Use ``"+"`` for the plus key and + ``"ctrl++"`` for a modified plus. action : KeyAction, default KeyAction.PRESS Key press, repeat, or release action. @@ -226,10 +227,14 @@ def parse(cls, chord: str, *, action: KeyAction = KeyAction.PRESS) -> KeyEvent: >>> ev = KeyEvent.parse("ctrl+shift+1") >>> (ev.key, ev.shifted_key) ('1', '!') + >>> KeyEvent.parse("alt+ctrl++").key + '+' """ if chord == "+": return cls(key="+", action=action) - parts = chord.split("+") + parts = ( + [*chord[:-2].split("+"), "+"] if chord.endswith("++") else chord.split("+") + ) mods = KeyMod.NONE for raw_mod in parts[:-1]: m = _MOD_NAMES.get(raw_mod.strip().lower()) diff --git a/tests/test_input.py b/tests/test_input.py index a21da22..50c07b5 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -23,6 +23,31 @@ from libvt.modes import Mode, ModeState +@pytest.mark.parametrize( + ("chord", "modifiers", "wire"), + [ + ("+", KeyMod.NONE, b"\x1b[43u"), + ("ctrl++", KeyMod.CTRL, b"\x1b[43;5u"), + ("alt++", KeyMod.ALT, b"\x1b[43;3u"), + ("Ctrl+alt++", KeyMod.CTRL | KeyMod.ALT, b"\x1b[43;7u"), + ], +) +def test_parse_literal_plus(chord: str, modifiers: KeyMod, wire: bytes) -> None: + """A trailing plus is the key after any named modifiers.""" + term = libvt.Terminal() + term.feed(b"\x1b[>8u") + event = KeyEvent.parse(chord) + assert event == KeyEvent("+", modifiers) + assert term.encode(event) == wire + + +@pytest.mark.parametrize("chord", ["++", "ctrl+++", "ctrl++a", "other++"]) +def test_parse_rejects_ambiguous_plus_chords(chord: str) -> None: + """Extra separators and unknown modifier names still raise.""" + with pytest.raises(ValueError, match="unknown key modifier"): + KeyEvent.parse(chord) + + def test_kitty_keyboard_stack_mouse_sgr_and_focus_encoding() -> None: """``KittyKeyboardStack`` isolates ``CSI u`` from ``SCORC`` and encodes input.""" term = libvt.Terminal(rows=4, cols=20) From a4770aeea9be8368f3b4f40fd8055bf1af304cdf Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:55:36 -0600 Subject: [PATCH 2/2] CHANGES(docs): Record modified plus why: Readers need the behavior and scope of this change at release. what: - Add the unreleased entry for PR #187 - Preserve previously published notes and the parent branch's entries --- CHANGES | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES b/CHANGES index ca1c339..31e3bb2 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,13 @@ _Notes on the upcoming release will go here._ +### Fixes + +#### Parse chords with a literal plus key (#187) + +{meth}`~libvt.input.KeyEvent.parse` accepts `"ctrl++"` and other chords that +end with a literal plus key. + ## libvt 0.0.1a5 (2026-11-30) `libvt` 0.0.1a5 adds rectangular grid blitting, display-column text writes, and