FuzzyFinder
- class curses_fzf.FuzzyFinder(multi: bool = False, title: str = 'ITEMS', query: str = '', display: Callable[[Any], str] = lambda item: ..., preselect: Callable[[Any, ScoringResult], bool] = lambda item, result: ..., preview: Callable[[window, ColorTheme, Any, ScoringResult], str] | None = None, score: Callable[[str, str], ScoringResult] = scoring_fzf, color_theme: ColorTheme | None = None, autoreturn: int = 0, min_items: int = 0, max_items: int = sys.maxsize, page_size: int = 10, preview_window_percentage: int = 40)[source]
FuzzyFinderis the main entry point for the library. Usefind()to run the fuzzyfinder on a list of items and return the selection. It allows the user to filter a list of items based on aqueryentered in UI or preseeded in constructor.- Parameters:
multi (bool) –
multiselection mode determines whether to allow selection of multiple items or not. Default isFalse.title (str) – The
titleto display in the upper left corner of the mainFuzzyFinderwindow. Default is"ITEMS".query (str) – The initial
queryto preseed the interface with. Default is"". Seequeryfor more details.display (Callable[[Any], str]) –
display()function is used to convert an item to a string for display and matching purposes. Default islambda item: str(item). Seedisplay()for more details.preselect (Callable[[Any, ScoringResult], bool]) –
preselect()is a function to determine if an item should be preselected inmultiselection mode based on the item and its scoring result. Default is a function that always returnsFalse. Seepreselect()for more details.preview (Optional[Callable[[curses.window, ColorTheme, Any, ScoringResult], str]]) – Show a preview window if
preview()function is provided. Default isNone. Seepreview()for more details.score (Callable[[str, str], ScoringResult]) – The
score()function is used to calculate the score of an item fromall_itemslist based on the currentqueryand thedisplay()string of the item. Default isscoring_fzf(). Seescore()for more details.color_theme (Optional[ColorTheme]) – The
color_themeto use for the interface, ifNonewas given the defaultColorThemewill be used. Default isNone.autoreturn (int) – If
autoreturnis a positive integer, theFuzzyFinderwill automatically return the items infilteredlist without user input if the number of items in the filtered list matches the given number. IfmultiisFalse, the actual value ofautoreturnis ignored and theFuzzyFinderwill automatically return if there is exactly one item in the filtered list. Default is0.min_items (int) – The minimum number of selected items to return in
multiselection mode. Below this threshold, theFuzzyFinderwill not accept the selection on ENTER. Default is0.max_items (int) – The maximum number of selected items to return in
multiselection mode. Above this threshold, theFuzzyFinderwill not accept the selection on ENTER. Default issys.maxsize.page_size (int) – Number of items to move in
filteredlist when pressing PAGE_UP/PAGE_DOWN. Default is10.preview_window_percentage (int) –
preview_window_percentagedefines the width of the preview window as a percentage of the total width. Default is40.
- all_items: List[Any]
The original list of all items given by the user in
find()method. This list will be filtered byqueryintofilteredlist.
- autoreturn: int
If
autoreturnis a positive integer, theFuzzyFinderwill automatically return the items infilteredlist without user input if the number of items in the filtered list matches the given number. IfmultiisFalse, the actual value ofautoreturnis ignored and theFuzzyFinderwill automatically return if there is exactly one item in the filtered list. Default is0.
- calculate_filtered() None[source]
Calculate the
filteredlist of items from theall_itemslist based on the currentqueryandscorefunction.This function will be called in each iteration of the main loop of
FuzzyFinderbefore rendering the items.
- color_theme: ColorTheme
The
color_themeto use for the interface. IfNonewas given in the constructor, the defaultColorThemewill be used.
- property cursor_items: int
The index of the cursor inside the
filteredlist of items. Usekb_move_items_cursor_absoluteandkb_move_items_cursor_relativeto update the cursor position.
- property cursor_query: int
The index of the cursor inside the
querystring. Usekb_move_query_cursor_absoluteandkb_move_query_cursor_relativeto update the cursor position.
- display: Callable[[Any], str]
display()function is used to convert an item to a string for display and matching purposes. Default islambda item: str(item).- Parameters:
item (Any) – The item to convert to a string.
- Returns:
The single-line string representation of the item.
- Return type:
str
- filtered: List[Tuple[Any, ScoringResult]]
The list of items filtered by the current
query, each paired with itsScoringResult. This list is updated bycalculate_filtered(), which is called in each iteration of the main loop before rendering the items.
- find(items: List[Any], title: str | None = None, query: str | None = None) List[Any][source]
Run the
FuzzyFinderon the given list of items and return theselecteditem(s).- Parameters:
items (List[Any]) – The list of items to filter and select from. This list will be stored in
all_itemsand filtered based on thequeryintofilteredlist.title (Optional[str]) – The
titleto display in the upper left corner of the mainFuzzyFinderwindow. Default isNone, in which case the title given on constructor will be reused.query (Optional[str]) – The initial
queryto preseed the interface with. Default isNone, in which case the query given on constructor will be reused. Seequeryfor more details.
- Returns:
The list of selected items.
- Return type:
List[Any]
- kb_abort_selection() None[source]
keymapfunction: Abort theFuzzyFinderand raise theCursesFzfAbortedexception.
- kb_accept_selection() None[source]
keymapfunction: Accept the currentselectionand return it. This will setreturn_selection_nowtoTrue.
- kb_add_to_query(text: str, index: int = -1) None[source]
keymapfunction: Add the given text to thequerybefore the given index. Adjust thecursor_querycursor and reset thecursor_itemscursor if necessary.May raise
CursesFzfIndexOutOfBoundsif the given index is out of bounds.- Parameters:
text (str) – The text to add to the
querystring.index (int) – The index before which to add the text in the
querystring. The default is-1, which means to add the text at the end of thequerystring. The index may also be negative, in which case it will be counted from the end of the string (e.g.-1means before the last character,-2means before the second last character, etc.).
- kb_add_to_query_cursor(text: str) None[source]
keymapfunction: Add the given text to the query at the currentcursor_querycursor position. Adjust thecursor_querycursor and reset thecursor_itemscursor if necessary.- Parameters:
text (str) – The text to add to the
querystring.
- kb_deselect_all() None[source]
keymapfunction: Deselect all items from the currentfilteredlist (only inmultimode).
- kb_move_items_cursor_absolute(position: int) None[source]
keymapfunction: Move thecursor_itemscursor to the absolutepositioninside thefilteredlist while keeping it within bounds of the list.- Parameters:
position (int) – The absolute index inside the
filteredlist to move the cursor to.
- kb_move_items_cursor_relative(offset: int) None[source]
keymapfunction: Move thecursor_itemscursor byoffsetwhile keeping it within bounds of thefilteredlist.- Parameters:
offset (int) – The relative offset to move the cursor by inside the
filteredlist. Negative values will move the cursor up, positive values will move it down.
- kb_move_query_cursor_absolute(position: int) None[source]
keymapfunction: Move thecursor_querycursor to the absolutepositioninside thequerystring while keeping it within bounds of the string.- Parameters:
position (int) – The absolute index inside the
querystring to move the cursor to.
- kb_move_query_cursor_relative(offset: int) None[source]
keymapfunction: Move thecursor_querycursor byoffsetwhile keeping it within bounds of thequerystring.- Parameters:
offset (int) – The relative offset to move the cursor by inside the
querystring. Negative values will move the cursor left, positive values will move it right.
- kb_remove_from_query(index: int = -1, length: int = 1) None[source]
keymapfunction: Removelengthcharacters from the query at positionindexand return thecursor_itemscursor to index0. Thecursor_querycursor will be adjusted if it is after the remove index.- Parameters:
index (int) – The index at which to remove characters from the
querystring. The default is-1, which means to remove the character before the end of thequerystring. The index may also be negative, in which case it will be counted from the end of the string (e.g.-1means the last character,-2means the second last character, etc.).length (int) – The number of characters to remove from the
querystring starting from the given index. The default is1. The length may be higher than the actual length of the query, but not negative.
- kb_remove_from_query_cursor(before: bool = True) None[source]
keymapfunction: Remove one character from thequerybeforecursor_querycursor position and return thecursor_itemscursor to index0. Thecursor_querycursor will be adjusted.- Parameters:
before (bool) – Whether to remove the character before the query cursor (like BACKSPACE) or the character at the query cursor (like DEL). Default is
True.
- kb_reset_query() None[source]
keymapfunction: Clear thequerystring and return thecursor_queryandcursor_itemscursor to index0.
- kb_select_all() None[source]
keymapfunction: Select all items from the currentfilteredlist (only inmultimode).
- kb_toggle_preview() None[source]
keymapfunction: Toggle the visibility of theshow_preview()window. This will toggle theshow_previewboolean.
- kb_toggle_selection() None[source]
keymapfunction: Toggle the selection state of the current item (only inmultimode). This will add/remove the item to/fromselectedlist.
- keymap
Dictionary mapping keys (e.g.
curses.KEY_UP) to their corresponding keybinding functions (e.g.lambda: self.kb_move_items_cursor_relative(-1)). If the target function takes no arguments, it can be directly assigned likecurses.KEY_F1: self.kb_show_help.All the functions starting with
kb_are keybinding functions that are used in the keymap and can also be reassigned or called directly.
- max_items: int
The maximum number of selected items to return in
multiselection mode. Above this threshold, theFuzzyFinderwill not accept the selection on ENTER. Default issys.maxsize.
- min_items: int
The minimum number of selected items to return in
multiselection mode. Below this threshold, theFuzzyFinderwill not accept the selection on ENTER. Default is0.
- multi: bool
multiselection mode determines whether to allow selection of multiple items or not. Default isFalse.
- page_size: int
Number of items to move in
filteredlist when pressing PAGE_UP/PAGE_DOWN. Default is10.
- preselect: Callable[[Any, ScoringResult], bool]
preselect()is a function to determine if an item should be preselected inmultiselection mode based on the item and its scoring result. Default is a function that always returnsFalse.- Parameters:
item (Any) – The item from
filteredlist to determine the preselection for.scoring_result (ScoringResult) – The
ScoringResultof the item based on the currentquery.
- Returns:
Whether the item should be preselected or not.
- Return type:
bool
- preview: Callable[[window, ColorTheme, Any, ScoringResult], str] | None
If a
preview()function is provided, a preview window will be shown for the currently highlighted item in thefilteredlist.preview_window_percentagedefines the width of the preview window as a percentage of the total width. The preview window can be toggled with Ctrl+P.The
preview()function can be used in two ways:If the function returns a non-empty string, it will be rendered line by line inside the preview window, honoring the available space. In this case the
curses.windowparameter can be ignored.If the function returns an empty string,
FuzzyFinderwill assume that the user is handling the rendering of the preview window using the providedcurses.windowparameter. In this case the user needs to take care of window boundaries.
- Parameters:
preview_window (curses.window) – The curses window to render the preview in.
color_theme (ColorTheme) – The
color_themeofFuzzyFinder.item (Any) – The item from
filteredlist to generate the preview for.score_result (ScoringResult) – The
ScoringResultof the item based on the currentquery.
- Returns:
The text to render in the preview window, if it is non-empty.
- Return type:
str
- preview_window_percentage: int
preview_window_percentagedefines the width of the preview window as a percentage of the total width. The default value is40. The preview window will be placed on the right side of the screen if apreview()function is provided.
- property query: str
The
queryentered by the user or preseeded on initialization. Setting this property will also reset thecursor_itemscursor and move thecursor_querycursor to the end of the query.
- return_selection_now: bool
Whether the
FuzzyFindershould end the main loop and return theselecteditems. This will be set toTruebykb_accept_selection()on ENTER.
- score: Callable[[str, str], ScoringResult]
The
score()function is used to calculate the score of an item fromall_itemslist based on the currentqueryand thedisplay()string of the item. Thescoreis used to sort the items in thefilteredlist and to determine which items match the query.Default is
scoring_fzf().- Parameters:
- Returns:
The
ScoringResultof the item.- Return type:
- selected: List[Any]
The list of currently selected items in
multiselection mode. In single selection mode this list will be bypassed and the currently highlighted item infilteredlist will be returned on ENTER.
- show_preview: bool
Show or hide the preview window. This can be toggled with Ctrl+P if a
preview()function is provided. It will be set bykb_toggle_preview().
- stdscr: window | None
The main curses window, will be set automatically in
FuzzyFinder’s main loop.
- title: str
The
titleto display in the upper left corner of the mainFuzzyFinderwindow. Default is"ITEMS".