Skip to content

Commit

Permalink
_character_to_key(): Fix comment and improve docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Random User committed Oct 16, 2023
1 parent e934f9d commit f8d3f8f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/textual/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,17 @@ 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():
try:
key = unicodedata.name(character)
except (TypeError, ValueError):
# ValueError: `character` has no name
# TypeError: `character` is not a unicode character (e.g. a control
# character like r"\r")
# 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(" ", "_")
Expand Down

0 comments on commit f8d3f8f

Please sign in to comment.