fuzzy

A prompt that lists choices to select while also allowing fuzzy search like fzf.

Example

Note

The following example will download a sample file and demos the performance of searching with 100k words.

demo

Classic Syntax (PyInquirer)
import urllib.request
from contextlib import ExitStack
from pathlib import Path

from InquirerPy import prompt


def get_choices(_):
    p = Path(__file__).resolve().parent.joinpath("sample.txt")
    choices = []
    result = []

    with ExitStack() as stack:
        if not p.exists():
            file = stack.enter_context(p.open("w+"))
            sample = stack.enter_context(
                urllib.request.urlopen(
                    "https://assets.kazhala.me/InquirerPy/sample.txt"
                )
            )
            file.write(sample.read().decode())
            file.seek(0, 0)
        else:
            file = stack.enter_context(p.open("r"))
        for line in file.readlines():
            choices.append(line[:-1])
        for choice in choices:
            if not choice:
                continue
            result.append(choice)
    return result


def main():
    questions = [
        {
            "type": "fuzzy",
            "message": "Select actions:",
            "choices": ["hello", "weather", "what", "whoa", "hey", "yo"],
            "default": "he",
            "max_height": "70%",
        },
        {
            "type": "fuzzy",
            "message": "Select preferred words:",
            "choices": get_choices,
            "multiselect": True,
            "validate": lambda result: len(result) > 1,
            "invalid_message": "minimum 2 selection",
            "max_height": "70%",
        },
    ]

    result = prompt(questions=questions)


if __name__ == "__main__":
    main()
Alternate Syntax
import urllib.request
from contextlib import ExitStack
from pathlib import Path

from InquirerPy import inquirer


def get_choices(_):
    p = Path(__file__).resolve().parent.joinpath("sample.txt")
    choices = []
    result = []

    with ExitStack() as stack:
        if not p.exists():
            file = stack.enter_context(p.open("w+"))
            sample = stack.enter_context(
                urllib.request.urlopen(
                    "https://assets.kazhala.me/InquirerPy/sample.txt"
                )
            )
            file.write(sample.read().decode())
            file.seek(0, 0)
        else:
            file = stack.enter_context(p.open("r"))
        for line in file.readlines():
            choices.append(line[:-1])
        for choice in choices:
            if not choice:
                continue
            result.append(choice)
    return result


def main():
    action = inquirer.fuzzy(
        message="Select actions:",
        choices=["hello", "weather", "what", "whoa", "hey", "yo"],
        default="he",
    ).execute()
    words = inquirer.fuzzy(
        message="Select preferred words:",
        choices=get_choices,
        multiselect=True,
        validate=lambda result: len(result) > 1,
        invalid_message="minimum 2 selections",
        max_height="70%",
    ).execute()


if __name__ == "__main__":
    main()

Choices

See also

choices

Attention

This prompt does not accepts choices containing Separator instances.

Keybindings

See also

keybindings

Hint

This prompt does not enable j/k navigation when vi_mode is True. When vi_mode is True in fuzzy prompt, the input buffer will become vim input mode, no other keybindings are altered.

The space key for toggle choice is also disabled since it blocks user from typing space in the input buffer.

{
    "answer": [{"key": "enter"}],   # answer the prompt
    "interrupt": [{"key": "c-c"}],  # raise KeyboardInterrupt
    "skip": [{"key": "c-z"}],   # skip the prompt
}

The following dictionary contains the additional keybindings created by this prompt.

{
    "down": [
        {"key": "down"},
        {"key": "c-n"},   # move down
    ],
    "up": [
        {"key": "up"},
        {"key": "c-p"},   # move up
    ],
    "toggle": [
        {"key": "space"},   # toggle choices
    ],
    "toggle-down": [
        {"key": "c-i"},   # toggle choice and move down (tab)
    ],
    "toggle-up": [
        {"key": "s-tab"},   # toggle choice and move up (shift+tab)
    ],
    "toggle-all": [
        {"key": "alt-r"},   # toggle all choices
        {"key": "c-r"},
    ],
    "toggle-all-true": [
        {"key": "alt-a"},   # toggle all choices true
        {"key": "c-a"}.
    ],
    "toggle-all-false": [],   # toggle all choices false
}

In addition, with the release of 0.3.2, you can now also toggle string matching algorithm.

{
    "toggle-exact": []  # toggle string matching algorithm between fuzzy or exact
}

Multiple Selection

Default Value

See also

default

The default parameter for this prompt will set the default search text in the input buffer (sort of replicate the behavior of fzf).

If you wish to pre-select certain choices, you can leverage the enabled parameter/key of each choice.

from InquirerPy.base import Choice

choices = [
    Choice(1, enabled=True),  # enabled by default
    Choice(2)  # not enabled
]

Exact Sub-String Match

This prompt uses the fzy fuzzy match algorithm by default. You can enable exact sub-string match by using the parameter match_exact.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
    {
        "type": "fuzzy",
        "message": "Select actions:",
        "choices": ["hello", "weather", "what", "whoa", "hey", "yo"],
        "match_exact": True,
        "exact_symbol": " E",   # indicator of exact match
    },
]

result = prompt(questions=questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.fuzzy(
    message="Select actions:",
    choices=["hello", "weather", "what", "whoa", "hey", "yo"],
    match_exact=True,
    exact_symbol=" E",  # indicator of exact match
).execute()

You can also enable a keybinding to toggle the matching algorithm.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
    {
        "type": "fuzzy",
        "message": "Select actions:",
        "choices": ["hello", "weather", "what", "whoa", "hey", "yo"],
        "keybindings": {"toggle-exact": [{"key": "c-t"}]},
    },
]

result = prompt(questions=questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.fuzzy(
    message="Select actions:",
    choices=["hello", "weather", "what", "whoa", "hey", "yo"],
    keybindings={"toggle-exact": [{"key": "c-t"}]},
).execute()

Reference

class InquirerPy.prompts.fuzzy.FuzzyPrompt(message, choices, default='', pointer='❯', style=None, vi_mode=False, qmark='?', amark='?', transformer=None, filter=None, instruction='', long_instruction='', multiselect=False, prompt='❯', marker='❯', marker_pl=' ', border=False, info=True, match_exact=False, exact_symbol=' E', height=None, max_height=None, validate=None, invalid_message='Invalid input', keybindings=None, cycle=True, wrap_lines=True, raise_keyboard_interrupt=True, mandatory=True, mandatory_message='Mandatory prompt', session_result=None)[source]

Create a prompt that lists choices while also allowing fuzzy search like fzf.

A wrapper class around Application.

Fuzzy search using pfzy.match.fuzzy_match() function.

Override the default keybindings for up/down as j/k cannot be bind even if editing_mode is vim due to the input buffer.

Parameters
  • message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message documentation for more details.

  • choices (Union[Callable[[InquirerPySessionResult], Union[List[Any], List[Choice], List[Dict[str, Any]]]], List[Any], List[Choice], List[Dict[str, Any]]]) – List of choices to display and select. Refer to choices documentation for more details.

  • style (Optional[InquirerPy.utils.InquirerPyStyle]) – An InquirerPyStyle instance. Refer to Style documentation for more details.

  • vi_mode (bool) – Use vim keybinding for the prompt. Refer to keybindings documentation for more details.

  • default (Union[Any, Callable[[InquirerPySessionResult], Any]]) – Set the default value in the search buffer. Different than other list type prompts, the default parameter tries to replicate what fzf does and add the value in default to search buffer so it starts searching immediatelly. Refer to default documentation for more details.

  • qmark (str) – Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.

  • amark (str) – Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.

  • pointer (str) – Pointer symbol. Customer symbol that will be used to indicate the current choice selection.

  • instruction (str) – Short instruction to display next to the question.

  • long_instruction (str) – Long instructions to display at the bottom of the prompt.

  • validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. The main use case for this prompt would be when multiselect is True, you can enforce a min/max selection. Refer to Validator documentation for more details.

  • invalid_message (str) – Error message to display when user input is invalid. Refer to Validator documentation for more details.

  • transformer (Optional[Callable[[Any], Any]]) – A function which performs additional transformation on the value that gets printed to the terminal. Different than filter parameter, this is only visual effect and won’t affect the actual value returned by execute(). Refer to transformer documentation for more details.

  • filter (Optional[Callable[[Any], Any]]) – A function which performs additional transformation on the result. This affects the actual value returned by execute(). Refer to filter documentation for more details.

  • height (Optional[Union[int, str]]) – Preferred height of the prompt. Refer to height documentation for more details.

  • max_height (Optional[Union[int, str]]) – Max height of the prompt. Refer to height documentation for more details.

  • multiselect (bool) – Enable multi-selection on choices. You can use validate parameter to control min/max selections. Setting to True will also change the result from a single value to a list of values.

  • prompt (str) – Input prompt symbol. Custom symbol to display infront of the input buffer to indicate for input.

  • border (bool) – Create border around the choice window.

  • info (bool) – Display choice information similar to fzf –info=inline next to the prompt.

  • match_exact (bool) – Use exact sub-string match instead of using fzy fuzzy match algorithm.

  • exact_symbol (str) – Custom symbol to display in the info section when info=True.

  • marker (str) – Marker Symbol. Custom symbol to indicate if a choice is selected. This will take effects when multiselect is True.

  • marker_pl (str) – Marker place holder when the choice is not selected. This is empty space by default.

  • keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.

  • cycle (bool) – Return to top item if hit bottom during navigation or vice versa.

  • wrap_lines (bool) – Soft wrap question lines when question exceeds the terminal width.

  • raise_keyboard_interrupt (bool) – Raise the KeyboardInterrupt exception when ctrl-c is pressed. If false, the result will be None and the question is skiped.

  • mandatory (bool) – Indicate if the prompt is mandatory. If True, then the question cannot be skipped.

  • mandatory_message (str) – Error message to show when user attempts to skip mandatory prompt.

  • session_result (Optional[Dict[Union[str, int], Optional[Union[str, bool, List[Any]]]]]) – Used internally for Classic Syntax (PyInquirer).

Return type

None

Examples

>>> from InquirerPy import inquirer
>>> result = inquirer.fuzzy(message="Select one:", choices=[1, 2, 3]).execute()
>>> print(result)
1