launcher: Better sorting of results
This commit is contained in:
+21
-5
@@ -4,6 +4,7 @@ use crate::draw::{draw_text, ROBOTO_REGULAR};
|
|||||||
use crate::module::{Input, ModuleImpl};
|
use crate::module::{Input, ModuleImpl};
|
||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
|
|
||||||
@@ -132,14 +133,29 @@ impl ModuleImpl for Launcher {
|
|||||||
|
|
||||||
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
||||||
if self.dirty || force {
|
if self.dirty || force {
|
||||||
self.matches = self
|
let mut m = self
|
||||||
.options
|
.options
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| x.to_lowercase().find(&self.cur.to_lowercase()).is_some())
|
.map(|x| (x.to_lowercase().find(&self.cur.to_lowercase()), x))
|
||||||
.map(|x| x.to_string())
|
.filter(|(x, _)| x.is_some())
|
||||||
.collect();
|
.map(|(x, y)| (x.unwrap(), y.to_string()))
|
||||||
|
.collect::<Vec<(usize, String)>>();
|
||||||
|
|
||||||
self.matches.sort();
|
m.sort_by(|(x1, y1), (x2, y2)| {
|
||||||
|
if x1 < x2 {
|
||||||
|
Ordering::Less
|
||||||
|
} else if x1 > x2 {
|
||||||
|
Ordering::Greater
|
||||||
|
} else if y1.len() < y2.len() {
|
||||||
|
Ordering::Less
|
||||||
|
} else if y1.len() > y2.len() {
|
||||||
|
Ordering::Greater
|
||||||
|
} else {
|
||||||
|
y1.cmp(y2)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.matches = m.into_iter().map(|(_, x)| x).collect();
|
||||||
self.dirty = false;
|
self.dirty = false;
|
||||||
Ok(true)
|
Ok(true)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user