diff --git a/CHANGELOG.md b/CHANGELOG.md index 42111addd..a8a4fa91e 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 85406b4e9..c00c4d6ce 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/examples/five_by_five.py b/examples/five_by_five.py index 3e668f04d..a0410ba6e 100644 --- a/examples/five_by_five.py +++ b/examples/five_by_five.py @@ -166,10 +166,10 @@ class Game(Screen): Binding("n", "new_game", "New Game"), Binding("question_mark", "push_screen('help')", "Help", key_display="?"), Binding("q", "quit", "Quit"), - Binding("up,w,k", "navigate(-1,0)", "Move Up", False), - Binding("down,s,j", "navigate(1,0)", "Move Down", False), - Binding("left,a,h", "navigate(0,-1)", "Move Left", False), - Binding("right,d,l", "navigate(0,1)", "Move Right", False), + Binding("up,w,k", "navigate(-1,0)", "Move Up", False, universal=True), + Binding("down,s,j", "navigate(1,0)", "Move Down", False, universal=True), + Binding("left,a,h", "navigate(0,-1)", "Move Left", False, universal=True), + Binding("right,d,l", "navigate(0,1)", "Move Right", False, universal=True), Binding("space", "move", "Toggle", False), ] """The bindings for the main game grid.""" diff --git a/src/textual/_typing.py b/src/textual/_typing.py index 6532b9ba5..340137d3e 100644 --- a/src/textual/_typing.py +++ b/src/textual/_typing.py @@ -9,3 +9,5 @@ if sys.version_info >= (3, 8): from typing import Final, Literal, Protocol, TypedDict else: from typing_extensions import Final, Literal, Protocol, TypedDict + +__all__ = ["TypeAlias", "Final", "Literal", "Protocol", "TypedDict"] diff --git a/src/textual/binding.py b/src/textual/binding.py index 63a150252..7b34f12f0 100644 --- a/src/textual/binding.py +++ b/src/textual/binding.py @@ -93,7 +93,7 @@ class Bindings: @classmethod def merge(cls, bindings: Iterable[Bindings]) -> Bindings: - """Merge a bindings. Subsequence bound keys override initial keys. + """Merge a bindings. Subsequent bound keys override initial keys. Args: bindings (Iterable[Bindings]): A number of bindings. diff --git a/src/textual/keys.py b/src/textual/keys.py index aac14c138..25fc148df 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 f3cf9975b..1a38659fb 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