Skip to content

Commit 72fe01e

Browse files
Niloth-pParth576
authored andcommitted
keys: Add function to convert an urwid key into display format.
Generates a user-friendly version of Urwid's command keys. Currently this applies capitalization of special keys and custom mapping of PgUp/PgDn keys. Tests added. Co-authored-by: Parth Shah <[email protected]>
1 parent 764fb72 commit 72fe01e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

tests/config/test_keys.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,30 @@ def test_updated_urwid_command_map() -> None:
112112
assert key in keys.keys_for_command(zt_cmd)
113113
except KeyError:
114114
pass
115+
116+
117+
@pytest.mark.parametrize(
118+
"urwid_key, display_key",
119+
[
120+
("a", "a"),
121+
("B", "B"),
122+
(":", ":"),
123+
("enter", "Enter"),
124+
("meta c", "Meta c"),
125+
("ctrl D", "Ctrl D"),
126+
("page up", "PgUp"),
127+
("ctrl page up", "Ctrl PgUp"),
128+
],
129+
ids=[
130+
"lowercase_alphabet_key",
131+
"uppercase_alphabet_key",
132+
"symbol_key",
133+
"special_key",
134+
"lowercase_alphabet_key_with_modifier_key",
135+
"uppercase_alphabet_key_with_modifier_key",
136+
"mapped_key",
137+
"mapped_key_with_modifier_key",
138+
],
139+
)
140+
def test_display_key_for_urwid_key(urwid_key: str, display_key: str) -> None:
141+
assert keys.display_key_for_urwid_key(urwid_key) == display_key

zulipterminal/config/keys.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,28 @@ def primary_key_for_command(command: str) -> str:
460460
return keys_for_command(command).pop(0)
461461

462462

463+
URWID_KEY_TO_DISPLAY_KEY_MAPPING = {
464+
"page up": "PgUp",
465+
"page down": "PgDn",
466+
}
467+
468+
469+
def display_key_for_urwid_key(urwid_key: str) -> str:
470+
"""
471+
Returns a displayable user-centric format of the urwid key.
472+
"""
473+
for urwid_map_key, display_map_key in URWID_KEY_TO_DISPLAY_KEY_MAPPING.items():
474+
if urwid_map_key in urwid_key:
475+
urwid_key = urwid_key.replace(urwid_map_key, display_map_key)
476+
display_key = [
477+
keyboard_key.capitalize()
478+
if len(keyboard_key) > 1 and keyboard_key[0].islower()
479+
else keyboard_key
480+
for keyboard_key in urwid_key.split()
481+
]
482+
return " ".join(display_key)
483+
484+
463485
def commands_for_random_tips() -> List[KeyBinding]:
464486
"""
465487
Return list of commands which may be displayed as a random tip

0 commit comments

Comments
 (0)