diff --git a/meshterm/ui/tui/session.py b/meshterm/ui/tui/session.py index 3aa4b0d..eef50f0 100644 --- a/meshterm/ui/tui/session.py +++ b/meshterm/ui/tui/session.py @@ -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) diff --git a/tests/test_tui.py b/tests/test_tui.py index 41944b3..48f9475 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -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