8 Commits

Author SHA1 Message Date
37ae70bc56 refactor: move fuzzy search out of command
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 22:08:39 +02:00
95fa4128ca refactor/matcher move to a separate file 2024-09-15 22:08:39 +02:00
55fff9612e refactor: move fuzzy search out of command 2024-09-15 22:08:39 +02:00
102af558f5 Actually add fuzzy matcher 2024-09-15 22:08:39 +02:00
ff8103c805 refactor: extract matcher
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 21:44:38 +02:00
6773122076 feat: implement naive fuzzy matcher
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 21:28:29 +02:00
1520374a39 chore: update dependencies
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 20:29:44 +02:00
fd6ffc9645 chore(deps): update rust crate anyhow to v1.0.89
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2024-09-15 04:30:37 +00:00
5 changed files with 93 additions and 9 deletions

23
Cargo.lock generated
View File

@@ -83,9 +83,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.88"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e1496f8fb1fbf272686b8d37f523dab3e4a7443300055e74cdaa449f3114356"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]]
name = "arc-swap"
@@ -483,7 +483,7 @@ dependencies = [
[[package]]
name = "gitnow"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"async-trait",
@@ -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"

View File

@@ -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"

View File

@@ -1,4 +1,8 @@
use crate::{app::App, cache::CacheApp, projects_list::ProjectsListApp};
use nucleo_matcher::{pattern::Pattern, Matcher, Utf32Str};
use crate::{
app::App, cache::CacheApp, fuzzy_matcher::FuzzyMatcherApp, projects_list::ProjectsListApp,
};
#[derive(Debug, Clone)]
pub struct RootCommand {
@@ -10,8 +14,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? {
@@ -25,9 +28,24 @@ impl RootCommand {
repositories
}
};
let needle = match search {
Some(needle) => needle.into(),
None => todo!(),
};
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())
.collect::<Vec<_>>();
let haystack = haystack.as_str_vec();
let res = self.app.fuzzy_matcher().match_pattern(&needle, &haystack);
let res = res.iter().take(10).rev().collect::<Vec<_>>();
for repo in res {
tracing::debug!("repo: {:?}", repo);
}
tracing::info!("amount of repos fetched {}", repositories.len());
@@ -35,3 +53,13 @@ impl RootCommand {
Ok(())
}
}
trait StringExt {
fn as_str_vec<'a>(&'a self) -> Vec<&'a str>;
}
impl StringExt for Vec<String> {
fn as_str_vec<'a>(&'a self) -> Vec<&'a str> {
self.iter().map(|r| r.as_ref()).collect()
}
}

View File

@@ -0,0 +1,34 @@
use nucleo_matcher::{pattern::Pattern, Matcher};
use crate::app::App;
pub struct FuzzyMatcher {}
impl FuzzyMatcher {
pub fn new() -> Self {
Self {}
}
pub fn match_pattern<'a>(&self, pattern: &'a str, items: &'a [&'a str]) -> Vec<&'a str> {
let pat = Pattern::new(
&pattern,
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 = pat.match_list(items, &mut matcher);
res.into_iter().map(|((item, _))| *item).collect::<Vec<_>>()
}
}
pub trait FuzzyMatcherApp {
fn fuzzy_matcher(&self) -> FuzzyMatcher;
}
impl FuzzyMatcherApp for &'static App {
fn fuzzy_matcher(&self) -> FuzzyMatcher {
FuzzyMatcher::new()
}
}

View File

@@ -12,6 +12,7 @@ mod cache;
mod cache_codec;
mod commands;
mod config;
mod fuzzy_matcher;
mod git_provider;
mod projects_list;
@@ -20,6 +21,9 @@ mod projects_list;
struct Command {
#[command(subcommand)]
command: Option<Commands>,
#[arg()]
search: Option<String>,
}
#[derive(Subcommand)]
@@ -51,7 +55,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?;
}
}