diff --git a/CHANGELOG.md b/CHANGELOG.md index 42111addd3..a8a4fa91ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Fixed + +- Ensure only printable characters are used as key_display https://github.com/Textualize/textual/pull/1361 ## [0.6.0] - 2022-12-11 diff --git a/docs/examples/widgets/footer.py b/docs/examples/widgets/footer.py index 85406b4e9f..c00c4d6cef 100644 --- a/docs/examples/widgets/footer.py +++ b/docs/examples/widgets/footer.py @@ -12,6 +12,7 @@ class FooterApp(App): description="Show help screen", key_display="?", ), + Binding(key="delete", action="delete", description="Delete the thing"), Binding(key="j", action="down", description="Scroll down", show=False), ] diff --git a/src/textual/keys.py b/src/textual/keys.py index aac14c138e..25fc148df1 100644 --- a/src/textual/keys.py +++ b/src/textual/keys.py @@ -245,9 +245,14 @@ def _get_key_display(key: str) -> str: return display_alias original_key = REPLACED_KEYS.get(key, key) + upper_original = original_key.upper().replace("_", " ") try: - unicode_character = unicodedata.lookup(original_key.upper().replace("_", " ")) + unicode_character = unicodedata.lookup(upper_original) except KeyError: - return original_key.upper() + return upper_original - return unicode_character + # 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 diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index f3cf9975b3..1a38659fbd 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -5104,134 +5104,134 @@ font-weight: 700; } - .terminal-1908614482-matrix { + .terminal-1971839132-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-1908614482-title { + .terminal-1971839132-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-1908614482-r1 { fill: #e1e1e1 } - .terminal-1908614482-r2 { fill: #c5c8c6 } - .terminal-1908614482-r3 { fill: #dde8f3;font-weight: bold } - .terminal-1908614482-r4 { fill: #ddedf9 } + .terminal-1971839132-r1 { fill: #e1e1e1 } + .terminal-1971839132-r2 { fill: #c5c8c6 } + .terminal-1971839132-r3 { fill: #dde8f3;font-weight: bold } + .terminal-1971839132-r4 { fill: #ddedf9 } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - FooterApp + FooterApp - - - - - - - - - - - - - - - - - - - - - - - - - - -  Q  Quit the app  ?  Show help screen  + + + + + + + + + + + + + + + + + + + + + + + + + + +  Q  Quit the app  ?  Show help screen  DELETE  Delete the thing