This commit is contained in:
Alexandre Bury 2019-04-11 17:08:55 -07:00
parent 158bc65c33
commit 51c6450688
3 changed files with 29 additions and 19 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -384,33 +384,39 @@ impl<T: 'static> SelectView<T> {
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<F>(&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<K, F>(&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<String> {
}
}
impl<T: 'static> SelectView<T> where T: Ord {
impl<T: 'static> SelectView<T>
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<T> Item<T> {
#[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);