Compare commits

...

2 Commits

Author SHA1 Message Date
cuddle-please
3b2641dd14 chore(release): 0.2.1
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-09-15 20:37:14 +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
3 changed files with 61 additions and 12 deletions

View File

@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.2.1] - 2024-09-15
### Added
- implement naive fuzzy matcher
### Other
- refactor fuzzy match into own function
- cleanup warnings
- move fuzzy search out of command
- refactor/matcher move to a separate file
- move fuzzy search out of command
- Actually add fuzzy matcher
- extract matcher
- update dependencies
- *(deps)* update rust crate anyhow to v1.0.89
## [0.2.0] - 2024-09-14
### Added

View File

@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"
[workspace.package]
version = "0.2.0"
version = "0.2.1"
[workspace.dependencies]
gitnow = { path = "crates/gitnow" }

View File

@ -1,5 +1,11 @@
use std::collections::BTreeMap;
use crate::{
app::App, cache::CacheApp, fuzzy_matcher::FuzzyMatcherApp, projects_list::ProjectsListApp,
app::App,
cache::CacheApp,
fuzzy_matcher::{FuzzyMatcher, FuzzyMatcherApp},
git_provider::Repository,
projects_list::ProjectsListApp,
};
#[derive(Debug, Clone)]
@ -31,16 +37,11 @@ impl RootCommand {
None => todo!(),
};
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<_>>();
let matched_repos = self
.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);
@ -61,3 +62,33 @@ impl StringExt for Vec<String> {
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
}
}