Use keys.py utility functions consistently.

This commit is contained in:
Rodrigo Girão Serrão
2023-03-02 17:32:23 +00:00
parent 7acacf0746
commit b88e4b08b4
3 changed files with 16 additions and 20 deletions

View File

@@ -8,7 +8,7 @@ from . import events, messages
from ._ansi_sequences import ANSI_SEQUENCES_KEYS
from ._parser import Awaitable, Parser, TokenCallback
from ._types import MessageTarget
from .keys import KEY_NAME_REPLACEMENTS
from .keys import KEY_NAME_REPLACEMENTS, _character_to_key
# When trying to determine whether the current sequence is a supported/valid
# escape sequence, at which length should we give up and consider our search
@@ -253,12 +253,7 @@ class XTermParser(Parser[events.Event]):
elif len(sequence) == 1:
try:
if not sequence.isalnum():
name = (
_unicode_name(sequence)
.lower()
.replace("-", "_")
.replace(" ", "_")
)
name = _character_to_key(sequence)
else:
name = sequence
name = KEY_NAME_REPLACEMENTS.get(name, name)

View File

@@ -70,7 +70,12 @@ from .features import FeatureFlag, parse_features
from .file_monitor import FileMonitor
from .filter import LineFilter, Monochrome
from .geometry import Offset, Region, Size
from .keys import REPLACED_KEYS, _get_key_display
from .keys import (
REPLACED_KEYS,
_character_to_key,
_get_key_display,
_get_unicode_name_from_key,
)
from .messages import CallbackType
from .reactive import Reactive
from .renderables.blank import Blank
@@ -864,19 +869,15 @@ class App(Generic[ReturnType], DOMNode):
await asyncio.sleep(float(wait_ms) / 1000)
else:
if len(key) == 1 and not key.isalnum():
key = (
unicodedata.name(key)
.lower()
.replace("-", "_")
.replace(" ", "_")
)
key = _character_to_key(key)
original_key = REPLACED_KEYS.get(key, key)
print(f"original key is {original_key}")
char: str | None
try:
char = unicodedata.lookup(original_key.upper().replace("_", " "))
char = unicodedata.lookup(_get_unicode_name_from_key(original_key))
except KeyError:
char = key if len(key) == 1 else None
print(f"press {key!r} (char={char!r})")
print(f"char is {char!r}")
key_event = events.Key(app, key, char)
driver.send_event(key_event)
await wait_for_idle(0)

View File

@@ -286,17 +286,17 @@ def _get_key_display(key: str) -> str:
return display_alias
original_key = REPLACED_KEYS.get(key, key)
upper_original = original_key.upper().replace("_", " ")
tentative_unicode_name = _get_unicode_name_from_key(original_key)
try:
unicode_character = unicodedata.lookup(upper_original)
unicode_character = unicodedata.lookup(tentative_unicode_name)
except KeyError:
return upper_original
return tentative_unicode_name
# Check if printable. `delete` for example maps to a control sequence
# which we don't want to write to the terminal.
if unicode_character.isprintable():
return unicode_character
return upper_original
return tentative_unicode_name
def _character_to_key(character: str) -> str: