fix(rawcli): Ctrl-/ key binding crashed launch ("Invalid key: c-/")

prompt_toolkit doesn't accept "c-/". Ctrl-/ is emitted as Ctrl-_ (0x1F)
by terminals, so bind "c-_" (plus "c-t" as an always-available fallback)
for the hex/binary toggle. install_key_bindings() now ignores any key
name a prompt_toolkit build rejects, so a bad key can never crash launch.
Regression test added.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
michael
2026-07-14 12:25:36 -07:00
parent ae3fd3d4ea
commit 3e7f47a740
2 changed files with 30 additions and 6 deletions

View File

@@ -28,18 +28,32 @@ def byte_space(text: str, mode: str = HEX) -> str:
return " ".join(compact[i:i + n] for i in range(0, len(compact), n))
def install_key_bindings(kb, session) -> None:
"""Wire Ctrl-/ (toggle entry mode) and live auto-byte-spacing of digit input onto ``kb``.
#: keys that toggle entry mode. Ctrl-/ is emitted as Ctrl-_ (0x1F) by most terminals; c-t is an
#: always-available fallback that power users can rely on regardless of terminal.
TOGGLE_KEYS = ("c-_", "c-t")
``session`` is the :class:`~pm3py.cli.rawcli.session.RawSession` whose ``entry_mode`` is
toggled and read."""
def _bind(kb, key, handler) -> None:
"""Add a binding, ignoring key names a given prompt_toolkit build doesn't accept."""
try:
kb.add(key)(handler)
except ValueError:
pass
def install_key_bindings(kb, session) -> None:
"""Wire the entry-mode toggle (Ctrl-/ , via Ctrl-_ , plus Ctrl-t) and live auto-byte-spacing
of digit input onto ``kb``. ``session`` is the :class:`RawSession` whose ``entry_mode`` is
toggled and read. Never raises on an unknown key name."""
from prompt_toolkit.document import Document
@kb.add("c-/")
def _toggle(event):
session.toggle_entry_mode()
event.app.invalidate() # redraw the breadcrumb
for key in TOGGLE_KEYS:
_bind(kb, key, _toggle)
def _regroup_after(event):
buf = event.current_buffer
buf.insert_text(event.data)
@@ -49,4 +63,4 @@ def install_key_bindings(kb, session) -> None:
buf.document = Document(grouped, len(grouped))
for ch in sorted(_DIGITS[HEX] | _DIGITS[BIN]):
kb.add(ch)(_regroup_after)
_bind(kb, ch, _regroup_after)