Key dispatch: the right-Ctrl rescue reaches the ^R/^P letter chords

The physical-key probe promoted only the eight navigation chords, so a layout-claimed right Ctrl (Canadian Multilingual Standard, which strips the ctrl flag) still could not reach the letter shortcuts: ^R (chat retry, atlas rebuild) and ^P (chat paths). A bare r/p typed while the right Ctrl is physically held now promotes to retry/paths via _CTRL_LETTER_CHORDS. It is safe because a genuine third-level character arrives as a different glyph and is never in the map; a layout that swallows the key makes this a harmless no-op.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-19 12:19:29 -04:00
1 parent 69526e76e0
commit 24273f1e00
2 files changed
+45 -4

No files matched your search

+19 -4
View File
@@ -88,6 +88,18 @@ _CTRL_CHORDS: dict[str, str] = {
"pagedown": "ctrl_pagedown",
}
#: The letter Ctrl chords (the ``Keys.Control*`` letter entries in :data:`_KEY_ACTIONS`),
#: keyed by the bare lowercase letter a layout-claimed right Ctrl delivers as *text* instead.
#: Same right-Ctrl rescue as :data:`_CTRL_CHORDS`, but promoting a typed letter rather than a
#: navigation key — a bare ``r``/``p`` only reaches text while the right Ctrl is physically
#: held when the layout has no third-level glyph for that key (i.e. it really is the chord); a
#: genuine third-level character arrives as some *other* glyph and is never in this map. Keep
#: in step with the ``Keys.ControlR``/``Keys.ControlP`` entries above.
_CTRL_LETTER_CHORDS: dict[str, str] = {
"r": "retry",
"p": "paths",
}
def _right_ctrl_down() -> bool:
"""Whether the right Ctrl key is physically held right now (Windows; ``False`` elsewhere).
@@ -1143,10 +1155,11 @@ class TuiSession:
def _dispatch(self, action: str, data: str = "") -> None:
"""Forward an action to the top screen and repaint.
A plain navigation action arriving while the right Ctrl key is physically held is
promoted to its Ctrl chord first (see :func:`_right_ctrl_down`) — a no-op when the
console already reported the chord, and the rescue when a layout-claimed right Ctrl
stripped it.
A plain navigation key — or a bare ``r``/``p`` typed as text — arriving while the right
Ctrl key is physically held is promoted to its Ctrl chord first (see
:func:`_right_ctrl_down`, :data:`_CTRL_CHORDS`, :data:`_CTRL_LETTER_CHORDS`) — a no-op
when the console already reported the chord, and the rescue when a layout-claimed right
Ctrl stripped it.
The repaint keeps prompt_toolkit's fast differential paint; a frame carrying a glyph
the terminal may draw narrower than pt reserves for it (an emoji) upgrades itself to a
@@ -1154,6 +1167,8 @@ class TuiSession:
"""
if action in _CTRL_CHORDS and _right_ctrl_down():
action = _CTRL_CHORDS[action]
elif action == "text" and data.lower() in _CTRL_LETTER_CHORDS and _right_ctrl_down():
action, data = _CTRL_LETTER_CHORDS[data.lower()], ""
top = self.top
if top is not None:
top.handle(action, data)
+26
View File
@@ -1531,6 +1531,32 @@ def test_dispatch_promotes_nav_actions_while_right_ctrl_is_held(monkeypatch) ->
assert seen == ["ctrl_left", "ctrl_home", "enter", "left"]
def test_dispatch_promotes_letter_chords_while_right_ctrl_is_held(monkeypatch) -> None:
"""A bare letter typed while the physical right Ctrl is down becomes its Ctrl-letter
chord — the same rescue as the nav keys, for the ^R/^P shortcuts a layout-claimed right
Ctrl would otherwise strip to plain text. Only the mapped letters promote (case-folded,
with the now-stale data dropped); other text — and everything once the key is released —
stays text."""
from meshterm.ui.tui import session as session_mod
session = TuiSession()
seen: list[tuple[str, str]] = []
class Probe(ScrollScreen):
def handle(self, action: str, data: str = "") -> None:
seen.append((action, data))
session.push(Probe(Text("x")))
monkeypatch.setattr(session_mod, "_right_ctrl_down", lambda: True)
session._dispatch("text", "r") # ^R retry
session._dispatch("text", "P") # ^P paths, case-folded
session._dispatch("text", "x") # unmapped letter: stays text even while held
monkeypatch.setattr(session_mod, "_right_ctrl_down", lambda: False)
session._dispatch("text", "r") # released: plain text again
assert seen == [("retry", ""), ("paths", ""), ("text", "x"), ("text", "r")]
def test_wide_glyph_detection_flags_emoji_not_marks() -> None:
"""The desync only ever comes from a width-2 glyph the terminal may draw narrower — an
emoji. Node-type marks, status marks and chart braille are width-1 everywhere, so they