Skip to content

Commit

Permalink
Limit the number of visible options in fuzzy select
Browse files Browse the repository at this point in the history
  • Loading branch information
craciuncezar committed Jan 13, 2022
1 parent 29da763 commit 94a276d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/fuzzyselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions src/prompts/fuzzy_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<Vec<_>>();

// Renders all matching items, from best match to worst.
Expand Down

0 comments on commit 94a276d

Please sign in to comment.