Skip to content

Commit d521c41

Browse files
committed
Add max page length to multi select
1 parent 19162c9 commit d521c41

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

examples/multi_select.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ fn main() {
2323
println!(" {}", multiselected[selection]);
2424
}
2525
}
26+
27+
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
28+
.with_prompt("Pick your food")
29+
.items(&multiselected[..])
30+
.defaults(&defaults[..])
31+
.max_length(2)
32+
.interact()
33+
.unwrap();
34+
if selections.is_empty() {
35+
println!("You did not select anything :(");
36+
} else {
37+
println!("You selected these things:");
38+
for selection in selections {
39+
println!(" {}", multiselected[selection]);
40+
}
41+
}
2642
}

src/prompts/multi_select.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct MultiSelect<'a> {
2626
items: Vec<String>,
2727
prompt: Option<String>,
2828
clear: bool,
29+
max_length: Option<usize>,
2930
theme: &'a dyn Theme,
3031
}
3132

@@ -63,6 +64,18 @@ impl MultiSelect<'_> {
6364
self
6465
}
6566

67+
/// Sets an optional max length for a page
68+
///
69+
/// Max length is disabled by None
70+
pub fn max_length(&mut self, val: usize) -> &mut Self {
71+
// Paging subtracts two from the capacity, paging does this to
72+
// make an offset for the page indicator. So to make sure that
73+
// we can show the intended amount of items we need to add two
74+
// to our value.
75+
self.max_length = Some(val + 2);
76+
self
77+
}
78+
6679
/// Add a single item to the selector.
6780
#[inline]
6881
pub fn item<T: ToString>(&mut self, item: T) -> &mut Self {
@@ -182,7 +195,7 @@ impl MultiSelect<'_> {
182195
));
183196
}
184197

185-
let mut paging = Paging::new(term, self.items.len(), None);
198+
let mut paging = Paging::new(term, self.items.len(), self.max_length);
186199
let mut render = TermThemeRenderer::new(term, self.theme);
187200
let mut sel = 0;
188201

@@ -315,6 +328,7 @@ impl<'a> MultiSelect<'a> {
315328
defaults: vec![],
316329
clear: true,
317330
prompt: None,
331+
max_length: None,
318332
theme,
319333
}
320334
}

0 commit comments

Comments
 (0)