File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -17,4 +17,16 @@ fn main() {
17
17
println ! ( " {}" , list[ sorted[ 0 ] ] ) ;
18
18
println ! ( "Your least favorite item:" ) ;
19
19
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 ] ] ) ;
20
32
}
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ pub struct Sort<'a> {
28
28
items : Vec < String > ,
29
29
prompt : Option < String > ,
30
30
clear : bool ,
31
+ max_length : Option < usize > ,
31
32
theme : & ' a dyn Theme ,
32
33
}
33
34
@@ -53,6 +54,17 @@ impl Sort<'_> {
53
54
self
54
55
}
55
56
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
+ }
56
68
/// Add a single item to the selector.
57
69
pub fn item < T : ToString > ( & mut self , item : T ) -> & mut Self {
58
70
self . items . push ( item. to_string ( ) ) ;
@@ -155,7 +167,7 @@ impl Sort<'_> {
155
167
) ) ;
156
168
}
157
169
158
- let mut paging = Paging :: new ( term, self . items . len ( ) , None ) ;
170
+ let mut paging = Paging :: new ( term, self . items . len ( ) , self . max_length ) ;
159
171
let mut render = TermThemeRenderer :: new ( term, self . theme ) ;
160
172
let mut sel = 0 ;
161
173
@@ -314,6 +326,7 @@ impl<'a> Sort<'a> {
314
326
items : vec ! [ ] ,
315
327
clear : true ,
316
328
prompt : None ,
329
+ max_length : None ,
317
330
theme,
318
331
}
319
332
}
You can’t perform that action at this time.
0 commit comments