Skip to content

Commit 94a276d

Browse files
committed
Limit the number of visible options in fuzzy select
1 parent 29da763 commit 94a276d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

examples/fuzzyselect.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ fn main() {
66
"Vanilla Cupcake",
77
"Chocolate Muffin",
88
"A Pile of sweet, sweet mustard",
9+
"Carrots",
10+
"Peas",
11+
"Pistacio",
12+
"Mustard",
13+
"Cream",
14+
"Banana",
15+
"Chocolate",
16+
"Flakes",
17+
"Corn",
18+
"Cake",
19+
"Tarte",
20+
"Cheddar",
21+
"Vanilla",
22+
"Hazelnut",
23+
"Flour",
24+
"Sugar",
25+
"Salt",
26+
"Potato",
27+
"French Fries",
28+
"Pizza",
29+
"Mousse au chocolat",
30+
"Brown sugar",
31+
"Blueberry",
32+
"Burger",
933
];
1034

1135
let selection = FuzzySelect::with_theme(&ColorfulTheme::default())

src/prompts/fuzzy_select.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ impl FuzzySelect<'_> {
152152
// Fuzzy matcher
153153
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default();
154154

155+
let term_size_rows = term.size().0 as usize;
156+
// Subtract -2 because we need space to render the prompt.
157+
let visible_term_rows = term_size_rows
158+
.max(3) // Safeguard in case term_size_rows is 2 or less.
159+
- 2;
160+
155161
term.hide_cursor()?;
156162

157163
loop {
@@ -164,6 +170,7 @@ impl FuzzySelect<'_> {
164170
.iter()
165171
.map(|item| (item, matcher.fuzzy_match(item, &search_term)))
166172
.filter_map(|(item, score)| score.map(|s| (item, s)))
173+
.take(visible_term_rows)
167174
.collect::<Vec<_>>();
168175

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

0 commit comments

Comments
 (0)