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 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
33 changes: 33 additions & 0 deletions examples/list_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import sys
from pprint import pprint


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


# To make the search case-insensitive
def matcher(choices, pressedKey, searchString):
if pressedKey == key.BACKSPACE:
searchString = searchString[:-1]
elif pressedKey.isprintable():
searchString += pressedKey
for i in range(len(choices)):
if choices[i].lower().startswith(searchString.lower()):
return (i, searchString)
return (0, searchString)


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

answers = inquirer.prompt(questions)

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


class Checkbox(Question):
Expand Down
9 changes: 9 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.search = ""

@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.search

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

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

if self.question.matcher is not None:
(index, search) = self.question.matcher(self.question.choices, pressed, self.search)
self.search = search
self.current = index

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

Expand Down
24 changes: 24 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,30 @@ 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(choices, pressedKey, searchString):
if pressedKey == key.BACKSPACE:
searchString = searchString[:-1]
elif pressedKey.isprintable():
searchString += pressedKey
for i in range(len(choices)):
if choices[i].lower().startswith(searchString.lower()):
return (i, searchString)
return (0, searchString)

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_first_hint_is_shown(self):
stdin = helper.event_factory(key.ENTER)
message = "Foo message"
Expand Down