Compare commits

...

10 Commits

Author SHA1 Message Date
43a7196cf8 chore(deps): update rust crate pretty_assertions to v1.4.1
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2024-09-16 00:29:45 +00:00
39e1fea36f chore: refactor fuzzy match into own function
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 22:36:39 +02:00
c2dfd020bf chore: cleanup warnings
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-15 22:12:42 +02:00
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
6 changed files with 125 additions and 18 deletions

25
Cargo.lock generated
View File

@ -492,6 +492,7 @@ dependencies = [
"dirs", "dirs",
"dotenv", "dotenv",
"gitea-rs", "gitea-rs",
"nucleo-matcher",
"octocrab", "octocrab",
"pretty_assertions", "pretty_assertions",
"prost", "prost",
@ -905,6 +906,16 @@ dependencies = [
"winapi", "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]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.6" version = "0.4.6"
@ -1134,9 +1145,9 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]] [[package]]
name = "pretty_assertions" name = "pretty_assertions"
version = "1.4.0" version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
dependencies = [ dependencies = [
"diff", "diff",
"yansi", "yansi",
@ -1965,6 +1976,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.9.0" version = "0.9.0"
@ -2329,9 +2346,9 @@ dependencies = [
[[package]] [[package]]
name = "yansi" name = "yansi"
version = "0.5.1" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
[[package]] [[package]]
name = "zeroize" name = "zeroize"

View File

@ -24,6 +24,7 @@ dirs = "5.0.1"
prost = "0.13.2" prost = "0.13.2"
prost-types = "0.13.2" prost-types = "0.13.2"
bytes = "1.7.1" bytes = "1.7.1"
nucleo-matcher = "0.3.1"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "1.4.0" pretty_assertions = "1.4.0"

View File

@ -1,4 +1,12 @@
use crate::{app::App, cache::CacheApp, projects_list::ProjectsListApp}; use std::collections::BTreeMap;
use crate::{
app::App,
cache::CacheApp,
fuzzy_matcher::{FuzzyMatcher, FuzzyMatcherApp},
git_provider::Repository,
projects_list::ProjectsListApp,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RootCommand { pub struct RootCommand {
@ -10,8 +18,7 @@ impl RootCommand {
Self { app } Self { app }
} }
#[tracing::instrument(skip(self))] pub async fn execute(&mut self, search: Option<impl Into<String>>) -> anyhow::Result<()> {
pub async fn execute(&mut self) -> anyhow::Result<()> {
tracing::debug!("executing"); tracing::debug!("executing");
let repositories = match self.app.cache().get().await? { let repositories = match self.app.cache().get().await? {
@ -25,9 +32,19 @@ impl RootCommand {
repositories repositories
} }
}; };
let needle = match search {
Some(needle) => needle.into(),
None => todo!(),
};
for repo in &repositories { let matched_repos = self
//tracing::info!("repo: {}", repo.to_rel_path().display()); .app
.fuzzy_matcher()
.match_repositories(&needle, &repositories);
let res = matched_repos.iter().take(10).rev().collect::<Vec<_>>();
for repo in res {
tracing::debug!("repo: {:?}", repo);
} }
tracing::info!("amount of repos fetched {}", repositories.len()); tracing::info!("amount of repos fetched {}", repositories.len());
@ -35,3 +52,43 @@ impl RootCommand {
Ok(()) Ok(())
} }
} }
trait StringExt {
fn as_str_vec(&self) -> Vec<&str>;
}
impl StringExt for Vec<String> {
fn as_str_vec(&self) -> Vec<&str> {
self.iter().map(|r| r.as_ref()).collect()
}
}
impl StringExt for Vec<&String> {
fn as_str_vec(&self) -> Vec<&str> {
self.iter().map(|r| r.as_ref()).collect()
}
}
trait RepositoryMatcher {
fn match_repositories(&self, pattern: &str, repositories: &[Repository]) -> Vec<Repository>;
}
impl RepositoryMatcher for FuzzyMatcher {
fn match_repositories(&self, pattern: &str, repositories: &[Repository]) -> Vec<Repository> {
let haystack = repositories
.iter()
.map(|r| (r.to_rel_path().display().to_string(), r))
.collect::<BTreeMap<_, _>>();
let haystack_keys = haystack.keys().collect::<Vec<_>>();
let haystack_keys = haystack_keys.as_str_vec();
let res = self.match_pattern(pattern, &haystack_keys);
let matched_repos = res
.into_iter()
.filter_map(|repo_key| haystack.get(repo_key).map(|r| (*r).to_owned()))
.collect::<Vec<_>>();
matched_repos
}
}

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

@ -1,10 +1,4 @@
use anyhow::Context; use octocrab::{models::Repository, params::repos::Sort, Octocrab, Page};
use octocrab::{
auth::Auth,
models::{hooks::Config, Repository},
params::repos::Sort,
NoSvc, Octocrab, Page,
};
use crate::{app::App, config::GitHubAccessToken}; use crate::{app::App, config::GitHubAccessToken};
@ -159,7 +153,7 @@ impl GitHubProvider {
fn get_client( fn get_client(
&self, &self,
url: Option<&String>, _url: Option<&String>,
access_token: &GitHubAccessToken, access_token: &GitHubAccessToken,
) -> anyhow::Result<Octocrab> { ) -> anyhow::Result<Octocrab> {
let client = octocrab::Octocrab::builder() let client = octocrab::Octocrab::builder()

View File

@ -12,6 +12,7 @@ mod cache;
mod cache_codec; mod cache_codec;
mod commands; mod commands;
mod config; mod config;
mod fuzzy_matcher;
mod git_provider; mod git_provider;
mod projects_list; mod projects_list;
@ -20,6 +21,9 @@ mod projects_list;
struct Command { struct Command {
#[command(subcommand)] #[command(subcommand)]
command: Option<Commands>, command: Option<Commands>,
#[arg()]
search: Option<String>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -51,7 +55,7 @@ async fn main() -> anyhow::Result<()> {
match cli.command { match cli.command {
Some(_) => todo!(), Some(_) => todo!(),
None => { None => {
RootCommand::new(app).execute().await?; RootCommand::new(app).execute(cli.search.as_ref()).await?;
} }
} }