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

Added a search feature for List() #524

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
25 changes: 25 additions & 0 deletions examples/list_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import sys
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer # noqa


# To make the search case-insensitive
def matcher(entry, search):
return entry.lower().startswith(search.lower())


questions = [
inquirer.List(
"size", message="What size do you need?",
choices=["Jumbo", "Large", "Standard"],
carousel=True, search=True, matcher=matcher
),
]

answers = inquirer.prompt(questions)

pprint(answers)
4 changes: 4 additions & 0 deletions src/inquirer/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ def __init__(
carousel=False,
other=False,
autocomplete=None,
search=False,
matcher=None
):
super().__init__(name, message, choices, default, ignore, validate, hints=hints, other=other)
self.carousel = carousel
self.autocomplete = autocomplete
self.search = search
self.matcher = matcher


class Checkbox(Question):
Expand Down
16 changes: 16 additions & 0 deletions src/inquirer/render/console/_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ class List(BaseConsoleRender):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current = self._current_index()
self.input = ""

@property
def is_long(self):
choices = self.question.choices or []
return len(choices) >= MAX_OPTIONS_DISPLAYED_AT_ONCE

def get_current_value(self):
return self.input if self.question.search else ""

def get_hint(self):
try:
choice = self.question.choices[self.current]
Expand Down Expand Up @@ -90,6 +94,18 @@ def process_input(self, pressed):

raise errors.EndOfInput(getattr(value, "value", value))

if self.question.matcher is not None:
if pressed.isprintable():
self.input += pressed
for choice in self.question.choices:
if self.question.matcher(choice, self.input):
self.current = self.question.choices.index(choice)
break

# BACKSPACE gives a \x7f on linux while it gives a \x08 on windows
if pressed == chr(127) or pressed == chr(8):
self.input = self.input[:-1]

if pressed == key.CTRL_C:
raise KeyboardInterrupt()

Expand Down
30 changes: 30 additions & 0 deletions tests/integration/console_render/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ def test_ctrl_c_breaks_execution(self):
with pytest.raises(KeyboardInterrupt):
sut.render(question)

def test_type_char(self):
stdin = helper.event_factory('b', 'A', 'z', key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

# To make the search case-insensitive
def matcher(entry, search):
return entry.lower().startswith(search.lower())

question = questions.List(variable, message, choices=choices, carousel=True, matcher=matcher)

sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)

assert result == "bazz"

def test_type_char_without_matcher(self):
stdin = helper.event_factory('b', 'A', 'z', key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

question = questions.List(variable, message, choices=choices, carousel=True)

sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)

assert result == "foo"

def test_first_hint_is_shown(self):
stdin = helper.event_factory(key.ENTER)
message = "Foo message"
Expand Down
Loading