Letter-search in SelectView wraps back

This commit is contained in:
Alexandre Bury 2015-05-31 19:09:30 -07:00
parent c8b2bc943d
commit a30a77f39b

View File

@ -117,11 +117,15 @@ impl <T> View for SelectView<T> {
Event::KeyEvent(Key::End) => self.focus = self.items.len()-1, Event::KeyEvent(Key::End) => self.focus = self.items.len()-1,
Event::CharEvent(c) => { Event::CharEvent(c) => {
// Starting from the current focus, find the first item that match the char. // Starting from the current focus, find the first item that match the char.
if let Some((i,_)) = self.items.iter().enumerate() // Cycle back to the beginning of the list when we reach the end.
// This is achieved by chaining twice the iterator
let iter = self.items.iter().chain(self.items.iter());
if let Some((i,_)) = iter.enumerate()
.skip(self.focus+1) .skip(self.focus+1)
.find(|&(_,item)| item.label.starts_with(c)) .find(|&(_,item)| item.label.starts_with(c))
{ {
self.focus = i; // Apply modulo in case we have a hit from the chained iterator
self.focus = i % self.items.len();
} }
}, },
_ => return EventResult::Ignored, _ => return EventResult::Ignored,