| 67 | } |
| 68 | |
| 69 | pub fn search(&self, query: &str, limit: usize) -> Vec<(String, u32)> { |
| 70 | if limit == 0 || self.entries.is_empty() { |
| 71 | return Vec::new(); |
| 72 | } |
| 73 | let trimmed = query.trim(); |
| 74 | if trimmed.is_empty() { |
| 75 | return self |
| 76 | .entries |
| 77 | .iter() |
| 78 | .take(limit) |
| 79 | .map(|item| (item.clone(), 0)) |
| 80 | .collect(); |
| 81 | } |
| 82 | |
| 83 | let pattern = Pattern::parse(trimmed, CaseMatching::Smart, Normalization::Smart); |
| 84 | let mut matcher = Matcher::new(Config::DEFAULT.match_paths()); |
| 85 | let mut utf32_buf = Vec::new(); |
| 86 | let mut scored = self |
| 87 | .entries |
| 88 | .iter() |
| 89 | .filter_map(|entry| { |
| 90 | pattern |
| 91 | .score(Utf32Str::new(entry, &mut utf32_buf), &mut matcher) |
| 92 | .map(|score| (entry.clone(), score)) |
| 93 | }) |
| 94 | .collect::<Vec<_>>(); |
| 95 | scored.sort_by(|a, b| { |
| 96 | b.1.cmp(&a.1) |
| 97 | .then_with(|| a.0.len().cmp(&b.0.len())) |
| 98 | .then_with(|| a.0.cmp(&b.0)) |
| 99 | }); |
| 100 | scored.truncate(limit); |
| 101 | scored |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn should_descend(entry: &DirEntry) -> bool { |