Skip to content

Commit 4dcdea8

Browse files
authored
Merge pull request #1361 from Textualize/key-display-fixes
Ensure only printable keys added to footer
2 parents 251e756 + e01c33c commit 4dcdea8

File tree

4 files changed

+72
-61
lines changed

4 files changed

+72
-61
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Ensure only printable characters are used as key_display https://github.com/Textualize/textual/pull/1361
813

914
## [0.6.0] - 2022-12-11
1015

docs/examples/widgets/footer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class FooterApp(App):
1212
description="Show help screen",
1313
key_display="?",
1414
),
15+
Binding(key="delete", action="delete", description="Delete the thing"),
1516
Binding(key="j", action="down", description="Scroll down", show=False),
1617
]
1718

src/textual/keys.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,14 @@ def _get_key_display(key: str) -> str:
245245
return display_alias
246246

247247
original_key = REPLACED_KEYS.get(key, key)
248+
upper_original = original_key.upper().replace("_", " ")
248249
try:
249-
unicode_character = unicodedata.lookup(original_key.upper().replace("_", " "))
250+
unicode_character = unicodedata.lookup(upper_original)
250251
except KeyError:
251-
return original_key.upper()
252+
return upper_original
252253

253-
return unicode_character
254+
# Check if printable. `delete` for example maps to a control sequence
255+
# which we don't want to write to the terminal.
256+
if unicode_character.isprintable():
257+
return unicode_character
258+
return upper_original

0 commit comments

Comments
 (0)