1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use super::history::History;
use super::settings::SearchMode;
use minspan::minspan;
pub fn reorder_fuzzy(mode: SearchMode, query: &str, res: Vec<History>) -> Vec<History> {
match mode {
SearchMode::Fuzzy => reorder(query, |x| &x.command, res),
_ => res,
}
}
fn reorder<F, A>(query: &str, f: F, res: Vec<A>) -> Vec<A>
where
F: Fn(&A) -> &String,
A: Clone,
{
let mut r = res.clone();
let qvec = &query.chars().collect();
r.sort_by_cached_key(|h| {
let (from, to) = match minspan::span(qvec, &(f(h).chars().collect())) {
Some(x) => x,
None => (0, res.len()),
};
1 + to - from
});
r
}