Skip to content

Commit 37221fd

Browse files
committed
Add max page length to sort
1 parent d521c41 commit 37221fd

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

examples/sort.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ fn main() {
1717
println!(" {}", list[sorted[0]]);
1818
println!("Your least favorite item:");
1919
println!(" {}", list[sorted[sorted.len() - 1]]);
20+
21+
let sorted = Sort::with_theme(&ColorfulTheme::default())
22+
.with_prompt("Order your foods by preference")
23+
.items(&list[..])
24+
.max_length(2)
25+
.interact()
26+
.unwrap();
27+
28+
println!("Your favorite item:");
29+
println!(" {}", list[sorted[0]]);
30+
println!("Your least favorite item:");
31+
println!(" {}", list[sorted[sorted.len() - 1]]);
2032
}

src/prompts/sort.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Sort<'a> {
2828
items: Vec<String>,
2929
prompt: Option<String>,
3030
clear: bool,
31+
max_length: Option<usize>,
3132
theme: &'a dyn Theme,
3233
}
3334

@@ -53,6 +54,17 @@ impl Sort<'_> {
5354
self
5455
}
5556

57+
/// Sets an optional max length for a page
58+
///
59+
/// Max length is disabled by None
60+
pub fn max_length(&mut self, val: usize) -> &mut Self {
61+
// Paging subtracts two from the capacity, paging does this to
62+
// make an offset for the page indicator. So to make sure that
63+
// we can show the intended amount of items we need to add two
64+
// to our value.
65+
self.max_length = Some(val + 2);
66+
self
67+
}
5668
/// Add a single item to the selector.
5769
pub fn item<T: ToString>(&mut self, item: T) -> &mut Self {
5870
self.items.push(item.to_string());
@@ -155,7 +167,7 @@ impl Sort<'_> {
155167
));
156168
}
157169

158-
let mut paging = Paging::new(term, self.items.len(), None);
170+
let mut paging = Paging::new(term, self.items.len(), self.max_length);
159171
let mut render = TermThemeRenderer::new(term, self.theme);
160172
let mut sel = 0;
161173

@@ -314,6 +326,7 @@ impl<'a> Sort<'a> {
314326
items: vec![],
315327
clear: true,
316328
prompt: None,
329+
max_length: None,
317330
theme,
318331
}
319332
}

0 commit comments

Comments
 (0)