feat: implement naive fuzzy matcher
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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"
|
||||
|
@@ -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<impl Into<String>>) -> 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::<Vec<_>>();
|
||||
|
||||
for (repo, _score) in res {
|
||||
tracing::debug!("repo: {:?}", repo);
|
||||
}
|
||||
|
||||
tracing::info!("amount of repos fetched {}", repositories.len());
|
||||
|
@@ -20,6 +20,9 @@ mod projects_list;
|
||||
struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
#[arg()]
|
||||
search: Option<String>,
|
||||
}
|
||||
|
||||
#[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?;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user