diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 6ccba32..ab23f6b 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -64,7 +64,7 @@ pub trait Backend { /// Main method used for printing fn print_at(&self, pos: Vec2, text: &str); - + /// First positions the cursor, similar to `print_at`, and then prints the given number of /// `repetitions` of `text`. fn print_at_rep(&self, pos: Vec2, repetitions: usize, text: &str) { diff --git a/src/backend/termion.rs b/src/backend/termion.rs index 7b70f3e..4ed27fe 100644 --- a/src/backend/termion.rs +++ b/src/backend/termion.rs @@ -276,8 +276,9 @@ impl backend::Backend for Backend { "{}{}", termion::cursor::Goto(1 + pos.x as u16, 1 + pos.y as u16), text - ).unwrap(); - + ) + .unwrap(); + let mut dupes_left = repetitions - 1; while dupes_left > 0 { write!(out, "{}", text).unwrap(); diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 2e3f4a9..b8fdc15 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -384,33 +384,39 @@ impl SelectView { fn focus(&self) -> usize { self.focus.get() } - + /// Sort the current items lexicographically by their label. /// Note that this does not change the current focus index, which means that the current /// selection will likely be changed by the sorting. /// This sort is stable: items with identical label will not be reordered. pub fn sort_by_label(&mut self) { - self.items.sort_by(|a, b| a.label.source().cmp(b.label.source())); + self.items + .sort_by(|a, b| a.label.source().cmp(b.label.source())); } - + /// Sort the current items with the given comparator function. /// Note that this does not change the current focus index, which means that the current /// selection will likely be changed by the sorting. /// The given comparator function must define a total order for the items. /// If the comparator function does not define a total order, then the order after the sort is /// unspecified. - /// This sort is stable: equal items will not be reordered. + /// This sort is stable: equal items will not be reordered. pub fn sort_by(&mut self, mut compare: F) - where F: FnMut(&T, &T) -> Ordering { + where + F: FnMut(&T, &T) -> Ordering, + { self.items.sort_by(|a, b| compare(&a.value, &b.value)); } - + /// Sort the current items with the given key extraction function. /// Note that this does not change the current focus index, which means that the current /// selection will likely be changed by the sorting. /// This sort is stable: items with equal keys will not be reordered. pub fn sort_by_key(&mut self, mut key_of: F) - where F: FnMut(&T) -> K, K: Ord { + where + F: FnMut(&T) -> K, + K: Ord, + { self.items.sort_by_key(|item| key_of(&item.value)); } @@ -700,7 +706,10 @@ impl SelectView { } } -impl SelectView where T: Ord { +impl SelectView +where + T: Ord, +{ /// Sort the current items by their natural ordering. /// Note that this does not change the current focus index, which means that the current /// selection will likely be changed by the sorting. @@ -827,7 +836,7 @@ impl Item { #[cfg(test)] mod tests { use super::*; - + #[test] fn select_view_sorting() { // We add items in no particular order, from going by their label. @@ -849,7 +858,7 @@ mod tests { view.on_event(Event::Key(Key::Down)); assert_eq!(view.selection(), Some(Rc::new(String::from("Z")))); } - + #[test] fn select_view_sorting_with_comparator() { // We add items in no particular order, from going by their value. @@ -871,19 +880,19 @@ mod tests { view.on_event(Event::Key(Key::Down)); assert_eq!(view.selection(), Some(Rc::new(3))); } - + #[test] fn select_view_sorting_by_key() { // We add items in no particular order, from going by their key value. #[derive(Eq, PartialEq, Debug)] struct MyStruct { - key: i32 + key: i32, } - + let mut view = SelectView::new(); - view.add_item("Y", MyStruct{ key: 2 }); - view.add_item("Z", MyStruct{ key: 1 }); - view.add_item("X", MyStruct{ key: 3 }); + view.add_item("Y", MyStruct { key: 2 }); + view.add_item("Z", MyStruct { key: 1 }); + view.add_item("X", MyStruct { key: 3 }); // Then sorting the list... view.sort_by_key(|s| s.key);