Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ parser state.
The HTML formatter hides the glyph and its decorations for `SGR 8` while
preserving the cell background and occupied width.

#### 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.

### Development

#### Design review and issue skills (#155)
Expand Down
2 changes: 2 additions & 0 deletions docs/topics/protocols-and-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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
Expand Down
9 changes: 7 additions & 2 deletions src/libvt/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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())
Expand Down
25 changes: 25 additions & 0 deletions tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading