File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,20 @@ fn main() {
23
23
println ! ( " {}" , multiselected[ selection] ) ;
24
24
}
25
25
}
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
+ }
26
42
}
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ pub struct MultiSelect<'a> {
26
26
items : Vec < String > ,
27
27
prompt : Option < String > ,
28
28
clear : bool ,
29
+ max_length : Option < usize > ,
29
30
theme : & ' a dyn Theme ,
30
31
}
31
32
@@ -63,6 +64,18 @@ impl MultiSelect<'_> {
63
64
self
64
65
}
65
66
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
+
66
79
/// Add a single item to the selector.
67
80
#[ inline]
68
81
pub fn item < T : ToString > ( & mut self , item : T ) -> & mut Self {
@@ -182,7 +195,7 @@ impl MultiSelect<'_> {
182
195
) ) ;
183
196
}
184
197
185
- let mut paging = Paging :: new ( term, self . items . len ( ) , None ) ;
198
+ let mut paging = Paging :: new ( term, self . items . len ( ) , self . max_length ) ;
186
199
let mut render = TermThemeRenderer :: new ( term, self . theme ) ;
187
200
let mut sel = 0 ;
188
201
@@ -315,6 +328,7 @@ impl<'a> MultiSelect<'a> {
315
328
defaults : vec ! [ ] ,
316
329
clear : true ,
317
330
prompt : None ,
331
+ max_length : None ,
318
332
theme,
319
333
}
320
334
}
You can’t perform that action at this time.
0 commit comments