Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring #3406

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/textual/_xterm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ def reissue_sequence_as_keys(reissue_sequence: str) -> None:
for event in sequence_to_key_events(character):
on_token(event)

def _sequence_to_key_events(
self, sequence: str, _unicode_name=unicodedata.name
) -> Iterable[events.Key]:
def _sequence_to_key_events(self, sequence: str) -> Iterable[events.Key]:
"""Map a sequence of code points on to a sequence of keys.

Args:
Expand All @@ -237,12 +235,5 @@ def _sequence_to_key_events(
for key in keys:
yield events.Key(key.value, sequence if len(sequence) == 1 else None)
elif len(sequence) == 1:
try:
if not sequence.isalnum():
name = _character_to_key(sequence)
else:
name = sequence
name = KEY_NAME_REPLACEMENTS.get(name, name)
yield events.Key(name, sequence)
except:
yield events.Key(sequence, sequence)
name = _character_to_key(sequence)
yield events.Key(name, sequence)
12 changes: 11 additions & 1 deletion src/textual/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,20 @@ def _get_key_display(key: str) -> str:
def _character_to_key(character: str) -> str:
"""Convert a single character to a key value.

If `character` has no key value, return it unchanged.

This transformation can be undone by the function `_get_unicode_name_from_key`.
"""
if not character.isalnum():
key = unicodedata.name(character).lower().replace("-", "_").replace(" ", "_")
try:
key = unicodedata.name(character)
except (TypeError, ValueError):
# ValueError: `character` has no name. Control characters, for
# example, have no name: https://stackoverflow.com/questions/70074668
# TypeError: `character` is not a single character.
key = character
else:
key = key.lower().replace("-", "_").replace(" ", "_")
else:
key = character
key = KEY_NAME_REPLACEMENTS.get(key, key)
Expand Down
1 change: 1 addition & 0 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
("?", "question_mark"),
("£", "pound_sign"),
(",", "comma"),
(r"\t", r"\t"),
],
)
def test_character_to_key(character: str, key: str) -> None:
Expand Down