Skip to content

Commit f1e46a5

Browse files
committed
Add scroll functionality
1 parent 94a276d commit f1e46a5

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/prompts/fuzzy_select.rs

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

155-
let term_size_rows = term.size().0 as usize;
156155
// 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;
156+
let visible_term_rows = (term.size().0 as usize).max(3) - 2;
157+
// Variable used to determine if we need to scroll through the list.
158+
let mut starting_row = 0;
160159

161160
term.hide_cursor()?;
162161

@@ -170,13 +169,17 @@ impl FuzzySelect<'_> {
170169
.iter()
171170
.map(|item| (item, matcher.fuzzy_match(item, &search_term)))
172171
.filter_map(|(item, score)| score.map(|s| (item, s)))
173-
.take(visible_term_rows)
174172
.collect::<Vec<_>>();
175173

176174
// Renders all matching items, from best match to worst.
177175
filtered_list.sort_unstable_by(|(_, s1), (_, s2)| s2.cmp(&s1));
178176

179-
for (idx, (item, _)) in filtered_list.iter().enumerate() {
177+
for (idx, (item, _)) in filtered_list
178+
.iter()
179+
.enumerate()
180+
.skip(starting_row)
181+
.take(visible_term_rows)
182+
{
180183
render.select_prompt_item(item, idx == sel)?;
181184
term.flush()?;
182185
}
@@ -191,6 +194,12 @@ impl FuzzySelect<'_> {
191194
return Ok(None);
192195
}
193196
Key::ArrowUp | Key::BackTab if filtered_list.len() > 0 => {
197+
if sel == 0 {
198+
starting_row =
199+
filtered_list.len().max(visible_term_rows) - visible_term_rows;
200+
} else if sel == starting_row {
201+
starting_row -= 1;
202+
}
194203
if sel == !0 {
195204
sel = filtered_list.len() - 1;
196205
} else {
@@ -205,6 +214,11 @@ impl FuzzySelect<'_> {
205214
} else {
206215
sel = (sel as u64 + 1).rem(filtered_list.len() as u64) as usize;
207216
}
217+
if sel == visible_term_rows + starting_row {
218+
starting_row += 1;
219+
} else if sel == 0 {
220+
starting_row = 0;
221+
}
208222
term.flush()?;
209223
}
210224
Key::ArrowLeft if position > 0 => {
@@ -242,6 +256,7 @@ impl FuzzySelect<'_> {
242256
position += 1;
243257
term.flush()?;
244258
sel = 0;
259+
starting_row = 0;
245260
}
246261

247262
_ => {}

0 commit comments

Comments
 (0)