Fix comma separated string in "Bindings" (#797)

This commit is contained in:
darrenburns
2022-10-04 18:37:06 +01:00
committed by GitHub
parent 239151f171
commit 54bdb4bac0
3 changed files with 49 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ from textual.widgets import Static, Footer
class JustABox(App): class JustABox(App):
BINDINGS = [ BINDINGS = [
Binding(key="t", action="text_fade_out", description="text-opacity fade out"), Binding(key="t", action="text_fade_out", description="text-opacity fade out"),
Binding(key="o", action="widget_fade_out", description="opacity fade out"), Binding(key="o,f,w", action="widget_fade_out", description="opacity fade out"),
] ]
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:

View File

@@ -1,17 +1,16 @@
from __future__ import annotations from __future__ import annotations
import sys import sys
import rich.repr
from dataclasses import dataclass from dataclasses import dataclass
from typing import Iterable, MutableMapping from typing import Iterable, MutableMapping
import rich.repr
if sys.version_info >= (3, 10): if sys.version_info >= (3, 10):
from typing import TypeAlias from typing import TypeAlias
else: # pragma: no cover else: # pragma: no cover
from typing_extensions import TypeAlias from typing_extensions import TypeAlias
BindingType: TypeAlias = "Binding | tuple[str, str, str]" BindingType: TypeAlias = "Binding | tuple[str, str, str]"
@@ -23,7 +22,7 @@ class NoBinding(Exception):
"""A binding was not found.""" """A binding was not found."""
@dataclass @dataclass(frozen=True)
class Binding: class Binding:
key: str key: str
"""Key to bind.""" """Key to bind."""
@@ -47,7 +46,20 @@ class Bindings:
def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]: def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
for binding in bindings: for binding in bindings:
if isinstance(binding, Binding): if isinstance(binding, Binding):
yield binding binding_keys = binding.key.split(",")
if len(binding_keys) > 1:
for key in binding_keys:
new_binding = Binding(
key=key,
action=binding.action,
description=binding.description,
show=binding.show,
key_display=binding.key_display,
allow_forward=binding.allow_forward,
)
yield new_binding
else:
yield binding
else: else:
if len(binding) != 3: if len(binding) != 3:
raise BindingError( raise BindingError(

31
tests/test_binding.py Normal file
View File

@@ -0,0 +1,31 @@
import pytest
from textual.binding import Bindings, Binding
BINDING1 = Binding("a,b", action="action1", description="description1")
BINDING2 = Binding("c", action="action2", description="description2")
@pytest.fixture
def bindings():
yield Bindings([BINDING1, BINDING2])
def test_bindings_get_key(bindings):
assert bindings.get_key("b") == Binding("b", action="action1", description="description1")
assert bindings.get_key("c") == BINDING2
def test_bindings_merge_simple(bindings):
left = Bindings([BINDING1])
right = Bindings([BINDING2])
assert Bindings.merge([left, right]).keys == bindings.keys
def test_bindings_merge_overlap():
left = Bindings([BINDING1])
another_binding = Binding("a", action="another_action", description="another_description")
assert Bindings.merge([left, Bindings([another_binding])]).keys == {
"a": another_binding,
"b": Binding("b", action="action1", description="description1"),
}