Skip to content

RadioList: search #758

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions prompt_toolkit/shortcuts/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
36 changes: 29 additions & 7 deletions prompt_toolkit/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down