From 6773122076d969af2469f353b95dd32f37eced40 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 15 Sep 2024 21:14:44 +0200 Subject: [PATCH] feat: implement naive fuzzy matcher --- Cargo.lock | 17 +++++++++++++++++ crates/gitnow/Cargo.toml | 1 + crates/gitnow/src/commands/root.rs | 29 +++++++++++++++++++++++++---- crates/gitnow/src/main.rs | 5 ++++- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a8c1092..ae12a53 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,6 +492,7 @@ dependencies = [ "dirs", "dotenv", "gitea-rs", + "nucleo-matcher", "octocrab", "pretty_assertions", "prost", @@ -905,6 +906,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "nucleo-matcher" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf33f538733d1a5a3494b836ba913207f14d9d4a1d3cd67030c5061bdd2cac85" +dependencies = [ + "memchr", + "unicode-segmentation", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -1965,6 +1976,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/crates/gitnow/Cargo.toml b/crates/gitnow/Cargo.toml index 0ba6c4c..dcf9df6 100644 --- a/crates/gitnow/Cargo.toml +++ b/crates/gitnow/Cargo.toml @@ -24,6 +24,7 @@ dirs = "5.0.1" prost = "0.13.2" prost-types = "0.13.2" bytes = "1.7.1" +nucleo-matcher = "0.3.1" [dev-dependencies] pretty_assertions = "1.4.0" diff --git a/crates/gitnow/src/commands/root.rs b/crates/gitnow/src/commands/root.rs index 2231532..446beaa 100644 --- a/crates/gitnow/src/commands/root.rs +++ b/crates/gitnow/src/commands/root.rs @@ -1,3 +1,5 @@ +use nucleo_matcher::{pattern::Pattern, Matcher, Utf32Str}; + use crate::{app::App, cache::CacheApp, projects_list::ProjectsListApp}; #[derive(Debug, Clone)] @@ -10,8 +12,7 @@ impl RootCommand { Self { app } } - #[tracing::instrument(skip(self))] - pub async fn execute(&mut self) -> anyhow::Result<()> { + pub async fn execute(&mut self, search: Option>) -> anyhow::Result<()> { tracing::debug!("executing"); let repositories = match self.app.cache().get().await? { @@ -26,8 +27,28 @@ impl RootCommand { } }; - for repo in &repositories { - //tracing::info!("repo: {}", repo.to_rel_path().display()); + let haystack = repositories + .iter() + .map(|r| r.to_rel_path().display().to_string()); + + let needle = match search { + Some(needle) => needle.into(), + None => todo!(), + }; + + let pattern = Pattern::new( + &needle, + nucleo_matcher::pattern::CaseMatching::Ignore, + nucleo_matcher::pattern::Normalization::Smart, + nucleo_matcher::pattern::AtomKind::Fuzzy, + ); + let mut matcher = Matcher::new(nucleo_matcher::Config::DEFAULT); + let res = pattern.match_list(haystack, &mut matcher); + + let res = res.iter().take(10).rev().collect::>(); + + for (repo, _score) in res { + tracing::debug!("repo: {:?}", repo); } tracing::info!("amount of repos fetched {}", repositories.len()); diff --git a/crates/gitnow/src/main.rs b/crates/gitnow/src/main.rs index 0a00951..e7d2459 100644 --- a/crates/gitnow/src/main.rs +++ b/crates/gitnow/src/main.rs @@ -20,6 +20,9 @@ mod projects_list; struct Command { #[command(subcommand)] command: Option, + + #[arg()] + search: Option, } #[derive(Subcommand)] @@ -51,7 +54,7 @@ async fn main() -> anyhow::Result<()> { match cli.command { Some(_) => todo!(), None => { - RootCommand::new(app).execute().await?; + RootCommand::new(app).execute(cli.search.as_ref()).await?; } }