Skip to content

Commit d9e0258

Browse files
committed
Simplify some sequences that seem unique to rxvt
Almost(?) all other terminals I've tested, shifted numeric keypad keys just yield the unshifted version of that key. rxvt seems to have sequences for those keys. Much like Textualize#3737 and Textualize#3739 we *could* add support for these sequences to the ANSI sequence list, but this is a bit different. Whereas in those other PRs we've been adding the alternate sequences that rxvt seems to have for well known and well-supported keys, here it would be the other way round: it would be adding bindable keys that are only available on a small subset of environments. Ideally, at least for the moment, we want to encourage people to bind keys that are well-supported; so here we turn the shifted keypad keys into their unshifted counterparts, thus matching many other environments. See Textualize#3741
1 parent 4058e59 commit d9e0258

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/textual/_ansi_sequences.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import annotations
2+
13
from typing import Mapping, Tuple
24

35
from .keys import Keys
46

57
# Mapping of vt100 escape codes to Keys.
6-
ANSI_SEQUENCES_KEYS: Mapping[str, Tuple[Keys, ...]] = {
8+
ANSI_SEQUENCES_KEYS: Mapping[str, Tuple[Keys, ...] | str] = {
79
# Control keys.
810
" ": (Keys.Space,),
911
"\r": (Keys.Enter,),
@@ -326,6 +328,21 @@
326328
"\x1b[1;8w": (Keys.Escape, Keys.ControlShift7),
327329
"\x1b[1;8x": (Keys.Escape, Keys.ControlShift8),
328330
"\x1b[1;8y": (Keys.Escape, Keys.ControlShift9),
331+
# Simplify some sequences that appear to be unique to rxvt; see
332+
# https://github.com/Textualize/textual/issues/3741 for context.
333+
"\x1bOj": "*",
334+
"\x1bOk": "+",
335+
"\x1bOm": "-",
336+
"\x1bOo": "/",
337+
"\x1bOq": "1",
338+
"\x1bOr": "2",
339+
"\x1bOs": "3",
340+
"\x1bOt": "4",
341+
"\x1bOu": "5",
342+
"\x1bOv": "6",
343+
"\x1bOw": "7",
344+
"\x1bOx": "8",
345+
"\x1bOy": "9",
329346
}
330347

331348
# https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036

src/textual/_xterm_parser.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,20 @@ def _sequence_to_key_events(
243243
Keys
244244
"""
245245
keys = ANSI_SEQUENCES_KEYS.get(sequence)
246-
if keys is not None:
246+
if isinstance(keys, tuple):
247+
# If the sequence mapped to a tuple, then it's values from the
248+
# `Keys` enum. Raise key events from what we find in the tuple.
247249
for key in keys:
248250
yield events.Key(key.value, sequence if len(sequence) == 1 else None)
249-
elif len(sequence) == 1:
251+
return
252+
# If keys is a string, the intention is that it's a mapping to a
253+
# character, which should really be treated as the sequence for the
254+
# purposes of the next step...
255+
if isinstance(keys, str):
256+
sequence = keys
257+
# If the sequence is a single character, attempt to process it as a
258+
# key.
259+
if len(sequence) == 1:
250260
try:
251261
if not sequence.isalnum():
252262
name = _character_to_key(sequence)

0 commit comments

Comments
 (0)