From 94a276d9e8a4215af95334a45b64a5923616abf6 Mon Sep 17 00:00:00 2001 From: Cezar Craciun Date: Thu, 13 Jan 2022 23:52:15 +0200 Subject: [PATCH] Limit the number of visible options in fuzzy select --- examples/fuzzyselect.rs | 24 ++++++++++++++++++++++++ src/prompts/fuzzy_select.rs | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/examples/fuzzyselect.rs b/examples/fuzzyselect.rs index 93b9b623..a00fb6b1 100644 --- a/examples/fuzzyselect.rs +++ b/examples/fuzzyselect.rs @@ -6,6 +6,30 @@ fn main() { "Vanilla Cupcake", "Chocolate Muffin", "A Pile of sweet, sweet mustard", + "Carrots", + "Peas", + "Pistacio", + "Mustard", + "Cream", + "Banana", + "Chocolate", + "Flakes", + "Corn", + "Cake", + "Tarte", + "Cheddar", + "Vanilla", + "Hazelnut", + "Flour", + "Sugar", + "Salt", + "Potato", + "French Fries", + "Pizza", + "Mousse au chocolat", + "Brown sugar", + "Blueberry", + "Burger", ]; let selection = FuzzySelect::with_theme(&ColorfulTheme::default()) diff --git a/src/prompts/fuzzy_select.rs b/src/prompts/fuzzy_select.rs index 4d423e49..155ebd29 100644 --- a/src/prompts/fuzzy_select.rs +++ b/src/prompts/fuzzy_select.rs @@ -152,6 +152,12 @@ impl FuzzySelect<'_> { // Fuzzy matcher let matcher = fuzzy_matcher::skim::SkimMatcherV2::default(); + let term_size_rows = term.size().0 as usize; + // Subtract -2 because we need space to render the prompt. + let visible_term_rows = term_size_rows + .max(3) // Safeguard in case term_size_rows is 2 or less. + - 2; + term.hide_cursor()?; loop { @@ -164,6 +170,7 @@ impl FuzzySelect<'_> { .iter() .map(|item| (item, matcher.fuzzy_match(item, &search_term))) .filter_map(|(item, score)| score.map(|s| (item, s))) + .take(visible_term_rows) .collect::>(); // Renders all matching items, from best match to worst.