From 6425cfaacf3aa1ebc48296dd20a4d9be8a3065ed Mon Sep 17 00:00:00 2001 From: squid Date: Thu, 12 Nov 2026 19:52:09 +0100 Subject: [PATCH] Changed command palette to have exit/quit as command, and the fuzzy matching ranks matching text, so quit makes the command higher then quick terminal --- src/main.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c953189..2a60d80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1483,7 +1483,7 @@ impl Command { .and_then(|folder| folder.profiles.get(*profile)) .map(|profile| profile.name.clone()) .unwrap_or_default(), - Self::Exit => "Exit".to_owned(), + Self::Exit => "Exit / Quit".to_owned(), } } @@ -1600,6 +1600,11 @@ impl Command { ); } + // Best matches first; ties keep the original command order. + commands.sort_by_key(|command| { + std::cmp::Reverse(fuzzy_score(&command.label(state), query)) + }); + commands } } @@ -1630,6 +1635,56 @@ fn fuzzy_match(haystack: &str, needle: &str) -> bool { .all(|needle| haystack.any(|haystack| haystack == needle)) } +/// Rank for a fuzzy match: exact and contiguous matches beat scattered +/// subsequences, so `quit` favours "Exit / Quit" over "Quick Terminal". +fn fuzzy_score(haystack: &str, needle: &str) -> i32 { + let needle = needle.trim().to_lowercase(); + if needle.is_empty() { + return 0; + } + + let haystack = haystack.to_lowercase(); + + if haystack == needle { + return 1000; + } + + if haystack.starts_with(&needle) { + return 900; + } + + if let Some(index) = haystack.find(&needle) { + let at_word_start = index == 0 + || !haystack[..index] + .chars() + .last() + .is_some_and(char::is_alphanumeric); + + return if at_word_start { 800 } else { 700 }; + } + + // Scattered subsequence: prefer the most compact span. + let characters: Vec = haystack.chars().collect(); + let mut needle = needle.chars(); + let mut next = needle.next(); + let mut first = None; + let mut last = 0; + + for (index, character) in characters.iter().enumerate() { + if next == Some(*character) { + first.get_or_insert(index); + last = index; + next = needle.next(); + } + } + + if next.is_some() { + return 0; + } + + 100 - (last - first.unwrap_or(0)).min(100) as i32 +} + /// Per-window UI state: tabs, menus and tab-strip scrolling. struct WindowState { tabs: Vec, @@ -13431,6 +13486,10 @@ mod tests { super::Command::matching(&state, window, "exit"), vec![super::Command::Exit] ); + assert_eq!( + super::Command::matching(&state, window, "quit").first(), + Some(&super::Command::Exit) + ); let _ = update(&mut state, Message::RunSelectedCommand(window)); @@ -13632,8 +13691,8 @@ mod tests { assert_eq!( super::Command::matching(&state, window, "temp 9"), vec![ - super::Command::FocusWindow(destination), - super::Command::FocusTab(destination, Tab::Blank(9)) + super::Command::FocusTab(destination, Tab::Blank(9)), + super::Command::FocusWindow(destination) ] );