Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key minus #1596

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/textual/cli/previews/keys.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from rich.panel import Panel

from rich.text import Text

from textual.app import App, ComposeResult
from textual.reactive import var, Reactive
from textual import events
from textual.containers import Horizontal
from textual.widgets import Button, Header, TextLog


INSTRUCTIONS = """\
Press some keys!
[u]Press some keys![/]
Because we want to display all the keys, ctrl+C won't quit this example. Use the Quit button below to exit the app.\
To quit the app press [b]ctrl+c[/b] [i]twice[i] or press the Quit button below.\
"""


Expand All @@ -32,6 +35,8 @@ class KeysApp(App, inherit_bindings=False):
}
"""

last_key: Reactive[str | None] = var(None)

def compose(self) -> ComposeResult:
yield Header()
yield Horizontal(
Expand All @@ -42,10 +47,13 @@ def compose(self) -> ComposeResult:
yield KeyLog()

def on_ready(self) -> None:
self.query_one(KeyLog).write(Panel(INSTRUCTIONS), expand=True)
self.query_one(KeyLog).write(Panel(Text.from_markup(INSTRUCTIONS)), expand=True)

def on_key(self, event: events.Key) -> None:
self.query_one(KeyLog).write(event)
if event.key == "ctrl+c" and self.last_key == "ctrl+c":
self.exit()
self.last_key = event.key

def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "quit":
Expand Down
2 changes: 1 addition & 1 deletion src/textual/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ._types import MessageTarget
from .geometry import Offset, Size
from .keys import _get_key_aliases
from .keys import _get_key_aliases, _get_key_display
from .message import Message

MouseEventT = TypeVar("MouseEventT", bound="MouseEvent")
Expand Down
1 change: 1 addition & 0 deletions src/textual/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Keys(str, Enum):
"backspace": "⌫",
"escape": "ESC",
"enter": "⏎",
"minus": "-",
}


Expand Down
6 changes: 5 additions & 1 deletion tests/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from textual.app import App
from textual.keys import _character_to_key
from textual.keys import _character_to_key, _get_key_display


@pytest.mark.parametrize(
Expand Down Expand Up @@ -48,3 +48,7 @@ def action_increment(self) -> None:
await pilot.press("x")
await pilot.pause()
assert counter == 3


def test_get_key_display():
assert _get_key_display("minus") == "-"