From 0feaae5ec997630a49ccf0a2b25f4993bfd63626 Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Mon, 22 Oct 2018 19:16:07 +0200 Subject: [PATCH 1/2] Support page-down/up in RadioList --- prompt_toolkit/widgets/base.py | 36 +++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/prompt_toolkit/widgets/base.py b/prompt_toolkit/widgets/base.py index 573290b07..192e59bd4 100644 --- a/prompt_toolkit/widgets/base.py +++ b/prompt_toolkit/widgets/base.py @@ -566,9 +566,11 @@ def __init__(self, values): assert all(isinstance(i, tuple) and len(i) == 2 for i in values) + self.all_values = values self.values = values self.current_value = values[0][0] self._selected_index = 0 + self.search = "" # Key bindings. kb = KeyBindings() @@ -601,15 +603,24 @@ def _(event): @kb.add('enter') @kb.add(' ') def _(event): - self.current_value = self.values[self._selected_index][0] + if self.values: + self.current_value = self.values[self._selected_index][0] @kb.add(Keys.Any) def _(event): - # We first check values after the selected value, then all values. - for value in self.values[self._selected_index + 1:] + self.values: - if value[1].startswith(event.data): - self._selected_index = self.values.index(value) - return + # Let's check for Delete first + if event.key_sequence[-1].key == 'c-h': + self.search = self.search[:-1] + else: + self.search += event.data + self.values = [] + index = None + for value in self.all_values: + if self.search in value[1]: + self.values.append(value) + if index is None: + index = self.values.index(value) + self._selected_index = index or 0 # Control and window. self.control = FormattedTextControl( @@ -649,7 +660,18 @@ def _get_text_fragments(self): result.append((style, ')')) result.append(('class:radio', ' ')) - result.extend(to_formatted_text(value[1], style='class:radio')) + display_value = value[1] + if self.search and self.search in display_value: + _parts = display_value.partition(self.search) + result.extend(to_formatted_text(_parts[0], style='class:radio')) + result.append(('bg:red', _parts[1])) + result.extend(to_formatted_text(_parts[2], style='class:radio')) + else: + result.extend(to_formatted_text(display_value, style='class:radio')) + result.append(('', '\n')) + + if not result: + result.append(('', " > No item found for '%s'" % self.search)) result.append(('', '\n')) result.pop() # Remove last newline. From bf91e0c6fbb1870e20fafcd7732ab1ddf397c084 Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Mon, 22 Oct 2018 19:24:26 +0200 Subject: [PATCH 2/2] Add escape key-binding to exit shortcuts dialogs --- prompt_toolkit/shortcuts/dialogs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/prompt_toolkit/shortcuts/dialogs.py b/prompt_toolkit/shortcuts/dialogs.py index ee824298b..4de4fa1c4 100644 --- a/prompt_toolkit/shortcuts/dialogs.py +++ b/prompt_toolkit/shortcuts/dialogs.py @@ -201,6 +201,7 @@ def _create_app(dialog, style): bindings = KeyBindings() bindings.add('tab')(focus_next) bindings.add('s-tab')(focus_previous) + bindings.add('escape')(lambda x: _return_none()) return Application( layout=Layout(dialog),