Skip to content

Commit 68866e6

Browse files
committed
Widgets: Add ignore_any param to execute_binding
1 parent 1eeea3b commit 68866e6

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

pytermgui/widgets/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def unbind(self, key: str) -> None:
575575

576576
del self._bindings[key]
577577

578-
def execute_binding(self, key: Any) -> bool:
578+
def execute_binding(self, key: Any, ignore_any: bool = False) -> bool:
579579
"""Executes a binding belonging to key, when present.
580580
581581
Use this method inside custom widget `handle_keys` methods, or to run a callback
@@ -584,14 +584,15 @@ def execute_binding(self, key: Any) -> bool:
584584
Args:
585585
key: Usually a string, indexing into the `_bindings` dictionary. These are the
586586
same strings as defined in `Widget.bind`.
587+
ignore_any: If set, `keys.ANY_KEY` bindings will not be executed.
587588
588589
Returns:
589590
True if the binding was found, False otherwise. Bindings will always be
590591
executed if they are found.
591592
"""
592593

593594
# Execute special binding
594-
if keys.ANY_KEY in self._bindings:
595+
if not ignore_any and keys.ANY_KEY in self._bindings:
595596
method, _ = self._bindings[keys.ANY_KEY]
596597
method(self, key)
597598

pytermgui/widgets/containers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def _handle_scrolling() -> bool:
815815

816816
return handled
817817

818-
def execute_binding(self, key: str) -> bool:
818+
def execute_binding(self, key: Any, ignore_any: bool = False) -> bool:
819819
"""Executes a binding on self, and then on self._widgets.
820820
821821
If a widget.execute_binding call returns True this function will too. Note
@@ -824,12 +824,13 @@ def execute_binding(self, key: str) -> bool:
824824
825825
Args:
826826
key: The binding key.
827+
ignore_any: If set, `keys.ANY_KEY` bindings will not be executed.
827828
828829
Returns:
829830
True if any widget returned True, False otherwise.
830831
"""
831832

832-
if super().execute_binding(key):
833+
if super().execute_binding(key, ignore_any=ignore_any):
833834
return True
834835

835836
selectables_index = 0

pytermgui/widgets/input_field.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def handle_key( # pylint: disable=too-many-return-statements, too-many-branches
248248
) -> bool:
249249
"""Adds text to the field, or moves the cursor."""
250250

251-
if self.execute_binding(key):
251+
if self.execute_binding(key, ignore_any=True):
252252
return True
253253

254254
for name, options in self.keys.items():
@@ -287,6 +287,10 @@ def handle_key( # pylint: disable=too-many-return-statements, too-many-branches
287287
else:
288288
self.insert_text(key)
289289

290+
if keys.ANY_KEY in self._bindings:
291+
method, _ = self._bindings[keys.ANY_KEY]
292+
method(self, key)
293+
290294
return True
291295

292296
if key == keys.BACKSPACE:

0 commit comments

Comments
 (0)