use ratatui::style::Style;
use ratatui::text::{Line, Span};
const MATCH: i32 = 16;
const BOUNDARY: i32 = 10;
const CONSECUTIVE: i32 = 12;
const GAP_START: i32 = -3;
const GAP_EXTENSION: i32 = -1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Match {
pub score: i32,
pub positions: Vec<usize>,
}
pub fn fuzzy(query: &str, text: &str) -> Option<Match> {
let exact = query.chars().any(char::is_uppercase);
let fold = |c: char| {
if exact {
c
} else {
c.to_lowercase().next().unwrap_or(c)
}
};
let q: Vec<char> = query
.chars()
.filter(|c| !c.is_whitespace())
.map(fold)
.collect();
let original: Vec<char> = text.chars().collect();
let t: Vec<char> = original.iter().copied().map(fold).collect();
if q.is_empty() {
return Some(Match {
score: 0,
positions: Vec::new(),
});
}
let (m, n) = (q.len(), t.len());
if m > n {
return None;
}
let bonus: Vec<i32> = (0..n)
.map(|j| {
let start = j == 0
|| matches!(
original[j - 1],
' ' | '-' | '_' | '/' | '.' | ':' | '@' | ','
)
|| (original[j - 1].is_lowercase() && original[j].is_uppercase());
if start {
BOUNDARY
} else {
0
}
})
.collect();
const NONE: i32 = i32::MIN / 2;
let mut score = vec![vec![NONE; n]; m];
let mut from = vec![vec![0usize; n]; m];
for j in 0..n {
if t[j] == q[0] {
score[0][j] = MATCH + bonus[j];
}
}
for i in 1..m {
let mut gap = NONE;
let mut gap_at = 0;
for j in 1..n {
if j >= 2 {
gap += GAP_EXTENSION;
let candidate = score[i - 1][j - 2] + GAP_START;
if candidate > gap {
gap = candidate;
gap_at = j - 2;
}
}
if t[j] != q[i] {
continue;
}
let next = score[i - 1][j - 1] + CONSECUTIVE;
let (best, at) = if next >= gap {
(next, j - 1)
} else {
(gap, gap_at)
};
if best > NONE / 2 {
score[i][j] = best + MATCH + bonus[j];
from[i][j] = at;
}
}
}
let (mut j, best) = score[m - 1]
.iter()
.copied()
.enumerate()
.filter(|&(_, s)| s > NONE / 2)
.max_by_key(|&(j, s)| (s, std::cmp::Reverse(j)))?;
let mut positions = vec![0; m];
for i in (0..m).rev() {
positions[i] = j;
j = from[i][j];
}
Some(Match {
score: best,
positions,
})
}
pub fn highlight(text: &str, positions: &[usize], base: Style, matched: Style) -> Line<'static> {
let mut spans: Vec<Span<'static>> = Vec::new();
let mut run = String::new();
let mut run_matched = false;
for (i, c) in text.chars().enumerate() {
let is_matched = positions.binary_search(&i).is_ok();
if is_matched != run_matched && !run.is_empty() {
let style = if run_matched { matched } else { base };
spans.push(Span::styled(std::mem::take(&mut run), style));
}
run_matched = is_matched;
run.push(c);
}
if !run.is_empty() {
spans.push(Span::styled(run, if run_matched { matched } else { base }));
}
Line::from(spans)
}
#[cfg(test)]
mod tests {
use super::*;
fn positions(query: &str, text: &str) -> Option<Vec<usize>> {
fuzzy(query, text).map(|m| m.positions)
}
fn score(query: &str, text: &str) -> i32 {
fuzzy(query, text).map_or(i32::MIN, |m| m.score)
}
#[test]
fn matches_the_characters_in_order() {
assert_eq!(positions("fnav", "fix-nav"), Some(vec![0, 4, 5, 6]));
assert_eq!(positions("", "anything"), Some(vec![]));
assert_eq!(positions("vanf", "fix-nav"), None);
assert_eq!(positions("abc", "ab"), None);
assert_eq!(positions("fix nav", "fix-nav").map(|p| p.len()), Some(6));
}
#[test]
fn prefers_runs_and_word_starts() {
assert_eq!(positions("tc", "tam-cli"), Some(vec![0, 4]));
assert!(score("nav", "fix-nav") > score("nav", "n-a-v"));
assert!(score("ui", "tam-ui") > score("ui", "build"));
assert_eq!(
positions("cache", "c-a-c-h-e-cache"),
Some(vec![10, 11, 12, 13, 14])
);
}
#[test]
fn smart_case() {
assert!(fuzzy("tam", "TAM").is_some());
assert!(fuzzy("Tam", "tam").is_none());
assert!(fuzzy("Tam", "Tam").is_some());
}
#[test]
fn highlighting() {
let bold = Style::new().add_modifier(ratatui::style::Modifier::BOLD);
let line = highlight("fix-nav", &[0, 4, 5, 6], Style::new(), bold);
let parts: Vec<(&str, bool)> = line
.spans
.iter()
.map(|s| (s.content.as_ref(), s.style == bold))
.collect();
assert_eq!(parts, [("f", true), ("ix-", false), ("nav", true)]);
}
}